微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React Owl Carousal 响应在较小的屏幕中不起作用

如何解决React Owl Carousal 响应在较小的屏幕中不起作用

我的猫头鹰轮播在较小的屏幕或移动屏幕上没有响应。我想找到解决这个问题的方法。 以下是截图:

  1. https://i.stack.imgur.com/aKpth.png

  2. https://i.stack.imgur.com/In8Mb.png

  3. https://i.stack.imgur.com/NY8cy.png

这是代码。提前致谢

    export default function HomeMovieSlider() {
  
    return (
  <div>
    <OwlCarousel className='owl-theme'
    loop center margin={1} autoplay ={true} dots={false} items={3} touchDrag={true} lazyLoad={true} 
    
    // responsive={"0:{items:1,},600:{items:3,1000:{items:5,}"}
    animateOut={'fadeOut'} animateIn={'flipInX'}>
    {MOVIEBANNER.map((movieBannertop) => {
        return (
            <div  >
                {/* <h4>{movieBannerp.movieTitle}</h4> */}
                {/* <img src={movieBannertop.bannerImage}/> */}
                <Card style={{width: "250px"}}> 
                    <Card.Img variant="top" src={movieBannertop.bannerImage} height="380" max-width= "100% !important"/>
                    <Card.Body> 
                      <Card.Title as="h6" ><b>{movieBannertop.movieTitle}</b></Card.Title>
                      <Card.Subtitle className="mb-2 text-muted">{movieBannertop.genres}</Card.Subtitle>
                      <Link to="/MovieDetail"><small className="text-muted">More Details</small></Link>
                    </Card.Body>
                    <Card.Footer className="text-center">
                      
                      <Button variant="danger" style={{backgroundColor: "#ff4444"}}><Link to="/movieBooking" style={{color: "#fff"}}>Book Now</Link></Button>
                    </Card.Footer> 
                  </Card> 
            </div>  
        );
      })}
    </OwlCarousel>
    <div className="col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12"></div>
    <div className="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
    <div className="prs_animate_btn1 prs_upcom_main_wrapper prs_third_slider_btn">
      <ul>
        <li data-animation="animated fadeInUp"><a href="/#/movies" className="button button--tamaya prs_upcom_main_btn" data-text="MORE"><span>MORE</span></a>
        </li>
      </ul>
    </div>
    </div>
    <div className="col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12"></div>
  </div>
    );
  }

解决方法

为了使 owl-carousel 具有响应性,您应该使用 state 属性创建一个名为 responsive 的对象,并将其发送到 responsiveOwlCarousel 属性。

export default function HomeMovieSlider() {
    const state= {
        responsive:{
            0: {
                items: 1,},450: {
                items: 2,600: {
                items: 3,1000: {
                items: 4,}
  return (
    <div>
      <OwlCarousel
        className="owl-theme"
        loop
        center
        margin={1}
        autoplay={true}
        dots={false}
        items={3}
        touchDrag={true}
        lazyLoad={true}
         responsive={state.responsive}// add this line
        animateOut={'fadeOut'}
        animateIn={'flipInX'}
      >
        {MOVIEBANNER.map(movieBannertop => {
          return (
            <div>
              {/* <h4>{movieBannerp.movieTitle}</h4> */}
              {/* <img src={movieBannertop.bannerImage}/> */}
              <Card style={{ width: '250px' }}>
                <Card.Img
                  variant="top"
                  src={movieBannertop.bannerImage}
                  height="380"
                  max-width="100% !important"
                />
                <Card.Body>
                  <Card.Title as="h6">
                    <b>{movieBannertop.movieTitle}</b>
                  </Card.Title>
                  <Card.Subtitle className="mb-2 text-muted">
                    {movieBannertop.genres}
                  </Card.Subtitle>
                  <Link to="/MovieDetail">
                    <small className="text-muted">More Details</small>
                  </Link>
                </Card.Body>
                <Card.Footer className="text-center">
                  <Button
                    variant="danger"
                    style={{ backgroundColor: '#ff4444' }}
                  >
                    <Link to="/movieBooking" style={{ color: '#fff' }}>
                      Book Now
                    </Link>
                  </Button>
                </Card.Footer>
              </Card>
            </div>
          );
        })}
      </OwlCarousel>       
  );
}

对于其他 owl-carousel 的 API,请参阅此链接:https://github.com/laurenchen0631/react-owl-carousel

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。