Swiper

 

一、swiper基本使用

1.1 什么是swiper?

Swiper是纯javascript打造的滑动特效插件,面向PC、平板电脑等移动终端。

Swiper能实现触屏焦点图、触屏Tab切换等常用效果。

Swiper开源、免费、稳定、使用简单、功能强大,是架构移动终端网站的重要选择!

这里以 swiper7为例

     

1.2 如何使用swiper?

  1. 引入swiper对应的css和js文件(swiper-bundle.min.js和swiper-bundle.min.css文件,不同Swiper版本用到的文件名略有不同)
  2. 按照框架的需求搭建三层结构
  3. 创建一个Swiper对象, 将容器元素传递给它

1.3 示例

    <link rel="stylesheet" href="css/swiper-bundle.min.css">
    <script src="js/swiper-bundle.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .swiper{
            width: 400px;
            height: 300px;
            border: 1px solid #000;
            margin: 100px auto;
            overflow: hidden;
        }
        .swiper>ul{
            list-style: none;
        }
    </style>
<div class="swiper">
    <ul class="swiper-wrapper">
        <li class="swiper-slide">Slide 1</li>
        <li class="swiper-slide">Slide 2</li>
        <li class="swiper-slide">Slide 3</li>
    </ul>
</div>
let mySwiper = new Swiper ('.swiper');

 


二、swiper高级使用

除了基本的使用以外还可以设置分页器、导航按钮、自动轮播、无限轮播等,具体操作可参考:

swiper中文官网API文档

2.1 示例

    <link rel="stylesheet" href="css/swiper-bundle.min.css" />
    <script src="js/swiper-bundle.min.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .swiper {
        width: 400px;
        height: 300px;
        border: 1px solid #000;
        margin: 100px auto;
        overflow: hidden;
        position: relative;
      }
      .swiper > ul {
        list-style: none;
        position: absolute;
      }
      .swiper-pagination-bullet {
        background: red;
      }
      .swiper-button-next {
        background: red;
      }
    </style>
    <div class="swiper">
      <ul class="swiper-wrapper">
        <li class="swiper-slide">Slide 1</li>
        <li class="swiper-slide">Slide 2</li>
        <li class="swiper-slide">Slide 3</li>
      </ul>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>
      <!-- 如果需要导航按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </div>
     let mySwiper = new Swiper(".swiper", {
        // 如果需要分页器
        pagination: {
          el: ".swiper-pagination",
        },
        // 如果需要前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        loop: true, // 循环模式选项
        autoplay:true, // 自动轮播
        autoplay: {
            delay: 1000,//1秒切换一次
            disableOnInteraction: false,//用户交互
        },
        speed: 2000, //设置切换速度
      });

 

2.3 示例2--移动端轮播图

    <link rel="stylesheet" href="css/swiper-bundle.min.css" />
    <script src="js/swiper-bundle.min.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
        touch-action: none;
      }
      div {
        width: 100%;
      }
      ul {
        list-style: none;
      }
      ul > li > img {
        width: 100%;
        vertical-align: bottom;
      }
      .my-bullet {
        display: inline-block;
        width: 20px;
        height: 20px;
        background: #fff;
        border-radius: 50%;
        margin: 0 5px;
      }
      .my-bullet-active {
        background: #f40;
        opacity: 1;
      }
      .swiper-button-prev,
      .swiper-button-next {
        width: 30px;
        height: 50px;
        background: rgba(0, 0, 0, 0.3);
      }
      .swiper-button-prev:after,
      .swiper-button-next:after {
        content: "";
      }
      .swiper-button-prev::before {
        content: "<";
        display: inline-block;
        width: 30px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        font-weight: bold;
        font-size: 30px;
        color: #fff;
      }
      .swiper-button-next::before {
        content: ">";
        display: inline-block;
        width: 30px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        font-weight: bold;
        font-size: 30px;
        color: #fff;
      }
    </style>
    <div class="swiper">
      <ul class="swiper-wrapper">
        <li class="swiper-slide"><img src="images/img1.jpg" alt="" /></li>
        <li class="swiper-slide"><img src="images/img2.jpg" alt="" /></li>
        <li class="swiper-slide"><img src="images/img3.jpg" alt="" /></li>
        <li class="swiper-slide"><img src="images/img4.jpg" alt="" /></li>
      </ul>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>
      <!-- 如果需要导航按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>
    </div>
      let mySwiper = new Swiper(".swiper", {
        // 如果需要分页器
        pagination: {
          el: ".swiper-pagination",
          bulletClass: "my-bullet", //设置分页器默认状态下的类名
          bulletActiveClass: "my-bullet-active", //自定义分页器内当前活动块的指示小点的类名
        },
        // 如果需要前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        loop: true, // 循环模式选项
        autoplay: {
          delay: 2000,
          disableOnInteraction: false, //用户交互
        },
      });

 


