Introduction to how to use JavaScript to create image switching
This article mainly introduces JavaScript to achieve image switching effect and the application of custom attributes in detail. It has certain reference value. Interested friends can refer to it.
The examples in this article are for everyone. Shared JavaScript to achieve image switching effect and application of custom attributes for your reference. The specific content is as follows
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图片切换实例</title> <style> body{ background-color: #A9A9A9; margin:0px; } ul{ padding: 0; margin: 0; } li{ list-style: none; } #pic{ width:670px; height: 420px; position:relative; margin: 0 auto;/*整个p放到页面中间的位置*/ background:url(img/loading.png) no-repeat center; background-color:#fff; } #pic img{ width:670px; height: 420px; } #pic ul{ position: absolute; top: 0px; right: -50px; } #pic li{ width:40px; height:40px; margin-bottom: 4px; background:#666; } #pic .active{ background: cadetblue; } #pic span{ top:0px; } #pic p{ bottom:0px; margin:0; } #pic p,#pic span{ width: 670px; height: 30px; line-height: 30px; text-align: center; position:absolute; left:0px; color:#fff; background-color:#333; } </style> <script> window.onload = function(){ var op = document.getElementById("pic"); var oImg = document.getElementsByTagName("img")[0];//有tag标签的地方就得有数组[0],否则不提示错误,但却会加载不出来需要的内容。 var oP = document.getElementsByTagName("p")[0]; var oNum = document.getElementsByTagName("span")[0]; var oUl = document.getElementsByTagName("ul")[0]; var aLi = oUl.getElementsByTagName("li");//通过父标签找到的子标签,这里不能加数组[0] var arrUrl = ["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg","img/5.png"]; var arrText = ["中原工学院图书馆","中工宿舍楼","玫瑰花","小猫咪","绿色盆栽"]; for(var i=0;i<arrUrl.length;i++){ oUl.innerHTML += "<li></li>";//添加用“+=”,该写用“=” } //初始化 //有数组的地方,大部分都有一个数字在静静的等待.数组配合数字以便找到需要的内容。 var num = 0; oImg.src = arrUrl[num]; oP.innerHTML = arrText[num]; oNum.innerHTML = 1+num+'/'+arrUrl.length; //在ul中添加li,根据数组的长度,为ul添加li的数量 aLi[num].className = "active";//为标签添加class属性,需要用到className for(i=0;i<aLi.length;i++){ aLi[i].index = i;//索引值,需要添加对应关系,就要想到添加索引值 //定义的有数组,就不能忘记加[0] aLi[i].onclick = function(){ //点击按钮,找到与之对应的图片 oImg.src = arrUrl[this.index]; oP.innerHTML = arrText[this.index]; oNum.innerHTML = 1+this.index+'/'+arrUrl.length; //添加对应的点击时,li的图标发生变化,两种思路 //思路1:清空当前所有active样式,为当前添加此class属性(扩展性好,但是运行速度可能不好) for(var i=0; i<aLi.length; i++){ aLi[i].className = ""; } this.className = "active"; //思路2:清空前一个点击li的样式,为当前添加class属性(定点清除) } } } </script> </head> <body> <p id="pic" > <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/007/a836e756f6223a8f25b9654d54879464-0.jpg?x-oss-process=image/resize,p_40" class="lazy" / alt="Introduction to how to use JavaScript to create image switching" > <span>数量正在加载中......</span> <p>文字说明正在加载中......</p> <ul> </ul> </p> </body> </html>
The JavaScript code snippet can be simplified as follows:
<script> window.onload = function(){ var op = document.getElementById("pic"); var oImg = document.getElementsByTagName("img")[0]; var oP = document.getElementsByTagName("p")[0]; var oNum = document.getElementsByTagName("span")[0]; var oUl = document.getElementsByTagName("ul")[0]; var aLi = oUl.getElementsByTagName("li"); var arrUrl = ["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg","img/5.png"]; var arrText = ["中原工学院图书馆","中工宿舍楼","玫瑰花","小猫咪","绿色盆栽"]; for(var i=0;i<arrUrl.length;i++){ oUl.innerHTML += "<li></li>"; } //初始化 var num = 0; function fnTab(){ oImg.src = arrUrl[num]; oP.innerHTML = arrText[num]; oNum.innerHTML = 1+num+'/'+arrUrl.length; aLi[num].className = ""; } fnTab(); for(i=0;i<aLi.length;i++){ aLi[i].index = i; aLi[i].onclick = function(){ num = this.index; fnTab(); } aLi[num].className = "active"; } } } </script>
Rendering:
The above is the detailed content of Introduction to how to use JavaScript to create image switching. For more information, please follow other related articles on the PHP Chinese website!

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.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of 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

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

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.

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
