用原生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>Document</title>
  <style type="text/css">
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    #mainDiv {
      width: 1200px;
      height: 600px;

      margin: 0 auto;
      overflow: hidden;
      position: relative;
    }

    #mainDiv ul {
      height: 600px;
      position: absolute;
      z-index: 100;
      top: 0;
      left: 0;
      transition: all 0.4s;
    }

    #mainDiv ul li {
      float: left;
      list-style: none;
    }

    #mainDiv ul li img {
      width: 1200px;
      height: 600px;
    }

    button {
      width: 50px;
      height: 50px;
      background-color: rgba(0, 0, 0, 0.5);
      position: absolute;
      z-index: 10000001;
      font-size: 40px;
      outline: none;
    }

    #btnPre {
      top: 300px;
      left: 0;
    }

    #btnForWard {
      top: 300px;
      right: 0;
    }
  </style>
</head>

<body>


  <div id="mainDiv">
    <ul>
      <li><img src="./images/pikachu.jpg"></li>
      <li><img src="./images/pokemon1.jpg"></li>
      <li><img src="./images/pokemon2.jpg"></li>
      <li><img src="./images/pokemonGreen.jpg"></li>

    </ul>

    <button id="btnPre"><</button>
    <button id="btnForWard">></button>



  </div>

  <script type="text/javascript">
    var theAllLis = document.querySelectorAll("#mainDiv ul li");

    var theUl = document.querySelector("#mainDiv ul");

    var theCurrentIndex = 0; //当前的图片的序号


    //给 ul设置宽度 ,有多少个li就设百分之多少

    theUl.style.width = theAllLis.length * 100 + "%";

    //后滚动设置

    document.querySelector("#btnPre").onclick = function () {

      theCurrentIndex--;

      if (theCurrentIndex == -1) {
        theCurrentIndex = theAllLis.length - 1;
      }



      theUl.style.marginLeft = -1200 * theCurrentIndex + "px";


    }
    document.querySelector("#btnForWard").onclick = function () {
      RollingNext();

    }

    ////////设置自动滚动
    var theInterTime = setInterval(function () {

      RollingNext();

    }, 2000);

    /////鼠标放到div上的时候图片停止自动滚动



    document.querySelector("#mainDiv").onmouseover = function () {

      clearInterval(theInterTime);

    }
    /////鼠标离开div的时候图片开始滚动
    document.querySelector("#mainDiv").onmouseout = function () {

      theInterTime = setInterval(function () {

        RollingNext();

      }, 2000);

    }


    function RollingNext() {

      theCurrentIndex++;

      if (theCurrentIndex == theAllLis.length) {
        theCurrentIndex = 0;
      }



      theUl.style.marginLeft = -1200 * theCurrentIndex + "px";


    }
  </script>
</body>

</html>

主要的思路是用ul 和 li, 然后让li 浮动起来,这样在让图片左滚动或者右滚动的时候,就改变ul的margin就好了, 如果想要动画效果就是加上transition

自动轮播是用setInterval的方法实现的,将右滚动的函数放在里面,设置为每隔2000毫秒函数运行一次。

鼠标放上去的时候会clearInterval ,这样图片不会乱动, 鼠标移开后就把原先的interval再设置一遍.

还有就是图片的话就放在两个li标签中间, 想多加几张图片也无所谓,因为ul的宽度也是根据li的多少设置的,就是注意路径要放对

猜你喜欢

转载自blog.csdn.net/raymondlin9292/article/details/82528749