当前位置:网站首页>原生js系列
原生js系列
2022-07-30 17:52:00 【YingUser】
1、图片的切换
包含了定时器以及修改属性的属性
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片切换</title>
</head>
<body>
<img src="./images/1.jpg" alt="" id="flower" width="200" height="200">
<br>
<br>
<button id="preBtn">上一张</button> <button id="nexBtn">下一张</button>
<script type="text/javascript">
let flower = document.getElementById('flower')
let preBtn = document.getElementById('preBtn')
let nexBtn = document.getElementById("nexBtn")
let minIndex = 1, maxIndex = 5; currentIndex = minIndex
// 上一张
nexBtn.onclick = function () {
if (currentIndex === maxIndex) {
currentIndex = minIndex
} else {
currentIndex++
}
console.log(currentIndex);
flower.setAttribute('src', `images/${currentIndex}.jpg`)
}
// 下一张
preBtn.onclick = function () {
if (currentIndex === minIndex) {
currentIndex = maxIndex
} else {
currentIndex--
}
console.log(currentIndex);
flower.setAttribute('src', `images/${currentIndex}.jpg`)
}
// 定时器切换
let timer = setInterval(() => {
currentIndex++
if (currentIndex === maxIndex) {
currentIndex = minIndex
}
flower.setAttribute('src', `images/${currentIndex}.jpg`)
}, 3000)
</script>
</body>
</html>
2、图片的显示与隐藏
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片的显示和隐藏</title>
</head>
<body>
<button id="btn">隐藏</button>
<br>
<img src="./images/10.jpg" alt="" id="new">
<script type="text/javascript">
// 1、获取事件源
let oBtn=document.getElementById('btn')
let newImg=document.getElementsByTagName('img')[0]
// let isShow=true 这是另外一种方法,用一个变量控制
// 绑定事件
oBtn.onclick=function(){
// 事件驱动程序
if(oBtn.innerHTML==='隐藏'){
newImg.style.display = 'none'
oBtn.innerHTML='显示'
// isShow=false
}else{
newImg.style.display = 'block'
oBtn.innerHTML = '隐藏'
// isShow = true
}
}
</script>
</body>
</html>
边栏推荐
- 5 个开源的 Rust Web 开发框架,你选择哪个?
- Basic knowledge points in js - BOM
- ByteArrayInputStream 类源码分析
- Promise entry to proficient (1.5w word detailed explanation)
- 编曲软件FL Studio中文版安装教程及切换语言教程
- 开源盛宴ApacheCon Asia 2022即将开幕,精彩不容错过!
- Dodging ice cream assassins?Crawling ice cream prices through crawlers
- leetcode-547:省份数量
- 数据库系统原理与应用教程(064)—— MySQL 练习题:操作题 51-61(八):查询条件的构造、通配符
- LayaBox---TypeScript---类型兼容性
猜你喜欢
随机推荐
数据库系统原理与应用教程(066)—— MySQL 练习题:操作题 71-81(十):连接查询
游戏化产品搭建思路的拆解与探究
【AGC】增长服务2-应用内消息示例
Pagoda builds PHP adaptive lazy website navigation source code measurement
Valid bracketed strings [greedy exercise]
Error occurred while trying to proxy request The project suddenly can't get up
Metaverse Web 3.0 和 DeFi大师班
数据库系统原理与应用教程(069)—— MySQL 练习题:操作题 95-100(十三):分组查询与聚合函数的使用
知识蒸馏2:目标检测中的知识蒸馏
LayaBox---TypeScript---枚举
Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
Servo System of Hydraulic Steering Gear Based on Fuzzy PID
【AGC】构建服务1-云函数示例
Moralis去中心化Web3应用开发教程
你好好想想,你真的需要配置中心吗?
[OC study notes] attribute keyword
Hangzhou electric school game 2 1001 2022 Static Query on Tree (Tree + hash table difference chain subdivision
一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧试读版
【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询