欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

原生js滑动轮播封装

程序员文章站 2022-04-26 13:49:58
本文实例为大家分享了原生js滑动轮播的具体代码,供大家参考,具体内容如下封装滑动轮播 &l...

本文实例为大家分享了原生js滑动轮播的具体代码,供大家参考,具体内容如下

封装滑动轮播

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="x-ua-compatible" content="ie=edge">
 <title>cmm无缝轮播</title>
 <style type="text/css">
 *{margin: 0 ;padding : 0}
  #container{
   height: 470px;
   width: 590px;
   border: 1px solid red;
   position: relative;
   margin: 50px auto;
  }
  #box{
   position: absolute;
   list-style: none;
   
  }
  #box li{
   float: left;
  }
  #pages {
 width: 100%;
 height: 30px;
 background: #ccc;
 position: absolute;
 bottom: 0;
 }

 #pages i {
 width: 20px;
 height: 20px;
 display: inline-block;
 border-radius: 10px;
 background: #fff;
 margin: 5px;
 }

 #pages i.current {
 background: #f00;
 }

 #prev, #next {
 width: 45px;
 height: 100px;
 position: absolute;
 top: 0;
 bottom: 0;
 margin: auto;
 background: #ccc;
 line-height: 100px;
 text-align: center;
 font-size: 40px;
 color: #fff;
 }
 #next {
 right: 0;
 }
 </style>
</head>
<body>
 <div id="container">
 <ul id="box">
 <li><img src="images/1.jpg"></li>
 <li><img src="images/2.jpg"></li>
 <li><img src="images/3.jpg"></li>
 <li><img src="images/4.jpg"></li>
 </ul>
 <div id="pages"></div>
 <div id="prev"><</div>
 <div id="next">></div>
 </div>

 <script src="js/tools.js"></script>
 <script>
  var lis = $("li"),
   length = lis.length,
   liwidth = lis[0].clientwidth,
   currentindex = 0,
   nextindex = 1,
   timer = null,
   move = null,
   circls = null,
   durations = 2000;

 // 动态设置ul宽度
 $("#box").style.width = length * liwidth + "px";

 // 动态设置小圆点
 var html = "";
 for(var i = 0 ;i <length ;i++){
  html += "<i></i>"
 }
 $("#pages").innerhtml= html;
 circls = $("i");
 circls[0].classname = "current";

 // 切换动画
 move = function(){
  // 设置box运动终点值
  var _left = -1 * nextindex * liwidth;

  // 开始动画
  animate($("#box"),{left:_left},200)

  // 修改小圆点样式
  circls[currentindex].classname = "";
  circls[nextindex].classname = "current";

  // 修改索引
  currentindex = nextindex;
  nextindex++;
  if(nextindex >= length){
   nextindex = 0;
  }
 }


 // 自动动画
 timer = setinterval(move, durations)

 // container中鼠标移入,移出事件
 on($("#container"),"mouseenter",function(){
  clearinterval(timer);
 })
 on($("#container"),"mouseleaver",function(){
  timer = setinterval(move, durations);
 })

 // 点击小圆点,切换至对应的图片
 on($("#pages"),"click",function(e){
  e = e || event;
  var src = e.target || src.element;
  if(src.nodename === "i"){
   var _index = array.from(circls).indexof(src);
   if(_index === currentindex){
    return
   }
   nextindex = _index;
   move();
  }
 })

 // 点击翻页进行切换
 on($("#prev"),"click",function(){
  nextindex = currentindex - 1;
  if(nextindex < 0){
   nextindex = length;
  }
  move();
 })
 on($("#next"),"click",function(){
  move();
 })
 </script>
</body>
</html>
相关标签: js 滑动轮播