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

Some points on using standard HTML codes in web page creation

<br />The most common mistake made by many w...

Detailed explanation of JS browser storage

Table of contents introduction Cookie What are Co...

How to deploy redis in linux environment and install it in docker

Installation Steps 1. Install Redis Download the ...

A small collection of html Meta tags

<Head>……</head> indicates the file he...

Can't connect to local MySQL through socket '/tmp/mysql.sock' solution

Error message: ERROR 2002: Can't connect to l...

Detailed explanation of whereis example to find a specific program in Linux

Linux finds a specific program where is The where...

How to select all child elements and add styles to them in CSS

method: Take less in the actual project as an exa...

React implements double slider cross sliding

This article shares the specific code for React t...

Example of implementing grouping and deduplication in MySQL table join query

Table of contents Business Logic Data table struc...

Docker data management and network communication usage

You can install Docker and perform simple operati...

Using CSS to implement loading animation of Android system

There are two common loading icons on the web, on...

CSS positioning layout (position, positioning layout skills)

1. What is positioning? The position attribute in...

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and rever...

Explanation of the configuration and use of MySQL storage engine InnoDB

MyISAM and InnoDB are the most common storage eng...