PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > Jquery教程 > 正文

    jquery 移动端无缝轮播

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:jquery 移动端无缝轮播
    jquery 移动端无缝轮播

    1. <!DOCTYPE html> 
    2. <html> 
    3.         <head> 
    4.                 <meta charset="UTF-8"
    5.                 <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">                 
    6.                 <title>无缝轮播图</title> 
    7.                   <style> 
    8.                     *{margin: 0;padding:0; } 
    9.                     ul{list-style: none;} 
    10.                     .banner{width:100vw;height: 55vw;position: relative;overflow: hidden;} 
    11.                     .img{position: absolute;top: 0;left: 0} 
    12.                     .img li{float: left;width:100vw; height:55vw} 
    13.                     .img li img{ 
    14.                             width:100vw; 
    15.                             height:55vw; 
    16.                     } 
    17.                     .num{position: absolute;bottom: 10px;width: 100vw;text-align: center;font-size: 0;} 
    18.                     .num li{width: 10px;height: 10px;background:rgba(0,0,0,0.5);display: block;border-radius: 100%;display: inline-block;margin: 0 5px;cursor: pointer;} 
    19.                     .num .active{background-color: #fff;} 
    20.                   </style> 
    21.                   <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> 
    22.         </head> 
    23.         <body> 
    24.             <div class="banner"
    25.                           <ul class="img"
    26.                           </ul> 
    27.                           <ul class="num"></ul> 
    28.                           <div class="btn"
    29.                             <span class="prev"><</span> 
    30.                             <span class="next">></span> 
    31.                           </div>         
    32.                 </div>         
    33.         </body>         
    34.                 <script>                   
    35.                   $(function(){                   
    36.                     var i=0; 
    37.                     var timer=null
    38.                     var str=''
    39.                     for (var k= 0; k < 5; k++) {  //创建图片 
    40.                        str+="<li><a href='###'><img src='img/5.jpg' alt='第5张图片'></a></li>" 
    41.                     } 
    42.                     console.log(str) 
    43.                      $('.img').append(str); 
    44.  
    45.                     for (var j = 0; j < $('.img li').length; j++) {  //创建圆点 
    46.                       $('.num').append('<li></li>'
    47.                     } 
    48.                     $('.num li').first().addClass('active'); //给第一个圆点添加样式 
    49.                    
    50.                     var firstimg=$('.img li').first().clone(); //复制第一张图片 
    51.                     $('.img').append(firstimg).width($('.img li').length*($('.img img').width())); //将第一张图片放到最后一张图片后,设置ul的宽度为图片张数*图片宽度 
    52.  
    53.            //判断左右滑动切换 
    54.                         $('.img').on("touchstart"function(e) { 
    55. //                            e.preventDefault(); 
    56.                             startX = e.originalEvent.changedTouches[0].pageX; 
    57. //                            e.stopPropagation(); 
    58.                 return false
    59.                         }); 
    60.                         $('.img').on("touchend"function(e) { 
    61. //                            e.preventDefault(); 
    62. //                            e.stopPropagation(); 
    63.                             moveEndX = e.originalEvent.changedTouches[0].pageX, 
    64.                             X = moveEndX - startX,         
    65.                             console.log(X) 
    66.                             if ( X > 0 ) { 
    67.                                   clearInterval(timer); 
    68.                               i--; 
    69.                               if (i==-1) { 
    70.                                 i=$('.img li').length-2; 
    71.                                 $('.img').css({left:-($('.img li').length-1)*$('.img img').width()}); 
    72.                               } 
    73.                               $('.img').stop().animate({left:-i*$('.img img').width()},300); 
    74.                               $('.num li').eq(i).addClass('active').siblings().removeClass('active');     
    75.                             } 
    76.                             else if ( X < 0 ) { 
    77.                                   clearInterval(timer); 
    78.                               i++; 
    79.                               if (i==$('.img li').length) { 
    80.                                 i=1; //这里不是i=0 
    81.                                 $('.img').css({left:0}); //保证无缝轮播,设置left值 
    82.                               }; 
    83.                            
    84.                               $('.img').stop().animate({left:-i*$('.img img').width()},300); 
    85.                               if (i==$('.img li').length-1) {   //设置小圆点指示 
    86.                                 $('.num li').eq(0).addClass('active').siblings().removeClass('active'); 
    87.                               }else
    88.                                 $('.num li').eq(i).addClass('active').siblings().removeClass('active'); 
    89.                               } 
    90.                             } 
    91.                             return false
    92.                         });                 
    93.                     //定时器自动播放 
    94.                     timer=setInterval(function(){ 
    95.                       i++; 
    96.                       if (i==$('.img li').length) { 
    97.                         i=1; 
    98.                         $('.img').css({left:0}); 
    99.                       }; 
    100.                    
    101.                       $('.img').stop().animate({left:-i*$('.img img').width()},300); 
    102.                       if (i==$('.img li').length-1) {  
    103.                         $('.num li').eq(0).addClass('active').siblings().removeClass('active'); 
    104.                       }else
    105.                         $('.num li').eq(i).addClass('active').siblings().removeClass('active'); 
    106.                       } 
    107.                     },1500)                                    
    108.                   }) 
    109.                 </script>         
    110. </html> 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-47-5091-1.html
    相关热词搜索: