鼠标悬停效果
//鼠标悬停效果
slider.addEventListener('mouseenter', function () {
clearInterval(timeId)
})
//鼠标移开效果
slider.addEventListener('mouseleave', function () {
timeId = setInterval(function fn() {
next.click()
}, 3000)
})
点击侧键切换图片功能
// 侧按钮渲染函数
function toggle() {
img.src = sliderData[i].url//修改图片路径
console.log(sliderData[i]);
p.innerHTML = sliderData[i].title//修改P
footer.style.backgroundColor = sliderData[i].color//修改背景色
document.querySelector('.slider-indicator .active').classList.remove('active') //删除小圆点
document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active') //修改小圆点
}
// 设置左按键点击事件
prev.addEventListener('click', function () {
i--
i = i < 0 ? (sliderData.length - 1) : i//利用三元表达式实现图片循环
toggle()//调用渲染函数
})
// 设置右按键点击事件
next.addEventListener('click', function () {
i++
i = i % sliderData.length//利用取余运算实现图片循环
toggle()//调用渲染函数
})
CSS部分
<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>
引入图片
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 random = parseInt(Math.random() * sliderData.length)//生成一个随机数
const img = document.querySelector(' .slider-wrapper img')//获取图片
const p = document.querySelector('.slider-footer p')//获取P
const footer = document.querySelector('.slider-footer') //获取背景颜色
const slider = document.querySelector('.slider')//获取背景盒子
const prev = document.querySelector('.prev')//获取左按键
const next = document.querySelector('.next')//获取右按键
const li = document.querySelector('li')//获取右按键