倒计时
<body>
<input type="button" value="倒计时5秒钟" />
<script>
let btn = document.querySelector('input')
btn.addEventListener('click', function () {
let timeCount = 5
let Id = setInterval(function () {
timeCount--
btn.value = `倒计时${timeCount}s`
btn.disabled = true
if (timeCount == -1) {
clearInterval(Id)
btn.disabled = false
btn.value = `倒计时5s`
}
}, 1000)
})
</script>
</body>
自动轮播图
<style>
* {
padding: 0;
margin: 0;
}
div {
width: 700px;
height: 320px;
margin: 100px auto;
position: relative;
}
p {
position: absolute;
left: 0;
bottom: 0;
line-height: 50px;
background-color: #666;
color: #fff;
width: 100%;
padding-left: 10px;
font-size: 20px;
box-sizing: border-box;
}
</style>
<body>
<div>
<img src="./images/b01.jpg" alt="" />
<p>第1张图片</p>
</div>
<script>
let img = document.querySelector('img')
let p = document.querySelector('p')
let index = 1
setInterval(function () {
index++
if (index > 9) {
index = 1
}
let path = `./images/b0${index}.jpg`
img.src = path
p.innerText = `第${index}张图片`
}, 1000)
</script>
随机点名后删除
<style>
p {
width: 200px;
height: 100px;
border: 1px solid red;
}
</style>
<body>
<p>这里显示名字</p>
<button>随机点名</button>
<script>
let names = ['11', '22', '33', '44', '55']
let p = document.querySelector('p')
let btn = document.querySelector('button')
btn.addEventListener('click', function () {
let random = parseInt(Math.random() * names.length)
let name = names[random]
p.innerHTML = name
names.splice(random, 1)
if (names.length < 1) {
btn.disabled = true
}
console.log(names);
})
</script>
随机点名-结束随机点名
<style>
p {
width: 200px;
height: 100px;
border: 1px solid red;
}
</style>
<body>
<p>这里显示名字</p>
<button class="start">开始随机点名</button>
<button class="end">结束随机点名</button>
<script>
let names = ['11', '22', '33', '44', '55']
let p = document.querySelector('p')
let start = document.querySelector('.start')
let end = document.querySelector('.end')
let timeId
start.addEventListener('click', function () {
timeId = setInterval(function () {
let random = parseInt(Math.random() * names.length)
let name = names[random]
p.innerHTML = name
}, 500)
})
end.addEventListener('click', function () {
clearInterval(timeId)
})
</script>
</body>
鼠标移入移出
<style>
* {
padding: 0;
margin: 0;
}
div {
width: 700px;
height: 320px;
margin: 100px auto;
position: relative;
}
p {
position: absolute;
left: 0;
bottom: 0;
line-height: 50px;
background-color: #666;
color: #fff;
width: 100%;
padding-left: 10px;
font-size: 20px;
box-sizing: border-box;
}
</style>
<body>
<div>
<img src="./images/b01.jpg" alt="" />
<p>第1张图片</p>
</div>
<script>
let img = document.querySelector('img')
let p = document.querySelector('p')
let index = 1
let timeId
function startTime() {
timeId = setInterval(function () {
index++
if (index > 9) {
index = 1
}
let path = `./images/b0${index}.jpg`
img.src = path
p.innerText = `第${index}张图片`
}, 1000)
}
let div = document.querySelector('div')
div.addEventListener('mouseenter', function () {
clearInterval(timeId)
})
div.addEventListener('mouseleave', function () {
startTime()
})
</script>
</body>