This article example shares the specific code of javascript to realize the switching of pictures by clicking buttons for your reference. The specific content is as follows Effect picture: First build the basic structure <div id="div"> <p id="desc"></p> <!--Display the first picture by default--> <img src="img/1.jpg" alt="Load failed" style="width: 800px;height: 400px;"> <button id="pre">Previous</button> <button id="next">Next</button> </div> Next, set the display style <style> * { margin: 0; padding: 0; } #div { width: 800px; height: 420px; margin: 20px auto; background-color: rgb(243, 119, 36); text-align: center; } button:hover { cursor: pointer; } </style> The most important part of JavaScript <script> //Preload, execute the script after the page is loaded window.onload = function () { var num = document.getElementsByTagName("img")[0]; //Although there is only one img tag here, the result of the num variable is still an array //Define the image address var shuzu = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg", "img/6.jpg", "img/7.jpg", "img/8.jpg", "img/9.jpg", "img/10.jpg", "img/11.jpg", "img/12.jpg"]; //Get the button var prev = document.getElementById("pre"); var next = document.getElementById("next"); var index = 0; //Picture description var p_desc = document.getElementById("desc"); p_desc.innerHTML = "Total" + shuzu.length + "pictures" + ", current is " + (index + 1) + "picture"; //Note that the string in front is concatenated, and brackets are needed to implement addition //Click to switch pictures prev.onclick = function () { index--; //Here let it loop if (index < 0) index = shuzu.length - 1; num.src = shuzu[index]; p_desc.innerHTML = "a total of" + shuzu.length + "pictures" + ", the current one is " + (index + 1) + ""; //Note that the string in front is concatenated, and brackets are needed to implement addition} next.onclick = function () { index++; if (index > shuzu.length - 1) index = 0; num.src = shuzu[index]; p_desc.innerHTML = "a total of" + shuzu.length + "pictures" + ", the current one is " + (index + 1) + ""; //Note that the string in front is concatenated, and brackets are needed to implement addition} } 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:
|
<<: MySQL log trigger implementation code
>>: How to publish a locally built docker image to dockerhub
<meta name="viewport" content="...
This article uses an example to describe the erro...
In HTML, the <img> tag is used to define an...
1. Create a new object using the Object.create() ...
Table of contents Preface Mixin Mixin Note (dupli...
1. Generally, mariadb is installed by default in ...
Preface In MySQL, both Innodb and MyIsam use B+ t...
Preface I recently wrote a small demo. Because I ...
MySQL downloads for all platforms are available a...
Solution: Directly in the directory where you dow...
Preface As we all know, "How to vertically c...
Table of contents Preface Virtual DOM What is Vir...
This article shares the specific code for importi...
"Tik Tok" is also very popular and is s...
Table of contents 1. About JavaScript 2. JavaScri...