javascript to switch pictures by clicking a button

javascript to switch pictures by clicking a button

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:
  • Detailed explanation of custom swiper component in JavaScript
  • Detailed explanation of the difference between arrow functions and normal functions in JavaScript
  • Implementing carousel effects with JavaScript
  • Summary of various methods for JavaScript to determine whether it is an array
  • JavaScript to achieve fireworks effects (object-oriented)
  • JavaScript Canvas implements Tic-Tac-Toe game
  • Detailed discussion of the differences between loops in JavaScript
  • Summary of several common ways to abbreviate javascript code
  • 13 JavaScript one-liners that will make you look like an expert

<<:  MySQL log trigger implementation code

>>:  How to publish a locally built docker image to dockerhub

Recommend

Use CSS to achieve circular wave effect

I often see some circular wave graphics on mobile...

How to build a drag and drop plugin using vue custom directives

We all know the drag-and-drop feature of HTML5, w...

Web design and production test questions and reference answers

<br />Web Design and Production Test Part I ...

Detailed examples of Zabbix remote command execution

Table of contents one. environment two. Precautio...

Vue implements a scroll bar style

At first, I wanted to modify the browser scroll b...

How to install and deploy gitlab server on centos7

I am using centos 7 64bit system here. I have tri...

6 solutions to IDEA's inability to connect to the MySQL database

This article mainly introduces 6 solutions to the...

How to install tomcat8 in docker

1. Install tomcat8 with docker 1. Find the tomcat...

Comparison of the use of form element attributes readonly and disabled

1) Scope of application: readonly:input[type="...

VUE implements token login verification

This article example shares the specific code of ...

Summary of MySQL stored procedure permission issues

MySQL stored procedures, yes, look like very rare...

Solution to the failure of docker windows10 shared directory mounting

cause When executing the docker script, an error ...

Server stress testing concepts and methods (TPS/concurrency)

Table of contents 1 Indicators in stress testing ...

Native JS to achieve drag photo wall

This article shares with you a draggable photo wa...

Write a mysql data backup script using shell

Ideas It's actually very simple Write a shell...