Home Web Front-end JS Tutorial JavaScript implements common functions of banner diagrams

JavaScript implements common functions of banner diagrams

Jun 28, 2017 pm 12:53 PM
banner javascript js

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>
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1254
29
C# Tutorial
1228
24
How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles