How to achieve image switching animation effect in javascript (code)
The content of this article is about how to achieve the animation effect (code) of image switching in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
First of all, the rendering is roughly like this. Click the button on the left to switch pictures.
It seems quite simple, but there are still several difficulties, so I will pick this case out and analyze it carefully.
Step One: Layout
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图片切换器</title> </head> <style type="text/css"> *{ margin: 0; padding: 0; } body{ background-color: RGB(123,113,104); } #pic{ width: 300px; height: 400px; position: relative; margin: 50px auto; } #pic img{ width: 300px; height: 400px; } #pic span,#pic p{ position: absolute; width: 300px; height: 30px; line-height: 30px; text-align: center; background-color: rgba(61,50,48,0.5); } #pic span{ top: 0px; } #pic p{ bottom: 0px; } #pic ul{ position: absolute; top:0px; left: 320px; } #pic li{ list-style: none; width: 40px; height: 40px; background-color: #333; margin-bottom:10px ; } #pic li.active{ background-color: #F2F2F2; } </style> <body> <p id="pic"> <img src="/static/imghwm/default1.png" data-src="img/1.jpeg" class="lazy" / alt="How to achieve image switching animation effect in javascript (code)" > <span>- / -</span> <p>图片描述正在加载中...</p> <ul> <li class="active"></li> <li></li> <li></li> <li></li> <li></li> </ul> </p> </body> </html>
Step Two: Data and Initialization
1) Find the data
2) Initialize the page
<script type="text/javascript"> window.onload = function(){ var op = document.getElementById("pic"); var oImg = op.getElementsByTagName('img')[0]; var oSpan = op.getElementsByTagName('span')[0]; var oP = op.getElementsByTagName('p')[0]; var oUl = op.getElementsByTagName('ul')[0]; var arrSrc = ['img/1.jpeg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg']; var arrText = ['图片描述:第一张','图片描述:第二张','图片描述:第三张','图片描述:第四张','图片描述:第五张']; //一般有数组就需要一个值 var num = 0; //初始化 oSpan.innerHTML = num+1 +" / "+ arrSrc.length; oImg.src = arrSrc[num]; oP.innerHTML = arrText[num]; for(var i=0;i<arrSrc.length;i++){ oUl.innerHTML += '<li></li>'; } } </script>
Step 3: Switch the picture. So far, the function of switching pictures and displaying text has been realized, but the effect of the ul next to it has not been realized yet
var oLi = oUl.getElementsByTagName('li'); //切换图片 for(var i=0;i<arrSrc.length;i++){ //自定义属性,一一对应 oLi[i].index = i; oLi[i].onclick = function(){ var id = this.index; //通过自定义的属性设置对应的信息 oImg.src = arrSrc[id]; oP.innerHTML = arrText[id]; oSpan.innerHTML = id+1 +" / "+ arrSrc.length; } }
Step 4: Implement the class style of li addition.
Idea 1:
Clear all li styles and add them to whichever one you click.
var oldLi = 0; //初始化 oLi[oldLi].className = 'active'; //在点击事件中 //循环将class清空 for(var j=0;j<arrSrc.length;j++){ oLi[j].className = ""; } oLi[id].className = "active";
Idea 2:
Clear the previous one, currently add
Define a variable, oldLi stores the previous value clicked
The default is 0
When we click the next one, the one that will be 0 (default) will be cleared.
And record the index custom attribute oldLi of the currently clicked li = this.index;
Then set the class attribute of the current li
oLi[oldLi].className = ''; oldLi = id; this.className = 'active';
Completed;
Post the complete code and screenshots below
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图片切换器</title> </head> <style type="text/css"> *{ margin: 0; padding: 0; } body{ background-color: RGB(123,113,104); } #pic{ width: 300px; height: 400px; position: relative; margin: 50px auto; } #pic img{ width: 300px; height: 400px; border-radius:10px; } #pic span,#pic p{ position: absolute; width: 300px; height: 30px; line-height: 30px; text-align: center; background-color: rgba(61,50,48,0.5); } #pic span{ top: 0px; border-radius:10px 10px 0 0; } #pic p{ bottom: 0px; border-radius: 0 0 10px 10px; } #pic ul{ position: absolute; top:0px; left: 320px; } #pic li{ list-style: none; width: 40px; height: 40px; background-color: #333; margin-bottom:10px ; border-radius: 10px; } #pic li.active{ background-color: #F2F2F2; } </style> <script type="text/javascript"> window.onload = function(){ var oDiv = document.getElementById("pic"); var oImg = oDiv.getElementsByTagName('img')[0]; var oSpan = oDiv.getElementsByTagName('span')[0]; var oP = oDiv.getElementsByTagName('p')[0]; var oUl = oDiv.getElementsByTagName('ul')[0]; var oLi = oUl.getElementsByTagName('li'); var arrSrc = ['img/1.jpeg','img/2.jpg','img/3.jpg','img/4.jpg','img/5.jpg']; var arrText = ['图片描述:第一张','图片描述:第二张','图片描述:第三张','图片描述:第四张','图片描述:第五张']; //一般有数组就需要一个值 var num = 0; var oldLi = 0; //初始化 oSpan.innerHTML = num+1 +" / "+ arrSrc.length; oImg.src = arrSrc[num]; oP.innerHTML = arrText[num]; for(var i=0;i<arrSrc.length;i++){ oUl.innerHTML += '<li></li>'; } oLi[oldLi].className = 'active'; //切换图片 for(var i=0;i<arrSrc.length;i++){ //自定义属性,一一对应 oLi[i].index = i; oLi[i].onclick = function(){ var id = this.index; //通过自定义的属性设置对应的信息 oImg.src = arrSrc[id]; oP.innerHTML = arrText[id]; oSpan.innerHTML = id+1 +" / "+ arrSrc.length; oLi[oldLi].className = ''; oldLi = id; this.className = 'active'; } } } </script> <body> <div id="pic"> <img src="/static/imghwm/default1.png" data-src="img/1.jpeg" class="lazy" / alt="How to achieve image switching animation effect in javascript (code)" > <span>- / -</span> <p>图片描述正在加载中...</p> <ul> <!--<li class="active"></li>--> </ul> </div> </body> </html>
Related recommendations:
How to implement import and export in javascript (with code)
Animation effects of native js carousel renderings (with code)
The above is the detailed content of How to achieve image switching animation effect in javascript (code). For more information, please follow other related articles on the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

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.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
