javascript to switch by clicking on the picture

javascript to switch by clicking on the picture

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:
  • The simplest js picture switching effect implementation code
  • A simple image switching effect implemented with html+css+js
  • Pure js to achieve background image switching effect code
  • Simple js code to switch pictures by clicking arrows
  • CSS picture switching effect code [without js]
  • Pure js without flash imitating Sohu Women's Channel FLASH picture switching effect code
  • JavaScript to achieve the slide effect source code of picture switching
  • JavaScript to achieve image switching effect
  • js mouse click picture switching effect code sharing
  • JS realizes the picture switching effect

<<:  What to do if you forget the initial password when installing MySQL on Mac

>>:  Installation of Docker CE on Ubuntu

Recommend

Vue implements multiple ideas for theme switching

Table of contents Dynamically change themes The f...

Solve the problem of Syn Flooding in MySQL database

Syn attack is the most common and most easily exp...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...

Linux super detailed gcc upgrade process

Table of contents Preface 1. Current gcc version ...

How to understand Vue's simple state management store mode

Table of contents Overview 1. Define store.js 2. ...

Detailed explanation of desktop application using Vue3 and Electron

Table of contents Vue CLI builds a Vue project Vu...

Example code for using HTML ul and li tags to display images

Copy the following code to the code area of ​​Drea...

js+canvas realizes code rain effect

This article shares the specific code of js+canva...

Native JS to achieve drag photo wall

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

HTML form tag usage learning tutorial

Forms in HTML can be used to collect various type...

Tips for importing csv, excel or sql files into MySQL

1. Import csv file Use the following command: 1.m...

Detailed explanation of Vue's sync modifier

Table of contents 1. Instructions 2. Modifiers 3....

Detailed explanation of Angular data binding and its implementation

Table of contents Preface What is data binding? T...