The editor below will bring you an example of using JS to implement image carousel (front and back, end to end). The editor thinks it’s pretty good, so now I’ll share the js source code with you and give it as a reference. If you are interested in js, please follow the editor to take a look.
I have been to various interviews recently, and finally I was asked this question. My mind was confused. I didn’t know how to connect end to end at the time. I will study it after I come back. After a while, I finally got it. Without further ado, let’s just look at the code.
The code refers to someone who has already written the image carousel function (thank you again), but there is no end-to-end function. On this basis, I added the end-to-end function.
The effect is as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>图片轮播</title> <style type="text/css"> body,p,ul,li,a,img{margin: 0;padding: 0;} ul,li{list-style: none;} a{text-decoration: none;} #wrapper{ position: relative; margin: 30px auto; /* 上下边距30px,水平居中 */ width: 400px; height: 200px; } #banner{ position:relative; width: 400px; height: 200px; overflow: hidden; } .imgList{ position:relative; width:2000px; height:200px; z-index: 10; overflow: hidden; } .imgList li{float:left;display: inline;} #prev, #next{ position: absolute; top:80px; z-index: 20; cursor: pointer; opacity: 0.2; filter:alpha(opacity=20); } #prev{left: 10px;} #next{right: 10px;} #prev:hover, #next:hover{opacity: 0.5;filter:alpha(opacity=50);} </style> </head> <body> <p id="wrapper"><!-- 最外层部分 --> <p id="banner"><!-- 轮播部分 --> <ul class="imgList"><!-- 图片部分 --> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/1.jpg" class="lazy" style="max-width:90%" height="200px" alt="1"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/2.jpg" class="lazy" style="max-width:90%" height="200px" alt="2"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/3.jpg" class="lazy" style="max-width:90%" height="200px" alt="3"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/4.jpg" class="lazy" style="max-width:90%" height="200px" alt="4"></a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="/static/imghwm/default1.png" data-src="./img/5.jpg" class="lazy" style="max-width:90%" height="200px" alt="5"></a></li> </ul> <img src="/static/imghwm/default1.png" data-src="./img/prev.png" class="lazy" style="max-width:90%" height="40px" id="prev" alt="Example of using JS to implement image carousel" > <img src="/static/imghwm/default1.png" data-src="./img/next.png" class="lazy" style="max-width:90%" height="40px" id="next" alt="Example of using JS to implement image carousel" > </p> </p> <script type="text/javascript" src="./js/jquery-3.2.1.min.js"></script> <script type="text/javascript"> var curIndex = 0, //当前index imgLen = $(".imgList li").length; //图片总数 $(".imgList").css("width", (imgLen+1)*400+"px"); // 定时器自动变换3秒每次 var autoChange = setInterval(function(){ if(curIndex < imgLen-1){ curIndex ++; }else{ curIndex = 0; } //调用变换处理函数 changeTo(curIndex); },3000); //左箭头滑入滑出事件处理 $("#prev").hover(function(){ //滑入清除定时器 clearInterval(autoChange); }, function(){ //滑出则重置定时器 autoChangeAgain(); }); //左箭头点击处理 $("#prev").click(function(){ //根据curIndex进行上一个图片处理 // curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1); if (curIndex == 0) { var element = document.createElement("li"); element.innerHTML = $(".imgList li")[imgLen - 1].innerHTML; // $(".imgList li")[imgLen - 1].remove(); $(".imgList").prepend(element); $(".imgList").css("left", -1*400+"px"); changeTo(curIndex); curIndex = -1; } else if (curIndex == -1) { $(".imgList").css("left", -(imgLen-1)*400+"px"); curIndex = imgLen-2; $(".imgList li")[0].remove(); changeTo(curIndex); } else { --curIndex; changeTo(curIndex); } }); //右箭头滑入滑出事件处理 $("#next").hover(function(){ //滑入清除定时器 clearInterval(autoChange); }, function(){ //滑出则重置定时器 autoChangeAgain(); }); //右箭头点击处理 $("#next").click(function(){ // curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0; console.log(imgLen); if (curIndex == imgLen-1) { var element = document.createElement("li"); element.innerHTML = $(".imgList li")[0].innerHTML; // $(".imgList li")[0].remove(); $(".imgList").append(element); ++curIndex; } else if (curIndex == imgLen) { curIndex = 0; $(".imgList").css("left", "0px"); $(".imgList li")[imgLen].remove(); curIndex++; } else { ++curIndex; } changeTo(curIndex); }); //清除定时器时候的重置定时器--封装 function autoChangeAgain(){ autoChange = setInterval(function(){ if(curIndex < imgLen-1){ curIndex ++; }else{ curIndex = 0; } //调用变换处理函数 changeTo(curIndex); },3000); } function changeTo(num){ var goLeft = num * 400; $(".imgList").animate({left: "-" + goLeft + "px"},500); } </script> </body> </html>
The above example of using JS to implement image carousel is a small I have compiled all the content to share with you, I hope it can give you a reference! !
Related recommendations:
Three implementation methods of defining functions in JavaScript
Detailed explanation of multiple inheritance examples in JavaScript
The most complete JavaScript learning summary
The above is the detailed content of Example of using JS to implement image carousel. For more information, please follow other related articles on the PHP Chinese website!

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


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

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

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
