• Swiper-轮播图


    Swiper(Swiper master)是目前应用较广泛的移动端网页触摸内容滑动js插件,可以用来做轮播和滑动.

    • 初始化

      复制代码
      <!DOCTYPE html>
      <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <link rel="stylesheet" type="text/css" href="swiper.css"/>
            <style>
                .swiper-container {
                     600px;
                    height: 300px;
                } 
                .swiper-slide{
                    font-size: 50px
                }
                .swiper-slide:nth-of-type(1){
                    background-color: cornflowerblue;
                }
                .swiper-slide:nth-of-type(2){
                    background-color: coral;
                }
                .swiper-slide:nth-of-type(3){
                    background-color: yellowgreen;
                }
            </style>
        </head>
        <body>
            <div class="swiper-container">
                <div class="swiper-wrapper">
                    <div class="swiper-slide">Slide 1</div>
                    <div class="swiper-slide">Slide 2</div>
                    <div class="swiper-slide">Slide 3</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>
            <!--导航等组件可以放在container之外-->
      
            <script src="swiper.js"></script>
            <script>        
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'vertical',
      //                loop: true,
      //                
      //                // 如果需要分页器
                    pagination: '.swiper-pagination',
      //                
      //                // 如果需要前进后退按钮
                    nextButton: '.swiper-button-next',
                    prevButton: '.swiper-button-prev',
      //                
      //                // 如果需要滚动条
                    scrollbar: '.swiper-scrollbar',
                })
              </script>
        </body>
      </html>
      复制代码
    • 基本配置
      复制代码
      var mySwiper = new Swiper ('.swiper-container', {
                         // 滑动方向
                        // horizontal水平
                        // vertical垂直
                    direction: 'horizontal',
                    // 初始化时候slide的索引(从0开始)
                    initialSlide: 1,                
                    // 手指松开至slide贴合的时间
                    speed:3000,                
                    // 设置自动播放的事件间隔
                    autoplay: 2000,
                    // 可显示数量
                    slidesPerView:2,                
                    // 滑块居中
                    centeredSlides:true,
                })
      复制代码
    • 触摸设置

      复制代码
        var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',
      
                    // 触摸距离与slide滑动距离的比率 
                    touchRatio:0.1,
      
                    // 无法滑动
                    onlyExternal:true,
      
                    // 滑块跟随手指进行移动
                    followFinger:false,
      
                    // 定义longSwipesMs
                    longSwipesMs:1000,
      
                    longSwipes:false,
      
                    shortSwipes:false,
      
                    longSwipesRatio:0.5,
      
                    touchAngle:10,
                })
      禁止切换和前进后退
      
       <body>
           <div class="swiper-container">
               <div class="swiper-wrapper">
                   <div class="swiper-slide stop">Slide 1</div>
                   <!--<div class="swiper-slide swiper-no-swiping">Slide 2</div>-->
                   <div class="swiper-slide">Slide 2</div>
                   <div class="swiper-slide">Slide 3</div>
               </div>
           </div>
           <button class="prev">prev</button>
           <button class="next">next</button>
      
           <script src="swiper.js"></script>
           <script>        
               var mySwiper = new Swiper ('.swiper-container', {
                   direction: 'horizontal',
                   noSwiping:true,
                   noSwipingClass : "stop",
                   nextButton : ".next",
                   prevButton : ".prev",
               })
             </script>
      分页器
      
            <style>
                .swiper-container {
                     600px;
                    height: 300px;
                } 
                .swiper-slide{
                    font-size: 50px
                }
                .swiper-slide:nth-of-type(1){
                    background-color: cornflowerblue;
                }
                .swiper-slide:nth-of-type(2){
                    background-color: coral;
                }
                .swiper-slide:nth-of-type(3){
                    background-color: yellowgreen;
                }
                .swiper-pagination-bullet{
                     20px;
                    height: 20px;
                }
                .swiper-pagination-bullet-active{
                    background-color: yellow;
                }
            </style>
        </head>
        <body>
            <div class="swiper-container">
                <div class="swiper-wrapper">
                    <div class="swiper-slide">Slide 1</div>
                    <div class="swiper-slide">Slide 2</div>
                    <div class="swiper-slide">Slide 3</div>
                </div>
                <div class="swiper-pagination"></div>
            </div>
      
            <script src="swiper.js"></script>
            <script>        
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',                
                    pagination : ".swiper-pagination",                
                    paginationType : "bullets",
                    paginationType : "fraction",
                    paginationType : "progress",                
                    paginationClickable : true,
                    paginationHide : true,
                    paginationElement : "i",
                    paginationBulletRender : function( swiper,index,classname ){
                        return "<span class='"+ classname +"'>"+ (index+1) +"</span>"
                    }
                })
              </script>
        </body>
      切换效果
      
        <script>        
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',
      
                    effect : "slide",
                    effect : "fade",
                    effect : "cube",
                    effect : "coverflow",
                    effect : "flip",
                })
              </script>
      进程
      
      <body>
            <div class="swiper-container">
                <div class="swiper-wrapper">
                    <div class="swiper-slide">Slide 1</div>
                    <div class="swiper-slide">Slide 2</div>
                    <div class="swiper-slide">Slide 3</div>
                </div>
            </div>
            <button id="btn">按钮</button>
      
            <script src="swiper.js"></script>
            <script>        
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',
      
                })
      
                btn.onclick = function(){
                    alert( mySwiper.progress );
                    alert( mySwiper.slides[0].progress );
                    console.log( mySwiper.slides[0].progress,mySwiper.slides[1].progress,mySwiper.slides[2].progress );
                }
      
                setInterval(function(){
                    console.log( mySwiper.slides[0].progress,mySwiper.slides[1].progress,mySwiper.slides[2].progress );
                },20)
              </script>
        </body>
      复制代码
    • 常用属性和回调

      复制代码
      <body>
            <div class="swiper-container">
                <div class="swiper-wrapper">
                    <div class="swiper-slide">Slide 1</div>
                    <div class="swiper-slide">Slide 2</div>
                    <div class="swiper-slide">Slide 3</div>
                </div>
            </div>
            <button id="btn">按钮</button>
            <script src="swiper.js"></script>
            <script>        
                var mySwiper = new Swiper ('.swiper-container', {
                    direction: 'horizontal',
      
                    speed : 2000,
      
                    onSlideChangeStart : function(){
                        console.log( "开始滑动" );
                    },
                    onSlideChangeEnd : function(){
                        console.log( "滑动结束" );
                    }
                })
      
                console.log( mySwiper.width );
                console.log( mySwiper.height );
      
                btn.onclick = function(){
                    console.log( mySwiper.translate );
                    console.log( mySwiper.activeIndex );
                    console.log( mySwiper.previousIndex );
                }        
              </script>
        </body>
      复制代码
  • 相关阅读:
    Php ArrayIterator的几个常用方法
    13 子元素和后代元素选择器 1 元素之间的关系 2 后代元素选择器 后代 元素用的是空格 3 子元素选择器 用的是>号 4 ietest的使用
    12常用选择器 1元素选择器 2 id选择器3 class类选择器 4 一个元素可以设置多个class属性值 5 选择器分组又叫并集选择器 6 通配选择器 * 7 复合选择器 交集选择器
    11 1 块元素div的定义 2 常见的块元素 3 块元素的用途 4 内联元素,行内元素,span 5 内联元素和块元素的用途 6 a元素超链接的用法 7 p元素不可以包含任何其他的块元素
    10 1、IDE,dreamweaver,webstorm,hbuilder 2 Hbuilder快捷键
    9 1 css的注释 2 css的语法:选择器,声明块
    8 1 css 2 元素就是标签 2 内联样式 3 css选择器 style ,head ,内部样式表 4 外部样式表 css文件, link标签 ,href,
    7 1 超链接 a标签 href ,_self,_blank 2 center标签 居中显示 3 # 4 回到顶部 5 id定位 6 电子邮件超链接
    5 1 html的语法规范 大小写,注释嵌套,标签成对出现,属性,值,加引号
    4 1meta标签,name ,content,keywords,description,url,refresh
  • 原文地址:https://www.cnblogs.com/a1847786534/p/7080045.html
Copyright © 2020-2023  润新知