Clicking to switch pictures is very common in life. Today's exercise happens to be to switch pictures. For your reference: The HTML code is as follows: <div class="img"> <img src="images/1.jpg" id="myImg" class="myImg" alt="Here is 1.jpg"> <p> <input type="button" id="pre" class="btn" value="Previous"> <input type="button" id="next" class="btn" value="Next"> </p> </div> The CSS code is as follows: *{ margin: 0; padding: 0; } img{ body:none; } button{ outline: none; vertical-align: middle; } .img{ width: 100%; margin-left: auto; margin-right: auto; margin-top: 20px; text-align: center; } .myImg{ width: 500px; height: 300px; } p{ text-align: center; } p .btn{ width: 100px; height: 30px; background: #306bbf; color: #fff; margin-top: 20px; margin-bottom: 20px; } javascript part: //Find the tag let myImg = document.getElementById("myImg"); let pre = document.getElementById("pre"); let next = document.getElementById("next"); //Create an array to store images let arrImg = ["images/1.jpg", "images/1-1.png", "images/3.jpg" ]; //Array index subscript let index=0; //Define event function function preImg(event){ index--; //Realize loop switching if (index<0) { index=arrImg.length-1; } myImg.src = arrImg[index]; } function nextImg(event){ index++; //Realize loop switching if (index>arrImg.length-1) { index=0; } myImg.src = arrImg[index]; } pre.addEventListener('click',preImg); next.addEventListener('click',nextImg); The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: What to do if you forget the initial password when installing MySQL on Mac
>>: Installation of Docker CE on Ubuntu
Table of contents Dynamically change themes The f...
Syn attack is the most common and most easily exp...
I have created a goods table here. Let's take...
Recently, there is a requirement for uploading pi...
Nowadays, cross-platform development technology i...
Table of contents Preface 1. Current gcc version ...
Table of contents Overview 1. Define store.js 2. ...
Table of contents Vue CLI builds a Vue project Vu...
Copy the following code to the code area of Drea...
This article shares the specific code of js+canva...
This article shares with you a draggable photo wa...
Forms in HTML can be used to collect various type...
1. Import csv file Use the following command: 1.m...
Table of contents 1. Instructions 2. Modifiers 3....
Table of contents Preface What is data binding? T...