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=""/> <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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Xiaomi 14Ultra is one of the most popular Xiaomi models this year. Xiaomi 14Ultra not only upgrades the processor and various configurations, but also brings many new functional applications to users. This can be seen from the sales of Xiaomi 14Ultra mobile phones. It is very popular, but there are some commonly used functions that you may not know yet. So how does Xiaomi 14Ultra switch between 4g and 5g? Let me introduce the specific content to you below! How to switch between 4g and 5g on Xiaomi 14Ultra? 1. Open the settings menu of your phone. 2. Find and select the "Network" and "Mobile Network" options in the settings menu. 3. In the mobile network settings, you will see the "Preferred network type" option. 4. Click or select this option and you will see

How to convert Win11 Home Edition to Win11 Professional Edition? In Win11 system, it is divided into Home Edition, Professional Edition, Enterprise Edition, etc., and most Win11 notebooks are pre-installed with Win11 Home Edition system. Today, the editor will show you the steps to switch from win11 home version to professional version! 1. First, right-click on this computer on the win11 desktop and properties. 2. Click Change Product Key or Upgrade Windows. 3. Then click Change Product Key after entering. 4. Enter the activation key: 8G7XN-V7YWC-W8RPC-V73KB-YWRDB and select Next. 5. Then it will prompt success, so you can upgrade win11 home version to win11 professional version.

Many friends may not be used to the win system when they first come into contact with it. There are dual systems in the computer. At this time, you can actually switch between the two systems. Let's take a look at the detailed steps for switching between the two systems. How to switch between two systems in win10 system 1. Shortcut key switching 1. Press the "win" + "R" keys to open Run 2. Enter "msconfig" in the run box and click "OK" 3. In the open "System Configuration" In the interface, select the system you need and click "Set as Default". After completion, "Restart" can complete the switch. Method 2. Select switch when booting 1. When you have dual systems, a selection operation interface will appear when booting. You can use the keyboard " Up and down keys to select the system

How to switch between Apple dual systems when starting up Apple computers are powerful devices. In addition to their own macOS operating system, you can also choose to install other operating systems, such as Windows, to achieve dual system switching. So how do we switch between the two systems when booting? This article will introduce to you how to switch between dual systems on Apple computers. First of all, before installing dual systems, we need to confirm whether our Apple computer supports dual system switching. Generally speaking, Apple computers are based on

In the application of excel software, we are accustomed to using shortcut keys to make some operations easier and faster. Sometimes there is related data between multiple tables in excel. When we view it, we have to constantly switch between tasks. If there is a faster switching method, it will save a lot of wasted time on switching, which will greatly help improve work efficiency. What method can be used to complete quick switching? To address this issue, the editor will talk about it today The content is: How to use the shortcut keys for switching workbooks in Excel. 1. First, you can see multiple workbooks at the bottom of the open excel table. You need to quickly switch between different workbooks, as shown in the figure below. 2. Then press the Ctrl key on the keyboard without moving, and select the job to the right if you need to

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

In daily life, we often encounter the problem of full-width and half-width, but few people may have a deep understanding of their meaning and difference. Full-width and half-width are actually concepts of character encoding methods, and they have special applications in computer input, editing, typesetting, etc. This article will delve into the differences between full-width and half-width, switching techniques, and real-life applications. First of all, the definitions of full-width and half-width in the field of Chinese characters are: a full-width character occupies one character position, and a half-width character occupies half a character position. In a computer, pass
