写一个音乐播放器_轮播图实现

轮播图还是很常用的

轮播图使用的场景通常在网页首页上,在有限的空间可以通过轮播图,循环播放同一类型的图片、文字等内容。轮播图目前表现形式有 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 -->
<!DOCTYPE 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>
  1. 在 js 文件夹下,新建 home 文件夹,在 home 文件夹内新增 home.js 文件,写下 document.querySelector('#app').innerHTML = '111';
  2. 动态加载 home.js 文件。在 index.js 文件中监听 window 的 load 事件,动态生成 script 标签,设置 src 属性为 home.js 文件夹的相对位置。
1
2
3
4
5
6
7
8
// index.js
window.addEventListener("load", () => {
console.log("load");
const script = document.createElement("SCRIPT");
script.src = "./js/home/home.js";
script.setAttribute("type", "module");
document.querySelector("body").appendChild(script);
});
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
// home.js
const homePageTemplate = `
<div class="w">
<div class="carousel-wrapper">
<div class="carousel-container ">
<!-- 切换箭头 -->
<!-- 轮播图图片需要动态生成 -->
</div>
<!-- 指示器 -->
<ul class="carousel-indicators d-flex">

</ul>
</div>
<div class="recommend-playlist">
<h3 class="recommend-playlist-header">推荐歌单<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-arrow-right"></use>
</svg>
</h3>
<ul class="recommend-playlist-container d-flex justify-content-between align-items-start">
<!-- 推荐歌单需要动态生成 -->
</ul>
</div>
</div>
`;
//首页初始化
document.querySelector("#app").innerHTML = homePageTemplate;

轮播图

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
// ajax.js
const BASE_URL = "http://localhost:3000";
export default function Ajax({
//请求参数配置
method = "GET", //默认为'get'请求
url,
data = {},
}) {
return new Promise((resolve) => {
// 通过 Promise 返回异步请求
const xhr = new XMLHttpRequest();
xhr.open(method, BASE_URL + url);
xhr.onload = function () {
resolve(JSON.parse(xhr.response));
};
xhr.onerror = function () {
// 待最后进行错误处理操作
if (xhr.status == 0) {
}
};
xhr.send(JSON.stringify(data));
});
}

/**
* @description: 获得轮播图信息
* @param {*}
* @return {*}
*/
export async function getBannerList() {
const result = Ajax({
url: `/homepage/block/page`,
});
return result;
}
1
2
3
4
5
//home.js
import { getBannerList } from "../service/ajax.js";

const result = await getBannerList();
const carouselData = result.data.blocks[0].extInfo.banners;

在 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);
});
-------------本文结束感谢您的阅读-------------
感谢阅读.

欢迎关注我的其它发布渠道