JS realizes automatic carousel effect (adaptive screen width + mobile phone touch screen sliding)_javascript skills

陈政宽~
Release: 2017-06-28 14:28:45
Original
4491 people have browsed it

This article mainly introduces JS to realize the automatic carousel effect (adaptive screen width + mobile phone touch screen sliding). Friends in need can refer to the following

1. This article uses js+jQuery to realize the carousel Figure needs to reference the jquery package. Another implementation is animate to implement adaptive carousel, and transform to smooth carousel (in the comment code).

2. You can change the pictures in the code yourself. The style and logic are all written in js.

3, html tagcode, js code


<p class="slider">
  //轮播箭头
 <p class="lastpic"><img src="../images/prev.png"></p>
 <p class="nextpic"><img src="../images/next.png"></p>
 //轮播图片
 <ul id="slides" class="slides clearfix">
 <li><img class="responsive" src="../images/wrap-page.jpg" alt="暂无图片"></li>
 <li><img class="responsive" src="../images/wrap-page.jpg" alt="暂无图片"></li>
 <li><img class="responsive" src="../images/wrap-page.jpg" alt="暂无图片"></li>
 <li><img class="responsive" src="../images/wrap-page.jpg" alt="暂无图片"></li>
 </ul>
</p>
Copy after login


 <script type="text/javascript">
  $(document).ready(function() {
  var len = $(".slider li").length-1;
  //给slider设置样式
  $(".slider").css({
   "width":"100%",
   "height": "inherit",
   "overflow": "hidden",
   "display":"inline-block"
  });
  
  //给ul设置宽度
  $(".slides").css({
   "position": "relative",
   "width":((len+1)*100).toString()+"%",
   "margin":"0",
   "padding":"0"});
  //给li设置百分比宽度
  $(".slides li").css({
   "width":(100/(len+1)).toString()+"%",
   "float":"left"
  });
  //给图片设置宽度
  $(".responsive").css({
   "width":"100%",
   "height":"inherit"
  });
  //控制点样式
  $(".slider p").css({
   "position": "absolute",
   "z-index":"999",
   "cursor": "pointer"
  });
  $(".slider .lastpic").css({
   "left":"0",
   "margin-top":"7%"
  });
  $(".slider .nextpic").css({
   "right":"0",
   "margin-top":"7%"
  });
  //animate移动
  var i = 0;
  $(".nextpic").click(function(){
   moveNext(i);
  });
  $(".lastpic").click(function(){
   moveLast(i);
  });
  //自动轮播
  var timer = setInterval(function(){
   moveNext(i);
  },5000);
  moveNext = function(n){
   if(n==len){
   i=-1;
   $(".slider .slides").animate({right: ""},800);
   }else{
   $(".slider .slides").animate({right:((n+1)*100).toString()+"%"}, 800);
   }
   i++;
  }
  moveLast = function(n){
   if(n==0){
   i=len+1;
   $(".slider .slides").animate({right:(len*100).toString()+"%"}, 800);
   }else{
   $(".slider .slides").animate({right:((n-1)*100).toString()+"%"}, 800);
   }
   i--;
  }
  //手机触摸效果
  var startX,endX,moveX;
  function touchStart(event){
   var touch = event.touches[0];
   startX = touch.pageX;
  }
  function touchMove(event){
   var touch = event.touches[0];
   endX = touch.pageX;
  }
  function touchEnd(event){
   moveX = startX - endX;
   if(moveX>50){
   moveNext(i);
   }else if(moveX<-50){
   moveLast(i);
   }
  }
  document.getElementById("slides").addEventListener("touchstart",touchStart,false);
  document.getElementById("slides").addEventListener("touchmove",touchMove,false);
  document.getElementById("slides").addEventListener("touchend",touchEnd,false);
  //transition移动固定宽度,无法自适应
  // $(".nextpic").click(function(){
  // if(i==len){
  //  i=-1;
  //  $(".slider .slides").css({
  //  &#39;transition-timing-function&#39;:&#39;linear&#39;,
  //  &#39;transition-duration&#39;:&#39;800ms&#39;,
  //  &#39;transform&#39;:&#39;translateX(0px)&#39;
  //  })
  // }else{
  //  $(".slider .slides").css({
  //  &#39;transition-timing-function&#39;:&#39;linear&#39;,
  //  &#39;transition-duration&#39;:&#39;800ms&#39;,
  //  &#39;transform&#39;:&#39;translateX(-&#39;+(i+1)*width+&#39;px)&#39;
  //  })
  // }
  // i++;
  // });
  // $(".lastpic").click(function(){
  // if(i==0){
  //  i=len+1;
  //  $(".slider .slides").css({
  //  &#39;transition-timing-function&#39;:&#39;linear&#39;,
  //  &#39;transition-duration&#39;:&#39;800ms&#39;,
  //  &#39;transform&#39;:&#39;translateX(-&#39;+len*width+&#39;px)&#39;
  //  })
  // }else{
  //  $(".slider .slides").css({
  //  &#39;transition-timing-function&#39;:&#39;linear&#39;,
  //  &#39;transition-duration&#39;:&#39;800ms&#39;,
  //  &#39;transform&#39;:&#39;translateX(-&#39;+(i-1)*width+&#39;px)&#39;
  //  })
  // }
  // i--;
  // })
  
  });
 </script>
Copy after login


The above is the JS implementation introduced by the editor to realize the automatic carousel effect (adaptive screen width + mobile phone touch screen sliding). I hope it will be helpful to you. If you have any questions, please leave me a message. , the editor will reply to everyone in time. I would also like to thank you all for your support of the Script House website!

The above is the detailed content of JS realizes automatic carousel effect (adaptive screen width + mobile phone touch screen sliding)_javascript skills. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template