轮播图案例

1.鼠标不在图片上方时,进行自动轮播,并且左右箭头不会显示;当鼠标放在图片上方时,停止轮播,并且左右箭头会显示;

2.图片切换之后,图片中下方的小圆点会同时进行切换,并且点击相应的小圆点可以切换到相应的图片上;

3.点击左右箭头可以进行左右图片的切换;

4.图片上需有介绍的文字,会随图片切换一起进行切换。

<!DOCTYPE html>

<html lang="zh-CN">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

<style>

* {

box-sizing: border-box;

}

.box1 {

width: 560px;

height: 400px;

overflow: hidden;

}

.box2 {

width: 100%;

height: 320px;

}

.box2 img {

width: 100%;

height: 100%;

display: block;

}

.box3 {

height: 80px;

background-color: rgb(100, 67, 68);

padding: 12px 12px 0 12px;

position: relative;

}

.btn {

position: absolute;

right: 0;

top: 12px;

display: flex;

}

.box3 .btn 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;

}

.next,.prev {

opacity: 0;

}

.box1:hover .next {

opacity: 1;

}

.box1:hover .prev {

opacity: 1;

}

.box3 .btn button:hover {

background: rgba(255, 255, 255, 0.2);

}

p {

margin: 0;

color: #fff;

font-size: 18px;

margin-bottom: 10px;

}

.indicator {

margin: 0;

padding: 0;

list-style: none;

display: flex;

align-items: center;

}

.indicator li {

width: 8px;

height: 8px;

margin: 4px;

border-radius: 50%;

background: #fff;

opacity: 0.4;

cursor: pointer;

}

.indicator li.active {

width: 12px;

height: 12px;

opacity: 1;

}

</style>

</head>

<body>

<div class="box1">

<div class="box2">

<img src="./img/course01.png" alt="">

</div>

<div class="box3">

<p>11111111111111111111</p>

<ul class="indicator">

<li class="active"></li>

<li></li>

<li></li>

<li></li>

<li></li>

<li></li>

<li></li>

<li></li>

</ul>

<div class="btn">

<button class="prev">&lt;</button>

<button class="next">&gt;</button>

</div>

</div>

</div>

<script>

// 轮播数据源定义

const data = [

{ url: './img/course1.png', title: '111111111', color: 'green' },

{ url: './img/course2.png', title: '222222222', color: 'yellow' },

{ url: './img/course3.png', title: '333333333', color: 'blue' },

{ url: './img/course4.png', title: '444444444', color: 'pink' },

{ url: './img/course5.png', title: '555555555', color: 'red' },

{ url: './img/course6.png', title: '666666666', color: 'brown' },

{ url: './img/course7.png', title: '777777777', color: 'grey' },

{ url: './img/course8.png', title: '888888888', color: 'skyblue' },

]

const img = document.querySelector('.box2 img')

const p = document.querySelector('.box3 p')

const box3 = document.querySelector('.box3')

const next = document.querySelector('.next')

const prev = document.querySelector('.prev')

let i = 0

// 下一张按钮点击事件

next.addEventListener('click', function () {

i++ // 索引+1,切换到下一项

if(i >= 8) { // 边界判断:超过最后一项(索引7),重置为第一项(索引0)

i = 0

}

btn() // 调用更新函数,同步渲染轮播内容

})

// 上一张按钮点击事件

prev.addEventListener('click', function () {

i-- // 索引-1,切换到上一项

if(i < 0) { // 边界判断:小于第一项(索引0),重置为最后一项(索引7)

i = 7

}

btn() // 调用更新函数

})

function btn () {

img.src = datai.url // 更新轮播图片路径

p.innerHTML = datai.title // 更新标题文本

box3.style.backgroundColor = datai.color // 更新box3背景色

// 指示器高亮切换:先移除原有的active,再给当前项加active

document.querySelector('.indicator .active').classList.remove('active')

document.querySelector(`.indicator li:nth-child(${i + 1})`).classList.add('active')

}

// 自动轮播:定时实现

let timerId = setInterval(function () {

next.click()

}, 1000)

const box1 = document.querySelector('.box1')

// 鼠标进入

box1.addEventListener('mouseenter', function () {

clearInterval(timerId) // 清除定时器,暂停轮播

})

// 鼠标离开

box1.addEventListener('mouseleave', function () {

clearInterval(timerId) // 先清除旧定时器

timerId = setInterval(function () { // 重新创建定时器,恢复轮播

next.click()

}, 1000)

})

</script>

</body>

</html>

相关推荐
TA远方7 小时前
【HTML】JavaScript Canvas 图像截取与保存完整指南
前端·javascript·html·canvas·截图·截取
feixing_fx8 小时前
选择器的威力——深入理解优先级计算与层叠规则
开发语言·前端·css·前端框架·html
星空10 小时前
html\css\js入门
javascript·css·html
ShyanZh12 小时前
【skill】HTML-PPT:36主题31布局的专业HTML演示文稿工作室
ai·html·powerpoint·html-ppt
晓得迷路了13 小时前
栗子前端技术周刊第 133 期 - Angular v22、React 编译器 Rust 版、pnpm 11.5...
前端·javascript·css
烤代码的吐司君14 小时前
Redis 服务配置与使用
前端·bootstrap·html
程序猿小泓14 小时前
2026 前端面试全攻略:手写题、算法与计网/TS 高频考点
前端·javascript·css
xinhuanjieyi1 天前
html修复游戏种太阳错误
前端·游戏·html
小小19921 天前
idea 配置less转化为css
前端·css·less
hhb_6181 天前
Less嵌套避坑:优先级冲突实战解析
前端·css·less