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

Details of function nesting and closures in js

Table of contents 1. Scope 2. Function return val...

How to reduce the root directory of XFS partition format in Linux

Table of contents Preface System environment Curr...

Summary of Docker configuration container location and tips

Tips for using Docker 1. Clean up all stopped doc...

HTML code example: detailed explanation of hyperlinks

Hyperlinks are the most frequently used HTML elem...

Detailed example code of mysql batch insert loop

background A few days ago, when I was doing pagin...

Example of how to set up a Linux system to automatically run a script at startup

Preface Hello everyone, I am Liang Xu. At work, w...

Examples of correct use of interface and type methods in TypeScript

Table of contents Preface interface type Appendix...

Analysis of permissions required to run docker

Running Docker requires root privileges. To solve...

Practical record of vue using echarts word cloud chart

echarts word cloud is an extension of echarts htt...

Index in MySQL

Preface Let's get straight to the point. The ...

MySQL 5.6.22 installation and configuration method graphic tutorial

This tutorial shares the specific code of MySQL5....

How to delete extra kernels in Ubuntu

Step 1: View the current kernel rew $ uname -a Li...

Share 12 commonly used Loaders in Webpack (Summary)

Table of contents Preface style-loader css-loader...