当前位置:网站首页>JS picture switching case
JS picture switching case
2022-06-24 18:35:00 【Brother Mengfan】
JS Picture switching case
One 、 Case needs
Click the previous or next button , The picture will switch
Two 、 case analysis 、
1、 Event source : Button (button)
2、 Event type : Click on (onclick)
3、 Event handler :
(1) First of all, determine the picture number
(2) Picture switching : change img Of src route
3、 ... and 、 preparation
1、 Photo 4 Zhang
2、 The naming format is uniform :p1.png、p2.png、p3.png、p4.png
3、 take 4 Photos in image In the document
Four 、 The code is as follows
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Picture switching </title>
<style>
img {
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<img src="images/image01.jpg" id="flower">
<br>
<button id="prev"> Previous </button>
<button id="next"> Next </button>
<script>
// 1. Get elements
var img = document.querySelector('img')
var btn1 = document.querySelector('#prev')
var btn2 = document.querySelector('#next')
var minIndex = 1,
maxIndex = 4,
currentIndex = minIndex;
// 2. Add event
btn1.onclick = function() {
// Judge whether the number is greater than 4, Greater than, the minimum value is assigned to the current index
if (currentIndex === minIndex) {
currentIndex = maxIndex;
} else {
currentIndex--;
}
img.src = 'images/image0' + currentIndex + '.jpg';
// img.setAttribute('src', `images/image0${currentIndex}.jpg`); //ES6 grammar
};
btn2.onclick = function() {
// Judge whether the number is greater than 4, Greater than, the minimum value is assigned to the current index
if (currentIndex >= maxIndex) {
currentIndex = minIndex;
} else {
currentIndex++;
}
img.src = 'images/image0' + currentIndex + '.jpg';
// img.setAttribute('src', `images/image0${currentIndex}.jpg`); //ES6 grammar
};
</script>
</body>
</html>边栏推荐
- Digital transformation informatization data planning and technology planning
- 如何在 R 中创建线性模型预测区间 并可视化
- SAP license: ERP for supply chain management and Implementation
- Why should state-owned enterprises accelerate the digital transformation
- Five skills of selecting embedded programming language
- Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
- SAP license: what is ERP supply chain
- Ultimate Guide: comprehensive analysis of log analysis architecture of Enterprise Cloud native PAAS platform
- RestCloud ETL抽取动态库表数据实践
- How to select the best test cases for automation?
猜你喜欢
An analysis of the comments on the TV series Douban by procedural apes

微服务系统设计——接口文档管理设计

"2022" plans to change jobs and raise salary. It is necessary to ask interview questions and answers - browser

Mcu-08 interrupt system and external interrupt application

Leetcode weekly buckle 281

SAP license: what is ERP supply chain

Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck

696. count binary substring
Business based precipitation component = & gt; manage-table

Different JVM
随机推荐
【leetcode】838. Push domino (Analog)
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
Two micro service interviews where small companies suffer losses
Leetcode weekly buckle 281
Redpacketframe and openmode packages
Why are more and more people studying for doctors? Isn't it more and more difficult to graduate a doctor?
How can programmers reduce bugs in development?
股票网上开户安全吗?应该怎么办理?
如何在 R 中执行稳健回归
Bisection function template
What if the database table structure changes? Smartbi products support one click synchronization
Industry Cloud video editing software
Wechat applet to realize stacked rotation
Digital transformation informatization data planning and technology planning
Leetcode topic [array] -216- combined sum III
Differences between get and post request modes
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
Six configuration management tools that administrators must know
微服务系统设计——接口文档管理设计
SAP license: SAP s/4 Hana module function introduction