轮播图还是很常用的
轮播图使用的场景通常在网页首页上,在有限的空间可以通过轮播图,循环播放同一类型的图片、文字等内容。轮播图目前表现形式有 2 种,一种是常规的只出现一张图片,另一种是出现三张图片凸显一张的卡片化的。因为轮播图广泛使用,目前很多工具库(例如 swiper )都提供了现成的轮播图解决方案.
知识点
- 事件防抖
- 轮播图
我都不想写了
首页中间内容初始化1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80<!-- index.html -->
<html lang="zh-CN">
<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>
<link rel="stylesheet" href="./css/reset.css" />
<link rel="stylesheet" href="./css/common.css" />
<link rel="stylesheet" href="./css/home.css" />
<script src="./assets/iconfont.js"></script>
</head>
<body>
<div class="wrapper d-flex flex-column">
<header class="header" id="header-title">
<a href="#home"> 推荐页</a>
</header>
<section id="app" class="content">
<!-- 此处为中间内容,通过 js 动态生成 -->
</section>
<section class="player-control d-flex justify-content-start ">
<div class="player-control-songinfo d-flex justify-content-start">
<div class="img ">
<img
src="https://th.bing.com/th/id/R7466240255fa48164e1d741fb8b29893?rik=qd9WWhLotZqIAg&riu=http%3a%2f%2fpicture.ik123.com%2fuploads%2fallimg%2f160906%2f3-160Z6154A8.jpg&ehk=T2cuZ%2fECZd2tvUCSXRgW2e2axgZ8c4rM2GQpotzo3Ec%3d&risl=&pid=ImgRaw"
alt=""
/>
</div>
<div class="songinfo d-flex flex-column align-items-start">
<span class="songname">歌曲一</span>
<span class="single-text-omitted singer">歌手2323232323232</span>
</div>
</div>
<div class="player-control-unit d-flex justify-content-between">
<div class="control">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-shangyiqu"></use>
</svg>
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-zanting"></use>
</svg>
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-xiayiqu"></use>
</svg>
</div>
<div class="d-flex justify-content-between">
<div class="song-progress d-flex justify-content-between">
<span class="current-time">00:00</span>
<div class="progress" id="progress-bar">
<div class="progress-bar"></div>
<div class="progress-dot"></div>
</div>
<span class="total-time">00:00</span>
</div>
<div class="volume d-flex">
<svg class="icon volume-icon" aria-hidden="true">
<use xlink:href="#icon-yinliang"></use>
</svg>
<div class="progress" id="volume-bar">
<div class="progress-bar"></div>
<div class="progress-dot"></div>
</div>
</div>
</div>
<div class="list">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-liebiaoxunhuan"></use>
</svg>
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-bofangliebiao"></use>
</svg>
</div>
</div>
</section>
</div>
<script src="./js/index.js" type="module"></script>
</body>
</html>
- 在 js 文件夹下,新建 home 文件夹,在 home 文件夹内新增 home.js 文件,写下
document.querySelector('#app').innerHTML = '111';
。 - 动态加载 home.js 文件。在 index.js 文件中监听 window 的 load 事件,动态生成 script 标签,设置 src 属性为 home.js 文件夹的相对位置。
1 | // index.js |
1 | // home.js |
轮播图
1 | // ajax.js |
1 | //home.js |
在 home 文件夹内新建一个 carousel.js 的文件来单独完成轮播图1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73//carousel.js
// 切换箭头为静态 HTML 样式,无需根据图片数量动态生成。
const carouselControl = `
<button class="carousel-control carousel-control-left carousel-control-hover">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-arrow-left"></use>
</svg>
</button>
<button class="carousel-control carousel-control-right carousel-control-hover">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-arrow-right"></use>
</svg>
</button>
`;
//轮播图配置
const carousel = {
data: [], //轮播图数据
currentIndex: 0, //轮播图当前切换的画面
times: 2000, //轮播图多少时间切换画面
animationTimes: 0.5, //轮播图动画持续时间,单位s
autoCycleTimer: new Set(), //如果在切换动画,无法进行切换画面
};
export function carouselRender(data) {
//初始化轮播图
let carouselItem = "",
carouselIndicatorsLi = "";
const wrapper = document.querySelector(".carousel-wrapper");
let { width = 0 } = wrapper.getBoundingClientRect(); //得到图片的宽度
//动态生成轮播图
data.forEach((item, index) => {
//指示器激活选中判断
let isActive = carousel.currentIndex == index ? "active" : "";
//动态生成轮播图图片,并给每一张图片加上偏移量和动画效果
carouselItem += `
<div class="carousel-item ${
"#" + index
}" style='transform:translateX(${
width * (index - 1)
}px);transition-duration:${carousel.animationTimes}s'>
<img src="${item.pic}" alt="">
</div>
`;
//动态生成轮播图指示器
carouselIndicatorsLi += `
<li data-slide-to="${index}" class="carousel-indicators-li ${isActive}"></li>
`;
});
// 通过模板字符串,按照 home.html 中的 html 结构进行排布
const carouselContainer = `
<div class="carousel-container" style="transition:transform ${carousel.animationTimes}s ">
${carouselControl}
<div class="carousel-content">
${carouselItem}
</div>
</div>
`;
const carouselIndicators = `
<ul class="carousel-indicators d-flex">
${carouselIndicatorsLi}
</ul>
`;
// 将得到的字符串通过 innerHTML 插入到轮播图盒子
wrapper.innerHTML = carouselContainer + carouselIndicators;
}
Ajax({
url: "/homepage/block/page",
}).then((res) => {
console.log(res);
carousel.data = res.data.blocks[0].extInfo.banners;
//首次渲染轮播图
carouselRender(carousel.data);
});