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

Some useful meta setting methods (must read)

<meta name="viewport" content="...

Detailed explanation of error handling examples in MySQL stored procedures

This article uses an example to describe the erro...

Detailed explanation of the usage of image tags in HTML

In HTML, the <img> tag is used to define an...

Detailed explanation of Object.create instance usage in js

1. Create a new object using the Object.create() ...

Let's talk about Vue's mixin and inheritance in detail

Table of contents Preface Mixin Mixin Note (dupli...

Detailed steps to install MYSQL8.0 on CentOS7.6

1. Generally, mariadb is installed by default in ...

What are the benefits of using B+ tree as index structure in MySQL?

Preface In MySQL, both Innodb and MyIsam use B+ t...

Nginx proxy axios request and precautions

Preface I recently wrote a small demo. Because I ...

Detailed tutorial for installing MySQL on Linux

MySQL downloads for all platforms are available a...

Solve the problem of installing Theano on Ubuntu 19

Solution: Directly in the directory where you dow...

How to use jsx syntax correctly in vue

Table of contents Preface Virtual DOM What is Vir...

Import csv file into mysql using navicat

This article shares the specific code for importi...

CSS3 analysis of the steps for making Douyin LOGO

"Tik Tok" is also very popular and is s...

A brief discussion on macrotasks and microtasks in js

Table of contents 1. About JavaScript 2. JavaScri...