Home >Web Front-end >JS Tutorial >js implements simple click on picture loop playback
This article mainly introduces the js implementation of simple click picture loop playback, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Simple click picture loop Play:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <img src="imges/01.jpg" alt="" id='img'> <button id='prev'>上一张</button> <button id='next'>下一张</button> </body> <script> window.onload = function(){ //获取要用到的元素,标签 var img = document.getElementById('img'); var prev = document.getElementById('prev'); var next = document.getElementById('next'); //定义两端的临界值, 定义活动的变量 最大5张、 var maxIndex = 5; var minIndex = 1; var activeIndex = minIndex; // 确定事件源 绑定事件函数。 prev.onclick = function(){ //当点击 上一张的时候, img的src 实现切换 img.src = 'imges/0' + activeIndex + '.jpg'; if(activeIndex === 1){ activeIndex = 5; }else{activeIndex --;} } next.onclick = function(){ if(activeIndex === 5){ activeIndex = 1; }else{ activeIndex++; } img.src = 'imges/0' + activeIndex + '.jpg'; } } </script> </html>
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to getters and setters in JavaScript
Using jQuery to implement a simple nine-square grid lottery
js realizes editing by clicking the button
The above is the detailed content of js implements simple click on picture loop playback. For more information, please follow other related articles on the PHP Chinese website!