<!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="./06-素材/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: './06-素材/images/slider01.jpg', title: '对人类来说会不会太超前了?', color: 'rgb(100, 67, 68)' },
{ url: './06-素材/images/slider02.jpg', title: '开启剑与雪的黑暗传说!', color: 'rgb(43, 35, 26)' },
{ url: './06-素材/images/slider03.jpg', title: '真正的jo厨出现了!', color: 'rgb(36, 31, 33)' },
{ url: './06-素材/images/slider04.jpg', title: '李玉刚:让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
{ url: './06-素材/images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
{ url: './06-素材/images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
{ url: './06-素材/images/slider07.jpg', title: '一站式解决你的电脑配置问题!!!', color: 'rgb(53, 29, 25)' },
{ url: './06-素材/images/slider08.jpg', title: '谁不想和小猫咪贴贴呢!', color: 'rgb(99, 72, 114)' },
]
const slider = document.querySelector('.slider')
const next = document.querySelector('.toggle .next')
const prev = document.querySelector('.toggle .prev')
const img = document.querySelector('.slider-wrapper img')
const sliderFooter = document.querySelector('.slider-footer ')
const footerContent = document.querySelector('.slider-footer p')
const list = document.querySelectorAll('.slider-indicator li')
let i = 0
//timeId在事件中是变量 每发生一次事件 timeId的值都加1
let timeId = setInterval(function () {
next.click()
}, 1000)
slider.addEventListener('mouseenter', function () {
clearInterval(timeId) //timeId是数字类型 所以切记 不要加引号
console.log(timeId);
})
//
slider.addEventListener('mouseleave', function () {
//setInterval已经申明全局变量直接用timeId即可 就不要再重复声明局部变量了 这样出现新的定时器导致轮播图加速
timeId = setInterval(function () {
next.click()
}, 1000)
console.log(timeId);
})
//console.log(slider,next,img,sliderFooter,footerContent,list);
next.addEventListener('click', function () {
i++
if (i >= sliderData.length) {
i = 0
}
fn()
})
prev.addEventListener('click', function () {
i--
if (i < 0) {
i = sliderData.length - 1
}
fn()
})
for(let j=0; j<list.length;j++)
list[j].addEventListener('click',function(){
i=j
fn()
})
function fn() {
img.src = sliderData[i].url //src是标签属性 css中 height=123px height是属性名 123px是属性值 对象由属性和方法组成
sliderFooter.style.backgroundColor = sliderData[i].color
footerContent.innerHTML = sliderData[i].title
//排他思想
document.querySelector('.active').classList.remove('active') //获取active 并清除所有html中已调用的active
list[i].classList.add('active') // list是一个已经获取所有li标签的伪数组 实现为list里面的元素li标签调用 类选择器active
}
</script>
</body>
</html>
轮播图(附带解决轮播图加速 和鼠标经过只暂停一次的bug)
最新推荐文章于 2023-11-21 20:17:05 发布