网页功能升华必备播放器

本文介绍了如何利用前端基础知识如HTML、CSS和JavaScript,创建一个网页音乐播放器,提升网页的功能性和趣味性。通过HTML布局,CSS美化和使用弹性容器布局、动画效果,以及JavaScript实现音乐切换功能。虽然目前播放器的音乐文件需要手动在JS中指定,但可通过内联框架将其嵌入到个人网页中,调整大小并定位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  当我们学完前端3件套,想在发挥下却没有想法,不如做一个可以播放音乐的播放器在自己的网页上,增添一些功能性,和趣味性

源码附上

这个是html的,需要有3个按钮控制歌曲上下还有歌曲进度条以及歌曲封面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css" />
    <link rel="stylesheet" href="style.css">
    <title>音乐播放器</title>
</head>

<body>
    
    <div class="music-container" id="music-container">
        <!-- 音乐信息 -->
        <div class="music-info">
            <!-- 音乐标题 -->
            <h4 id="title"></h4>
            <!-- 音乐播放进度条 -->
            <div class="progress-container" id="progress-container">
                <div class="progress" id="progress"></div>
            </div>
        </div>

        <!-- 默认第一首音乐 -->
        <audio src="music/音乐.mp3" id="audio"></audio>

        <!-- 音乐封面 -->
        <div class="img-container">
            <img src="img/音乐.jpg" alt="music-cover" id="music-cover">
        </div>
        <!-- 播放控制 -->
        <div class="navigation">
            <button id="prev" class="action-btn">
                <i class="fas fa-backward"></i>
            </button>
            <button id="play" class="action-btn action-btn-big">
                <i class="fas fa-play"></i>
            </button>
            <button id="next" class="action-btn">
                <i class="fas fa-forward"></i>
            </button>
        </div>
    </div>
    <script src="script.js"></script>
</body>

</html>

 加上css去美化一下他,主要运用弹性容器的居中与层叠的关系去设置布局,动画方面主要用transition来让动画变得高级生动起来

/*transform:translate(x,y)控制区块位置,rotate(n deg)控制旋转角度 scale(2)控制放大倍数(2倍)skew(deg,0)控制两边移动角度 */

@import url('https://fonts.googleapis.com/css?family=Lato&display=swap');

* {
    box-sizing: border-box;
   
}

body {

    height: 100vh;
/*设置容器为弹性容器*/
    display: flex;
    /* 沿垂直主轴上下垂直排列 */
    flex-direction: column;
    /* 主轴水平居中 */
    align-items: center;
    /* 主轴垂直居中 */
    justify-content: center;

    font-family: 'Lato', sans-serif;
    margin: 0;
}

.music-container {
    background-color: rgb(255, 146, 146);
    border-radius: 15px;
 
    display: flex;
    padding: 20px 30px;
    position: relative;
    margin: 70px 0;
    z-index: 10;
}

.img-container {
    position: relative;
    width: 110px;
}

.img-container::after {
    content: "";
    background-color: rgb(248, 150, 150);
    border-radius: 50%;
    position: absolute;
    bottom: 100%;
    left: 50%;
    width: 15px;
    height: 0px;
    /* 旋转 */
    transform: translate(-50%, 50%);
/*transform:translate(x,y)控制区块位置,rotate(n deg)控制旋转角度 scale(2)控制放大倍数(2倍)skew(deg,0)控制两边移动角度 */
}

.img-container img {
    border-radius: 50%;
    height: 110px;
    width: inherit;
    object-fit: cover;
    position: absolute;
    bottom: 0;
    left: 0;
    /* 封面360°旋转,默认不动 */
    animation: rotate 10s linear infinite; 
    /* 上方可设置图片旋转时间 */
    animation-play-state: paused;
}

.music-container.play .img-container img {
    /* 播放 */
    animation-play-state: running;
}

