目录
1、自动轮播
2、点击更换
3、自动播放加左右箭头点击切换
4、完整版轮播图
1、自动轮播
用定时器setInterval()来写,可以实现自动播放
<!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="./images/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>// 1. 初始数据const sliderData = [{ url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },{ url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },{ url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },{ url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },{ url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },{ url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },{ url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },{ url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },]//获取元素const img = document.querySelector('.slider-wrapper img')const p = document.querySelector(".slider-footer p")const footer = document.querySelector('.slider-footer')const slider = document.querySelector(".slider")const lis = document.querySelectorAll('.slider-indicator li')const next = document.querySelector('.toggle .next')const prev = document.querySelector('.toggle .prev')let i = 0//开启定时器let timer = setInterval(function () {i++if (i === sliderData.length) {i = 0}// 设置图片img.src = sliderData[i].url// 设置标题p.innerHTML = sliderData[i].title// 设置底部区域背景色footer.style.backgroundColor = sliderData[i].color// 设置小圆圈样式// 把其他某个li的active移除 再给对应的li设置document.querySelector('.slider-indicator .active').classList.remove('active')lis[i].classList.add('active')}, 1000)</script>
</body></html>
2、点击更换
点击那个小圆点就跳到对应位置
<!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="./images/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>// 1. 初始数据const sliderData = [{ url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },{ url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },{ url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },{ url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },{ url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },{ url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },{ url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },{ url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },]//获取元素const img = document.querySelector('.slider-wrapper img')const p = document.querySelector(".slider-footer p")const footer = document.querySelector('.slider-footer')const slider = document.querySelector(".slider")const lis = document.querySelectorAll('.slider-indicator li')const next = document.querySelector('.toggle .next')const prev = document.querySelector('.toggle .prev')//遍历所有的li,给每个li绑定一个点击事件for(let i=0;i<lis.length;i++){lis[i].addEventListener('click',function() {//移出所有的active类document.querySelector('.slider-indicator .active').classList.remove('active')//谁点击谁添加active类this.classList.add('active')img.src = sliderData[i].urlp.innerHTML = sliderData[i].titlefooter.style.backgroundColor = sliderData[i].color})}</script>
</body></html>
3、自动播放加左右箭头点击切换
需求:要自动播放
点击左右按钮可以跳转
鼠标移入停止自动播放,鼠标离开继续自动播放
思路:
需要开启定时器来实现自动播放
给左右按钮添加点击事件
给整体添加鼠标移入清除定时器功能
鼠标移出添加开启定时器功能
<!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="./images/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>// 1. 初始数据const sliderData = [{ url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },{ url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },{ url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },{ url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },{ url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },{ url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },{ url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },{ url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },]const img = document.querySelector('.slider-wrapper img')const p = document.querySelector(".slider-footer p")const footer = document.querySelector('.slider-footer')const slider = document.querySelector(".slider")const lis = document.querySelectorAll('.slider-indicator li')const next = document.querySelector('.toggle .next')const prev = document.querySelector('.toggle .prev')let i = 0//开启定时器let timer = setInterval(function () {i++if (i === sliderData.length) {i = 0}img.src = sliderData[i].urlp.innerHTML = sliderData[i].titlefooter.style.backgroundColor = sliderData[i].colordocument.querySelector('.slider-indicator .active').classList.remove('active')lis[i].classList.add('active')}, 1000)next.addEventListener('click', function () {i++if (i === sliderData.length) {i = 0}img.src = sliderData[i].urlp.innerHTML = sliderData[i].titlefooter.style.backgroundColor = sliderData[i].colordocument.querySelector('.slider-indicator .active').classList.remove('active')lis[i].classList.add('active')})prev.addEventListener('click',function() {i--if (i< 0) {i = sliderData.length}img.src = sliderData[i].urlp.innerHTML = sliderData[i].titlefooter.style.backgroundColor = sliderData[i].colordocument.querySelector('.slider-indicator .active').classList.remove('active')lis[i].classList.add('active')})slider.addEventListener('mouseenter', function () {clearInterval(timer)})slider.addEventListener('mouseleave', function () {timer = setInterval(function () {i++if (i === sliderData.length) {i = 0}img.src = sliderData[i].urlp.innerHTML = sliderData[i].titlefooter.style.backgroundColor = sliderData[i].colordocument.querySelector('.slider-indicator .active').classList.remove('active')lis[i].classList.add('active')}, 1000)})</script>
</body></html>
优化代码:把重复的代码封装成一个为toggle() 的函数,哪里需要,哪里调用,减少代码冗余
<!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="./images/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>// 1. 初始数据const sliderData = [{url: './images/slider01.jpg',title: '对人类来说会不会太超前了?',color: 'rgb(100, 67, 68)',},{url: './images/slider02.jpg',title: '开启剑与雪的黑暗传说!',color: 'rgb(43, 35, 26)',},{url: './images/slider03.jpg',title: '真正的jo厨出现了!',color: 'rgb(36, 31, 33)',},{url: './images/slider04.jpg',title: '李玉刚:让世界通过B站看到东方大国文化',color: 'rgb(139, 98, 66)',},{url: './images/slider05.jpg',title: '快来分享你的寒假日常吧~',color: 'rgb(67, 90, 92)',},{url: './images/slider06.jpg',title: '哔哩哔哩小年YEAH',color: 'rgb(166, 131, 143)',},{url: './images/slider07.jpg',title: '一站式解决你的电脑配置问题!!!',color: 'rgb(53, 29, 25)',},{url: './images/slider08.jpg',title: '谁不想和小猫咪贴贴呢!',color: 'rgb(99, 72, 114)',},]// 找到相关元素const oImg = document.querySelector('.slider-wrapper > img')const title = document.querySelector('.slider-footer > p')const sliderFooter = document.querySelector('.slider-footer')const lis = document.querySelectorAll('.slider-indicator li')const prev = document.querySelector('.prev')const next = document.querySelector('.next')const slider = document.querySelector('.slider')// 定义一个变量let i = 0// 开启定时器 开启自动播放let timerId = setInterval(function() {// i++// if (i === sliderData.length) {// i = 0// }// toggle()// 方式二 模拟点击右侧按钮next.click()}, 1500)// 给右侧按钮绑定事件next.addEventListener('click', function () {i++if (i === sliderData.length) {i = 0}toggle()})// 给左侧按钮绑定事件prev.addEventListener('click', function () {i--if (i < 0) {i = sliderData.length - 1}toggle()})// 给slider绑定鼠标移入事件slider.addEventListener('mouseenter', function () {clearInterval(timerId)})// 给slider绑定鼠标移出事件slider.addEventListener('mouseleave', function () {timerId = setInterval(function () {next.click()}, 1500)})// 切换function toggle() {// 设置图片oImg.src = sliderData[i].url// 设置标题title.innerHTML = sliderData[i].title// 设置底部区域背景色sliderFooter.style.backgroundColor = sliderData[i].color// 设置小圆圈样式// 把其他某个li的active移除 再给对应的li设置document.querySelector('.slider-indicator .active').classList.remove('active')// lis[i].classList.add('active') 方式二document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')}</script></body>
</html>
4、完整版轮播图
结合以上所有功能,是一个综合
<!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="./images/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>// 1. 初始数据const sliderData = [{ url: './images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },{ url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },{ url: './images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },{ url: './images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },{ url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },{ url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },{ url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },{ url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },]const img = document.querySelector('.slider-wrapper img')const p = document.querySelector(".slider-footer p")const footer = document.querySelector('.slider-footer')const slider = document.querySelector(".slider")const lis = document.querySelectorAll('.slider-indicator li')const next = document.querySelector('.toggle .next')const prev = document.querySelector('.toggle .prev')let i = 0//开启定时器let timer = setInterval(function () {next.click()}, 1000)//右侧点击next.addEventListener('click', function () {i++if (i === sliderData.length) {i = 0}toggle()})//左侧点击prev.addEventListener('click', function () {i--if (i < 0) {i = sliderData.length}toggle()})//鼠标移入清除定时器slider.addEventListener('mouseenter', function () {clearInterval(timer)})//鼠标移出,开启定时器slider.addEventListener('mouseleave', function () {timer = setInterval(function () {next.click()}, 1000)})//给每个li(小圆点)设置点击事件for (let j = 0; j < lis.length; j++) {lis[j].addEventListener('click', function () {document.querySelector('.slider-indicator .active').classList.remove('active')this.classList.add('active')img.src = sliderData[j].urlp.innerHTML = sliderData[j].titlefooter.style.backgroundColor = sliderData[j].colori = j})}//相同代码封装成一个函数,方便复用function toggle() {img.src = sliderData[i].urlp.innerHTML = sliderData[i].titlefooter.style.backgroundColor = sliderData[i].colordocument.querySelector('.slider-indicator .active').classList.remove('active')lis[i].classList.add('active')}</script>
</body></html>