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 Web Components customElements O...
<br />The most common mistake made by many w...
Table of contents introduction Cookie What are Co...
Installation Steps 1. Install Redis Download the ...
<Head>……</head> indicates the file he...
Error message: ERROR 2002: Can't connect to l...
Linux finds a specific program where is The where...
method: Take less in the actual project as an exa...
This article shares the specific code for React t...
Table of contents Business Logic Data table struc...
You can install Docker and perform simple operati...
There are two common loading icons on the web, on...
1. What is positioning? The position attribute in...
1. The difference between forward proxy and rever...
MyISAM and InnoDB are the most common storage eng...