JavaScript implements image carousel effect_javascript skills
The example in this article introduces the detailed code of javascript to achieve the image carousel effect, and shares it with everyone for your reference. The specific content is as follows
Rendering:
Specific code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } .wrap{ width: 510px; margin: 0 auto; overflow: hidden; position: relative; } .caktye{ width: 9999px; overflow: hidden; position: relative; left: 0; } .main{ text-align: center; position: absolute; bottom: 10px; /*border: 1px solid red;*/ left: 35%; } .main button{ width: 25px; height: 25px; text-align: center; line-height: 25px; border-radius: 25px; display: inline-block; border: none; } .active{ background-color: yellow; } .caktye a{ float: left; } .caktye img { display: block; width:510px; } #prevBtn{ display: block; position: absolute; left: 5px; bottom: 45%; width: 15px; height: 15px; /*overflow: hidden;*/ border-left: 5px solid ; border-top: 5px solid ; border-color: rgba(255,255,255,0.5); -webkit-transform: rotate(45deg); transform: rotate(-45deg); } #nextBtn{ display: block; position: absolute; right: 5px; bottom: 45%; width: 15px; height: 15px; /*overflow: hidden;*/ border-right: 5px solid; border-top: 5px solid; -webkit-transform: rotate(45deg); transform: rotate(45deg); border-color: rgba(255,255,255,0.5); /*background-color: rgba(255,255,255,0.8);*/ } </style> </head> <body> <div class="wrap"> <div class="caktye" id="inner"> <a href="###"><img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" alt="JavaScript implements image carousel effect_javascript skills" ></a> <a href="###"><img src="/static/imghwm/default1.png" data-src="img/2.jpg" class="lazy" alt="JavaScript implements image carousel effect_javascript skills" ></a> <a href="###"><img src="/static/imghwm/default1.png" data-src="img/3.jpg" class="lazy" alt="JavaScript implements image carousel effect_javascript skills" ></a> <a href="###"><img src="/static/imghwm/default1.png" data-src="img/4.jpg" class="lazy" alt="JavaScript implements image carousel effect_javascript skills" ></a> <a href="###"><img src="/static/imghwm/default1.png" data-src="img/5.jpg" class="lazy" alt="JavaScript implements image carousel effect_javascript skills" ></a> <a href="###"><img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" alt="JavaScript implements image carousel effect_javascript skills" ></a> </div> <div class="main"> <button class="active">1</button> <button>2</button> <button>3</button> <button>4</button> <button>5</button> </div> <div> <a href="###" id="prevBtn"></a> <a href="###" id="nextBtn"></a> </div> </div> <script type="text/javascript"> var innerList = document.getElementById("inner"); var btnList = document.getElementsByTagName("button"); var perWidth = inner.children[0].offsetWidth; var prevBtn = document.getElementById("prevBtn"); var nextBtn = document.getElementById("nextBtn"); // var imgList = inner.getElementsByTagName("img"); // var perWidth = imgList[0].offsetWidth; var timer = 0; var timer1 = 0; var index =0; var runFlag = true; //设置一个动画是否走完的标志位 for(var i = 0; i < btnList.length; i++) { btnList[i].index = i; btnList[i].onclick = function() { index = this.index; tab(); } } function tab() { var start = inner.offsetLeft; var end = - perWidth * index; var change = end - start; var t = 0; var maxT = 30; clearInterval(timer1); timer1 = setInterval(function() { t++; if(t >= maxT) { clearInterval(timer1); // alert("停下来了"); runFlag = true; } inner.style.left = change/maxT * t + start + "px"; if(index == btnList.length && t >= maxT) { inner.style.left = 0; } },30) // inner.style.left = - perWidth * index + "px"; for(var j = 0; j < btnList.length; j++) { btnList[j].className = ""; } if(index >= btnList.length) { btnList[0].className = "active"; }else { btnList[index].className = "active"; } } function xunhuan(){ index++; if(index>btnList.length){ index=1; } // inner.style.left = - perWidth * index + "px"; tab(); } var timer =setInterval(xunhuan,5000); inner.onmouseover =function(){ clearInterval(timer); } inner.onmouseout =function(){ timer = setInterval(xunhuan,5000); } function next() { index++; if(index > btnList.length) { index = 1; } tab(); } function prev() { index--; if(index < 0 ) { index = btnList.length - 1; inner.style.left = - btnList.length * perWidth + "px"; console.log(index); } tab(); } //下一张 nextBtn.onclick = function() { clearInterval(timer); if(runFlag) { next(); } runFlag = false; } //上一张 prevBtn.onclick = function() { clearInterval(timer); if(runFlag) { prev(); } runFlag = false; } </script> </body> </html>
I hope this article will be helpful to everyone learning JavaScript programming.

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

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

Notepad++7.3.1
Easy-to-use and free code editor

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
