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 lazy" src="/static/imghwm/default1.png" data-src="../img/c95d7b9676ae739cccfc55457b93fe9c.jpg" / alt="JavaScript implements common functions of banner diagrams" > <img class="img lazy" src="/static/imghwm/default1.png" data-src="../img/5f5bdebddd8f1d276aeac8af5f8fa38d.jpg" / alt="JavaScript implements common functions of banner diagrams" > <img class="img lazy" src="/static/imghwm/default1.png" data-src="../img/5f5e5c091ecb0525fc8204f200670dd9.jpg" / alt="JavaScript implements common functions of banner diagrams" > <img class="img lazy" src="/static/imghwm/default1.png" data-src="../img/efa11cad9094f951061ee21324277efe.jpg" / alt="JavaScript implements common functions of banner diagrams" > <img class="img lazy" src="/static/imghwm/default1.png" data-src="../img/0b54c021bd4384c168d835dfc0908018.jpg" / alt="JavaScript implements common functions of banner diagrams" > <img class="img lazy" src="/static/imghwm/default1.png" data-src="../img/25d10d413faca3bdd7e2d88667f4298f_看图王.jpg" / alt="JavaScript implements common functions of banner diagrams" > <img class="img lazy" src="/static/imghwm/default1.png" data-src="../img/c95d7b9676ae739cccfc55457b93fe9c.jpg" / alt="JavaScript implements common functions of banner diagrams" > <!--第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="/static/imghwm/default1.png" data-src="../img/forword.png" class="lazy" / alt="JavaScript implements common functions of banner diagrams" > </p> <p class="p2" onclick="asideChange(0)"> <img src="/static/imghwm/default1.png" data-src="../img/back.png" class="lazy" / alt="JavaScript implements common functions of banner diagrams" > </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!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