三、Swiper Animate使用方法

用于在Swiper内快速制作CSS3动画效果的小插件,适用于Swiper2.x、Swiper3.x、Swiper4.x和Swiper5.x

这里以 swiper4.x 为例

具体可参考:swiper动画

3.1 示例

    <link rel="stylesheet" href="css/swiper.css">
    <link rel="stylesheet" href="css/swiper.animate.min.css">
    <script src="js/swiper.js"></script>
    <script src="js/swiper.animate1.0.3.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        p{
            width: 200px;
            height: 200px;
            line-height: 200px;
            text-align: center;
            background: #f00;
            margin: 0 auto;
        }

        /* 自定义动画 */
        @keyframes myFadeIn {
            0% {
                opacity: 0;
                transform: scale(2);
            }

            100% {
                opacity: 1;
                transform: scale(0.5);
            }
        }

        .myFadeIn {
            -webkit-animation-name: myFadeIn;
            /* 切换自定义动画名称为自己的 */
            animation-name: myFadeIn; 
        }
    </style>
<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">
            <!--
            swiper-animate-effect:切换效果,例如 fadeInUp
            swiper-animate-duration:可选,动画持续时间(单位秒),例如 0.5s
            swiper-animate-delay:可选,动画延迟时间(单位秒),例如 0.3s
            -->
            <p class="ani" swiper-animate-effect="myFadeIn">内容</p>
        </div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
    </div>
</div>
    let mySwiper = new Swiper ('.swiper-container', {
        on:{
            init: function(){
                swiperAnimateCache(this); //隐藏动画元素 
                swiperAnimate(this); //初始化完成开始动画
            },
            slideChangeTransitionEnd: function(){
                swiperAnimate(this); //每个slide切换结束时也运行当前slide动画
            }
        }
    });

 


四、综合案例--酷狗音乐

    <title>酷狗音乐手机版</title>
    <meta name="keywords" content="酷狗音乐手机版,酷狗,音乐">
    <meta name="description" content="酷狗音乐手机版">
    <link rel="shortcut icon" href="favicon.ico">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/swiper.css">
    <link rel="stylesheet" href="css/swiper.animate.min.css">
    <link rel="stylesheet" href="css/index.css">
    <script src="js/swiper.js"></script>
    <script src="js/swiper.animate1.0.3.min.js"></script>
    <script src="js/index.js"></script>
<div class="swiper-container">
    <div class="swiper-wrapper">
        <!--第一屏-->
        <div class="swiper-slide section1">
            <img src="images/section1-title.png" class="ani" swiper-animate-effect="myFadeIn">
            <img src="images/section1-img.png" class="ani" swiper-animate-effect="fadeInUp">
            <img src="images/section1-btn.png" alt="">
        </div>

        <!--第二屏-->
        <div class="swiper-slide section">
            <img src="images/section2-title.png" class="ani" swiper-animate-effect="myFadeIn" swiper-animate-duration="0.5s">
            <img src="images/section2-img.png" class="ani" swiper-animate-effect="fadeInUp" swiper-animate-delay="0.5s">
        </div>

        <!--第三屏-->
        <div class="swiper-slide section">
            <img src="images/section3-title.png" class="ani" swiper-animate-effect="myFadeIn" swiper-animate-duration="0.5s">
            <img src="images/section3-img.png" class="ani" swiper-animate-effect="fadeInUp" swiper-animate-delay="0.5s">
        </div>

        <!--第四屏-->
        <div class="swiper-slide section">
            <img src="images/section4-title.png" class="ani" swiper-animate-effect="myFadeIn" swiper-animate-duration="0.5s">
            <img src="images/section4-img.png" class="ani" swiper-animate-effect="fadeInUp" swiper-animate-delay="0.5s">
        </div>

        <!--第五屏-->
        <div class="swiper-slide section">
            <img src="images/section5-title.png" class="ani" swiper-animate-effect="myFadeIn" swiper-animate-duration="0.5s">
            <img src="images/section5-img.png" class="ani" swiper-animate-effect="fadeInUp" swiper-animate-delay="0.5s">
        </div>

        <!--第六屏-->
        <div class="swiper-slide section6">
            <div class="top ani" swiper-animate-effect="myFadeIn">
                <img src="images/section6-logo.png" alt="">
                <p>适用于iOS 8.0及以上系统版本</p>
                <p>
                    <img src="images/section6-back.png" alt="">
                    <img src="images/section6-share.png" alt="">
                </p>
                <p>我们致力于推动网络正版音乐的发展</p>
                <p>相关版权合作请联系copyrights@kugou.com</p>
                <p>
                    <a href="#">在线听歌</a>
                    <span>|</span>
                    <a href="#">用户服务协议</a>
                    <span>|</span>
                    <a href="#">卸载说明</a>
                </p>
            </div>
            <div class="bottom">
                <p>粤ICP备09017694号-2</p>
                <p>公司名称:广州酷狗计算机科技有限公司</p>
                <p>公司地址:广州市天河区黄埔大道中315号羊城晚报创意产业园内自编号1-18</p>
                <p>公司电话:<i>020-37529000</i></p>
            </div>
        </div>
    </div>
