Home Web Front-end JS Tutorial How to achieve image switching animation effect in javascript (code)

How to achieve image switching animation effect in javascript (code)

Aug 23, 2018 am 11:26 AM
Picture switching

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="img/1.jpeg"/>
			<span>- / -</span>
			<p>图片描述正在加载中...</p>
			<ul>
				<li class="active"></li>
				<li></li>
				<li></li>
				<li></li>
				<li></li>
				
			</ul>
		</p>
	</body>
</html>
Copy after login

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(&#39;img&#39;)[0];
			var oSpan = op.getElementsByTagName(&#39;span&#39;)[0];
			var oP = op.getElementsByTagName(&#39;p&#39;)[0];
			var oUl = op.getElementsByTagName(&#39;ul&#39;)[0];
			
			var arrSrc = [&#39;img/1.jpeg&#39;,&#39;img/2.jpg&#39;,&#39;img/3.jpg&#39;,&#39;img/4.jpg&#39;,&#39;img/5.jpg&#39;];
			var arrText = [&#39;图片描述:第一张&#39;,&#39;图片描述:第二张&#39;,&#39;图片描述:第三张&#39;,&#39;图片描述:第四张&#39;,&#39;图片描述:第五张&#39;];
			
			//一般有数组就需要一个值
			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 += &#39;<li></li>&#39;;
			}
			
		}
	</script>
Copy after login

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(&#39;li&#39;);

//切换图片
			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;
				}
			}
Copy after login

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 = &#39;active&#39;;

//在点击事件中
//循环将class清空
			for(var j=0;j<arrSrc.length;j++){
				oLi[j].className = "";					
			}
			oLi[id].className = "active";
Copy after login

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 = &#39;&#39;;
					oldLi = id;
					this.className = &#39;active&#39;;
Copy after login

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(&#39;img&#39;)[0];
            var oSpan = oDiv.getElementsByTagName(&#39;span&#39;)[0];
            var oP = oDiv.getElementsByTagName(&#39;p&#39;)[0];
            var oUl = oDiv.getElementsByTagName(&#39;ul&#39;)[0];
            var oLi = oUl.getElementsByTagName(&#39;li&#39;);
            
            var arrSrc = [&#39;img/1.jpeg&#39;,&#39;img/2.jpg&#39;,&#39;img/3.jpg&#39;,&#39;img/4.jpg&#39;,&#39;img/5.jpg&#39;];
            var arrText = [&#39;图片描述:第一张&#39;,&#39;图片描述:第二张&#39;,&#39;图片描述:第三张&#39;,&#39;图片描述:第四张&#39;,&#39;图片描述:第五张&#39;];
            
            //一般有数组就需要一个值
            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 += &#39;<li></li>&#39;;
            }
            oLi[oldLi].className = &#39;active&#39;;
            
            
            //切换图片
            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 = &#39;&#39;;
                    oldLi = id;
                    this.className = &#39;active&#39;;
                }
            }
            
        }
    </script>
    <body>
        <div id="pic">
            <img src="img/1.jpeg"/>
            <span>- / -</span>
            <p>图片描述正在加载中...</p>
            <ul>
                <!--<li class="active"></li>-->
            </ul>
        </div>
    </body>
</html>
Copy after login

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!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

How to achieve seamless left and right sliding switching effect of images in JavaScript? How to achieve seamless left and right sliding switching effect of images in JavaScript? Oct 19, 2023 am 08:56 AM

How to achieve seamless left and right sliding switching effect of images with JavaScript? With the development of the Internet, images are often used as important elements of the page in web design. The switching effect of pictures plays an important impact on the beauty and interactivity of the page. In this article, we will explore how to use JavaScript to achieve a seamless left-right sliding switching effect of images, and attach specific code examples. To achieve the seamless left and right sliding switching effect of pictures, you first need to do the following: Create a picture container and use

Methods and techniques on how to achieve image carousel effects through pure CSS Methods and techniques on how to achieve image carousel effects through pure CSS Oct 18, 2023 am 08:27 AM

Methods and techniques on how to achieve image carousel effects through pure CSS. In modern web design, image carousel effects are often used to display multiple images or advertisements in turn. There are many ways to achieve an image carousel effect, one of the common ways is to use CSS animation. This article will introduce methods and techniques on how to achieve image carousel effects through pure CSS, and provide specific code examples. 1. HTML structure First, the image elements for carousel need to be prepared in HTML. Here is a simple HTML structure example: &l

How to achieve image switching and carousel effects through Vue? How to achieve image switching and carousel effects through Vue? Aug 18, 2023 pm 04:57 PM

How to achieve image switching and carousel effects through Vue? Vue is a JavaScript framework for building user interfaces that provides an elegant and efficient way to handle data and interaction logic in web applications. One of the many great features of Vue is that it can easily handle image switching and carousel effects. In this article, we will introduce how to use Vue to achieve these effects. First, we need to prepare some basic HTML structures and styles to display images. We can use &lt;i

How to use HTML, CSS and jQuery to create a responsive image switching effect How to use HTML, CSS and jQuery to create a responsive image switching effect Oct 24, 2023 am 08:01 AM

Creating responsive image switching effects is one of the common tasks in front-end development. In this article, we will use HTML, CSS and jQuery to achieve this effect. Below are detailed steps and specific code examples. HTML structure First, we need to create the HTML structure required for the image switching effect. You can use the following code example to create a simple HTML structure. &lt;divclass="slider-container"&gt;

How to use JavaScript to achieve the gradient effect of image switching? How to use JavaScript to achieve the gradient effect of image switching? Oct 21, 2023 am 09:33 AM

How to use JavaScript to achieve the gradient effect of image switching? With the development of the Internet, website design pays more and more attention to user experience. Image switching is one of the common interactive effects on websites. Gradient switching of images can better attract users' attention. This article will introduce how to use JavaScript to achieve the gradient effect of image switching, and provide specific code examples. Before we start, we need to prepare some image resources. Suppose we have three pictures, namely "image1.jpg", "

How to achieve seamless left and right sliding switching effects of images with JavaScript while adding zoom and fade animations? How to achieve seamless left and right sliding switching effects of images with JavaScript while adding zoom and fade animations? Oct 25, 2023 am 09:39 AM

How can JavaScript achieve a seamless left and right sliding switching effect of an image while adding zoom and fade animations? In website development, the sliding switching effect of images is a very common requirement. Here we will introduce how to use JavaScript to achieve a left and right seamless sliding switching effect, while adding zoom and fade animations. This article will provide detailed code examples so that you can easily achieve this effect. First, we need to prepare a container in HTML to place images, and set the container's

How to use Layui to achieve image switching carousel effect How to use Layui to achieve image switching carousel effect Oct 26, 2023 am 11:52 AM

How to use Layui to achieve the image switching carousel effect requires specific code sample title: Detailed explanation of using Layui to achieve the image switching carousel effect Introduction: In modern web design, the image switching carousel effect has become one of the common elements. Using image carousels can make web pages more dynamic and attractive. This article will use Layui as the basis to introduce how to achieve the image switching carousel effect, and give specific code examples. 1. Introduction to Layui carousel component Layui is a classic front-end UI framework, which includes

How to use Layui to achieve image switching and stretching effects How to use Layui to achieve image switching and stretching effects Oct 25, 2023 am 08:13 AM

How to use Layui to achieve image switching and stretching effects. In recent years, with the rapid development of Web front-end technology, more and more frameworks and libraries are used to beautify and enhance the functions of web pages. Among them, Layui is a very popular front-end framework that provides rich UI components and easy-to-use function extensions. This article will introduce how to use Layui to achieve image switching and stretching effects, and give specific code examples. 1. Implementation of image switching effect HTML structure First, we need to prepare some HTML structures for

See all articles