轮播图:可触发效果
:1、按箭头符号可左右切换图片
2、到最后一张的下一张是开头第一张;开头第一张的前一张是最后一张
3、鼠标放在轮播图上时轮播图停止定时器;鼠标移开定时器继续开启
注意:图片需要自己准备且对应
部分HTML代码:
<!DOCTYPE html>
<html lang="en"><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><style>* {box-sizing: border-box;}.slider {width: 560px;height: 400px;overflow: hidden;}.slider-wrapper {width: 100%;height: 320px;}.slider-wrapper img {width: 100%;height: 100%;display: block;}.slider-footer {height: 80px;background-color: rgb(100, 67, 68);padding: 12px 12px 0 12px;position: relative;}.slider-footer .toggle {position: absolute;right: 0;top: 12px;display: flex;}.slider-footer .toggle button {margin-right: 12px;width: 28px;height: 28px;appearance: none;border: none;background: rgba(255, 255, 255, 0.1);color: #fff;border-radius: 4px;cursor: pointer;}.slider-footer .toggle button:hover {background: rgba(255, 255, 255, 0.2);}.slider-footer p {margin: 0;color: #fff;font-size: 18px;margin-bottom: 10px;}.slider-indicator {margin: 0;padding: 0;list-style: none;display: flex;align-items: center;}.slider-indicator li {width: 8px;height: 8px;margin: 4px;border-radius: 50%;background: #fff;opacity: 0.4;cursor: pointer;}.slider-indicator li.active {width: 12px;height: 12px;opacity: 1;}</style>
</head><body><div class="slider"><div class="slider-wrapper"><img src="./imgs/slider01.jpg" alt="" /></div><div class="slider-footer"><p>对人类来说会不会太超前了?</p><ul class="slider-indicator"><li class="active"></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li></ul><div class="toggle"><button class="prev"><</button><button class="next">></button></div></div></div><script>// 需求:当点击左边的按钮可以切换轮播图// 分析:右侧按钮点击,变量++;如果大于等于8则复原0;// 左侧按钮点击,变量--;如果小于0则复原最后一张// 鼠标经过暂停定时器,鼠标离开开始定时器// 1. 初始数据const sliderData = [{ url: './imgs/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },{ url: './imgs/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },{ url: './imgs/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },{ url: './imgs/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },{ url: './imgs/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },{ url: './imgs/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },{ url: './imgs/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },{ url: './imgs/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },]</script>
</body></html>
JavaScript代码:
// 获取p元素const p = document.querySelector('.slider-footer p')//获取footerconst footer = document.querySelector('.slider-footer')// 获取右边按钮const next = document.querySelector('.next')// 获取左边按钮const prev = document.querySelector('.prev')let i = 0// while (true) {//渲染对应的数据function move(i) {img.src = sliderData[i].urlp.innerHTML = sliderData[i].title// 获取元素footer.style.backgroundColor = sliderData[i].color// 先删掉之前的activedocument.querySelector('.slider-indicator .active').classList.remove('active')// 只让当前的litianjia activedocument.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')}//定时器函数function timer() {}next.addEventListener('click', function () {// if (i >= 8)// i = 0// i++i++i = i >= sliderData.length - 1 ? 0 : i// 调用渲染函数move(i)})prev.addEventListener('click', function () {// if (i < 0)// i = 7i--i = i < 0 ? sliderData.length - 1 : i// 调用渲染函数move(i)})// }// 记录定时器idlet timeId = 0// 自动播放timeId = setInterval(function () {// 利用js自动调用点击事件next.click()}, 1000)document.querySelector('.slider').addEventListener('mouseenter', function () {// 停止定时器clearInterval(timeId)})//鼠标移开开启定时器document.querySelector('.slider').addEventListener('mouseleave', function () {timeId = setInterval(function () {// 利用js自动调用点击事件next.click()}, 1000)})