图片名称按数字规律排序的,使用了模板字符串
<img class="image" src="./images/1.jpg" alt="">
<br>
<button class="up">上一张</button>
<button class="down">下一张</button>
<script>
var image = document.querySelector('.image')
var minIndex = 1,
maxIndex = 4,
currentIndex = minIndex
document.querySelector('.down').onclick = function() {
if (currentIndex === maxIndex) {
currentIndex = minIndex
} else {
currentIndex++
}
image.setAttribute('src', `./images/${currentIndex}.jpg`)
}
document.querySelector('.up').onclick = function() {
if (currentIndex === minIndex) {
currentIndex = maxIndex
} else {
currentIndex--
}
image.setAttribute('src', `./images/${currentIndex}.jpg`)
}
</script>
效果简单

这篇文章介绍了如何使用JavaScript和模板字符串创建一个简单的图片轮播组件,通过点击按钮实现图片的上下切换,遵循特定的数字顺序排列(1到4)。

1960

被折叠的 条评论
为什么被折叠?