</div>

index.css,

html,body{
    width: 100%;
    height: 100%;
}
body{
    background: url("./../images/bg.png") no-repeat center bottom;
    background-size: 100% auto;
}
.swiper-container, .swiper-wrapper, .swiper-slide{
    width: 100%;
    height: 100%;
}
/*第一屏*/
.section1>img{
    display: block;
    margin: 0 auto;
}
.section1>img:nth-of-type(1){
    width: 49.58%;
    margin-top: 10.9375%;
}
.section1>img:nth-of-type(2){
    width: 53.88%;
    margin-top: 10%;
}
.section1>img:nth-of-type(3){
    width: 56%;
    margin-top: 10%;
}
@keyframes myFadeIn {
    0% {
        opacity: 0;
        transform: scale(2);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}
.myFadeIn {
    -webkit-animation-name: myFadeIn;
    animation-name: myFadeIn
}
/*第2~5屏*/
.section>img{
    display: block;
    margin: 0 auto;
}
.section>img:nth-of-type(1){
    width: 60.8%;
    margin-top: 10.9375%;
}
.section>img:nth-of-type(2){
    width: 68.2%;
    margin-top: 10%;
}
/*第六屏*/
.section6>.top{
    margin-top: 30%;
}
.section6>.top>img:nth-of-type(1){
    display: block;
    margin: 0 auto;
    width: 26%;
}
.section6>.top>p:nth-of-type(1){
    font-size: 14px;
    color: #9c9c9c;
    text-align: center;
    margin-top: 2%;
}
.section6>.top>p:nth-of-type(2){
    text-align: center;
    margin-top: 5%;
}
.section6>.top>p:nth-of-type(2) img{
    width: 33%;
    margin: 0 10px;
}
.section6>.top>p:nth-of-type(3), .section6>.top>p:nth-of-type(4){
    margin-top: 10%;
    font-size: 14px;
    color: #666;
    line-height: 25px;
    text-align: center;
}
.section6>.top>p:nth-of-type(4){
     margin-top: 0;
 }
.section6>.top>p:nth-of-type(5){
    color: #0090ff;
    margin-top: 15%;
    text-align: center;
    font-size: 14px;
}
.section6>.top>p:nth-of-type(5) a{
    color: #0090ff;
}
.section6>.top>p:nth-of-type(5) span{
    margin: 0 5%;
}
.section6>.bottom{
    width: 100%;
    position: absolute;
    left: 0;
    bottom: 20px;
}
.section6>.bottom>p{
    font-size: 12px;
    color: #9c9c9c;
    text-align: center;
    line-height: 20px;
}
.section6>.bottom>p>i{
    color: #0090ff;
}

index.js,

window.onload = function () {
    let mySwiper = new Swiper ('.swiper-container', {
        direction: 'vertical', // 垂直切换选项
        on:{
            init: function(){
                swiperAnimateCache(this); //隐藏动画元素
                swiperAnimate(this); //初始化完成开始动画
            },
            slideChangeTransitionEnd: function(){
                swiperAnimate(this); //每个slide切换结束时也运行当前slide动画
            }
        }
    });
}

 

  • 1
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白小白从不日白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值