使用swiper来实现轮播图

1、效果图

1

2、官方资料

http://www.swiper.com.cn/usage/index.html 它很简明地告诉了你应该如何去搭建这样的框架。
http://www.swiper.com.cn/api/ 这里讲述了我们应该如何去设置更多的功能。

3、源码
<!DOCTYPE html>
<html>
<head>
    <title>swiper</title>
    <style>
        .swiper-container {
            width: 600px;
            height: 300px;
        }  
        .swiper-slide{
            width: 600px;
            height: 300px;
        }
        .swiper-slide img{
            width: 100%;
            height: 100%;
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/css/swiper.min.css">
</head>
<body>
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide"><img src="http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg"/></div>
            <div class="swiper-slide"><img src="http://pic55.nipic.com/file/20141208/19462408_171130083000_2.jpg" alt=""></div>
            <div class="swiper-slide"><img src="http://img3.imgtn.bdimg.com/it/u=2714944387,2569159950&fm=23&gp=0.jpg" alt=""></div>
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination"></div>
        
        <!-- 如果需要导航按钮 -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
        
        <!-- 如果需要滚动条 -->
        <div class="swiper-scrollbar"></div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/3.4.1/js/swiper.min.js"></script>
    <script type="text/javascript">
    window.onload = function() {
      var mySwiper = new Swiper ('.swiper-container', {
        // 轮播图的方向,也可以是vertical方向
        direction:'horizontal',
        
        //播放速度
        loop: true,

        // 自动播放时间
        autoplay:1000,

        // 播放的速度
        speed:2000,
        
        // 如果需要分页器,即下面的小圆点
        pagination: '.swiper-pagination',
        
        // 如果需要前进后退按钮
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',           // 这样,即使我们滑动之后, 定时器也不会被清除      autoplayDisableOnInteraction : false,
        
        // 如果需要滚动条
        scrollbar: '.swiper-scrollbar',
      });
    }        
    </script>
</body>
</html>

4、其他

(1) swiper禁止手动滑动

只需要在最外层的容器上增加class=“swiper-no-swiping”(在引入了swiper相关css的前提下)

(2)swiper页面切换后swiper无法滚动问题解决

<div className="swiper-container white-home-swiper swiper-no-swiping" ref={() => this.refSwiper()}>
    <div className="swiper-wrapper">
        {noticeList.map((item, i) => {
            return (
                <div className="secretary-item swiper-slide" key={i} onClick={() => { this.goTo(`articleDetail/${item.ID}?type=通告详情`) }} >
                    {item.hasRead ? <Badge >{item.Title}</Badge> : <Badge dot>{item.Title}</Badge>}
                </div>
            );
        })}
    </div>
</div>
componentWillUnmount() {
    this.swiper && this.swiper.destroy();
    this.swiper = undefined;
}
componentDidUpdate() {
    this.swiper && this.swiper.update();
}
refSwiper() {
    this.swiper = new Swiper(".swiper-container", {
        direction: "vertical",
        slidesPerView: 1,
        autoplay: {
            delay: 4000,
            disableOnInteraction: false
        }
    });
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值