/* 定义旋转动画 */
@keyframes rotate {
    from {
        transform: rotate(0);
    }

    to {
        transform: rotate(360deg);
    }
}
/*transform:translate(x,y)控制区块位置,rotate(n deg)控制旋转角度 scale(2)控制放大倍数(2倍)skew(deg,0)控制两边移动角度 */
.navigation {
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

.action-btn {
    /* 取消默认样式 */
    border: 0;
    background-color:  rgb(248, 150, 150);
    /* ----- */
    color: #e9890c;
    font-size: 20px;
    cursor: pointer;
    padding: 10px;
    margin: 0 20px;
}

.action-btn:focus {
    /* 取消默认样式 */
    outline: 0;
}

.action-btn.action-btn-big {
    color: #e9890c;
    font-size: 30px;
}

.music-info {
    position: absolute;
    top: 0;
    left: 20px;
    /* 父元素宽度-40px */
    width: calc(100% - 40px);
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 15px 15px 0 0;
    padding: 10px 10px 10px 150px;
    /* 没播放时默认隐藏 */
    opacity: 0;
    transform: translateY(0%);
    transition: transform 0.3s ease-in, opacity 0.3s ease-in;
    /* 上方是点击音乐播放后弹出窗口弹出时间设置 */
    /* transition:名称,完成时间,速度效果,何时开始 ,这里只用来前3个*/
    z-index: 0;
}

.music-info h4 {
    /* 取消默认边距 */
    margin: 0;
}

.music-container.play .music-info {
    opacity: 1;
    transform: translateY(-100%);
/*transform:translate(x,y)可单独设置一个位置的如translateX()translateY()*/
}


.progress-container {
    background-color: #fff;
    border-radius: 5px;
    cursor: pointer;
    margin: 10px 0;
    height: 4px;
    width: 100%;
}

.progress {
    background-color: #0decfc;
    border-radius: 5px;
    height: 100%;
    /* 一开始进度条长度为0 */
    width: 0%;
    transition: width 0.1s linear;

}

    音乐的切换主要是由js来完成的主要是函数创建与变量的读取与写入。不过有一点缺点的是歌曲不能放进文件夹自己读取而是得放进去后在js文件中输入放入音乐的名字才能播放。

const musicContainer = document.getElementById("music-container")
const playBtn = document.getElementById("play")
const prevBtn = document.getElementById("prev")
const nextBtn = document.getElementById("next")

const audio = document.getElementById("audio")
const progress = document.getElementById("progress")
const progressContainer = document.getElementById("progress-container")
const title = document.getElementById("title")
const musicCover = document.getElementById("music-cover")

// 音乐信息
const songs = ["Be What You Wanna Be","Superstart", "晴天", "一路向北", "那个男孩", "本草纲目" ,"水星记", "光辉岁月" , "真的爱你","夜曲","后会无期","苦笑"]
// 默认从第一首开始
let songIndex = 0;
// 将歌曲细节加载到DOM
loadSong(songs[songIndex])
// 更新歌曲细节
function loadSong(song) {
    title.innerHTML = song
    audio.src = `music/${song}.mp3`;      // 路径为 music/Be What You Wanna Be.mp3
    musicCover.src = `img/${song}.jpg`;
}

// 播放歌曲
function playSong() {
    // 元素添加类名
    musicContainer.classList.add("play")
    playBtn.querySelector('i.fas').classList.remove('fa-play')
    playBtn.querySelector('i.fas').classList.add('fa-pause')

    audio.play()
}

// 停止播放
function pauseSong() {
    musicContainer.classList.remove('play');
    playBtn.querySelector('i.fas').classList.add('fa-play');
    playBtn.querySelector('i.fas').classList.remove('fa-pause');

    audio.pause();
}

// 上一首
function prevSong() {
    songIndex--
    if (songIndex < 0) {
        songIndex = songs.length - 1
    }
    // 加载歌曲信息并播放
    loadSong(songs[songIndex])
    playSong()
}
// 下一首
function nextSong() {
    songIndex++;

    if (songIndex > songs.length - 1) {
        songIndex = 0;
    }

    loadSong(songs[songIndex]);

    playSong();
}

// 进度条更新
function updateProgress(e) {
    // audio.duration: 音频长度
    // audio.currentTime: 音频播放位置
    // 对象解构操作
    const {
        duration,
        currentTime
    } = e.target;
    // e.target = {
    //     duration: 225,  // 当前音频时间长度 
    //     currentTime:0  // 当前播放时间
    // }
    const progressPercent = (currentTime / duration) * 100
    // 进度条
    progress.style.width = `${progressPercent}%`
}
// 设置进度条
function setProgress(e) {
    // progressContainer代理视图宽度
    const width = this.clientWidth
    // 鼠标点击时处于progressContainer里的水平偏移量
    const clickX = e.offsetX

    // audio.duration: 音频长度
    const duration = audio.duration

    // audio.currentTime: 音频播放位置
    audio.currentTime = (clickX / width) * duration
}
// 事件监听
// 1.播放歌曲
playBtn.onclick = function () {
    // 判断当前是否是正在播放

    // const isPlaying = musicContainer.classList.contains('play')
    // if (isPlaying) {
    //     pauseSong()
    // } else {
    //     playSong()
    // }
    musicContainer.classList.contains('play') ? pauseSong() : playSong()
}
// 2.切换歌曲
prevBtn.onclick = prevSong
nextBtn.onclick = nextSong

// 3.播放器进度条相关
// 3.1 设置播放进度
progressContainer.onclick = setProgress
// 3.2 进度条更新
audio.ontimeupdate = updateProgress
// 3.3 歌曲结束后自动下一首
audio.onended = nextSong

 如果有小伙伴想要把他放进自己的网页的话,那就需要用到一点简单的内联框架了,宽度和高度随你想怎么调都行不过得跳到一个可以看到的位置,前面两个属性一个是删除移动框,一个是删除边框。把他放进自己的网页就行了,位置按照你播放器的文件改就行

<iframe scrolling="no" frameborder="0" src="播放器/播放器.html" width="800px" height="300px"></iframe>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值