Home  >  Article  >  Web Front-end  >  JavaScript implements common functions of banner diagrams

JavaScript implements common functions of banner diagrams

陈政宽~
陈政宽~Original
2017-06-28 12:53:192543browse

This article mainly introduces the common functions of the original JS to implement the banner diagram in detail. It has a certain reference value. Interested friends can refer to it.

Although, using jQuery to implement the banner diagram Various effects are very simple and fast, but today I used css+js code to implement several common functions of banner images, and the effect is not bad.

This time, we mainly want to achieve the following functions:

1. Continuous switching of banner images in a loop

2. Switching of designated banner images through self-made buttons

3. Use the direction buttons to switch the left/right orientation of the banner image in sequence

4. When the onmouseover event exists in the banner image, stop the banner switching, and continue to switch when there is onmouseout

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
      #banner{
        width: 716.8px;
        height: 537.6px;
        background-color: aquamarine;
        margin: 100px auto;
        position: relative;
        font-size: 0px;    /*清除img图片间的回车符产生的间隔*/
        overflow: hidden;
      }
      #banner #bannerImg{
        width: 100%;
        position: absolute;
        top: 0px;
        left: 0px;
        white-space: nowrap;  /*使这个图片能一行显示*/
        transition:all 1s linear;
      }
      #banner #bannerImg .img{
        width: 100%;
      }
      #banner #bannerButton{
        font-size: 16px;
        color: white;
        position: absolute;
        bottom: 10px;
        left: 20px;
      }
      #banner #bannerButton .Button{
        border-radius: 9px;
        border: none;
        outline: none;
        cursor: pointer;
        background-color: #7FFFD4;
      }
      #banner #bannerButtonAside .p1{
        position: absolute;
        right: 10px;
        top: 50%;
        margin-top: -32px;
        cursor: pointer;
      }
      
      #banner #bannerButtonAside .p2{
        position: absolute;
        left: 10px;
        top: 50%;
        margin-top: -32px;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <!--实现 左右按钮,1234,自动滑动,鼠标停上显示小手不动 暂停。-->
    <section id="banner" onmouseover="changeStop()" onmouseout="changeStart()">
      
      <!--以下是我们的banner图-->
      <p id="bannerImg">
        <img class="img" src="../img/c95d7b9676ae739cccfc55457b93fe9c.jpg"/>
        <img class="img" src="../img/5f5bdebddd8f1d276aeac8af5f8fa38d.jpg"/>
        <img class="img" src="../img/5f5e5c091ecb0525fc8204f200670dd9.jpg"/>
        <img class="img" src="../img/efa11cad9094f951061ee21324277efe.jpg"/>
        <img class="img" src="../img/0b54c021bd4384c168d835dfc0908018.jpg"/>
        <img class="img" src="../img/25d10d413faca3bdd7e2d88667f4298f_看图王.jpg"/>
        <img class="img" src="../img/c95d7b9676ae739cccfc55457b93fe9c.jpg"/>  <!--第7张与第一张为同一图片,消除图片切换间断-->
      </p>
      
      <!--以下是我们左下方的banner图按钮-->
      <p id="bannerButton">
        <button class="Button" onclick="buttonChange(0)">1</button>
        <button class="Button" onclick="buttonChange(1)">2</button>
        <button class="Button" onclick="buttonChange(2)">3</button>
        <button class="Button" onclick="buttonChange(3)">4</button>
        <button class="Button" onclick="buttonChange(4)">5</button>
        <button class="Button" onclick="buttonChange(5)">6</button>
      </p>
      
      <!--以下是我们左右两个方向按钮-->
      <p id="bannerButtonAside">
        <p class="p1" onclick="asideChange(1)">
          <img src="../img/forword.png"/>
        </p>
        <p class="p2" onclick="asideChange(0)">
          <img src="../img/back.png"/>
        </p>
      </p>
    </section>
  </body>
  
  <script type="text/javascript">
    var bannerImg=document.getElementById("bannerImg");  /*取出img容器的节点*/
    var Button=document.getElementsByClassName("Button");  /*取出所有的button按钮*/
    var num=0;   /*定义全局变量num,控制banner的切换次序*/
    var aaa=0;   /*定义一个全局变量,用来取定时器函数,并在没有鼠标事件的时候清除定时器*/
      
    /*通过定时器实现banner图的每3000毫秒切换一次的效果的changeStart()函数*/
    function changeStart(){
        aaa=setInterval(function(){
        if (num<=6) {
          bannerImg.style.transition="all 1s linear";
          bannerImg.style.left=(-716.8)*(num)+"px";
          num++;
        }else{
          bannerImg.style.transition="all 0s linear";    /*消除num=0时,bannerImg移动的过渡效果*/
          num=0;
          bannerImg.style.left=(-716.8)*(num)+"px";
          
        }
        console.log("哈哈哈继续");
      },3000)
    }
    changeStart();
    
    /*以下是当鼠标悬浮在banner图上时,图片停止自动切换的changeStop()函数*/
    function changeStop(){
      clearInterval(aaa);  
      console.log("停止他");
    }
    
    /*以下是点击按钮实现对应banner图切换的change()函数*/
    function buttonChange(Num){
      num=Num+1;
      bannerImg.style.transition="all 0s linear";
      bannerImg.style.left=(-716.8)*(Num)+"px";
    }
    
    /*以下是点击左右两个按钮实现banner图切换的buttonChange()函数*/
    function asideChange(x){  /*通过传递形参x,判断往左/往右切换banner图*/
      if (num!=6&&x==1) {
        num++;
      }else if(num==6&&x==1){
        num=0;
      }else if(num!=0&&x==0){
        num--;
      }
      else if(num==0&&x==0){
        num=5;
      }
      bannerImg.style.transition="all 0s linear";
      bannerImg.style.left=(-716.8)*(num)+"px";
    }
  </script>
</html>

However, after testing by the blogger, it was found that there are certain flaws in the program. The first picture is retained longer than other pictures, and this problem exists every time the timer is restarted. For now, bloggers don’t have a simpler

way to improve it, so it’s just for reference. If you want to use it in the future, of course jQuery will save you trouble!

If there are errors, friends are welcome to point them out and we will discuss them together to improve the code!

The above is the entire content of this article. I hope it will be helpful to everyone's study. I also hope that everyone will support Script House.

The above is the detailed content of JavaScript implements common functions of banner diagrams. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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