Vue实现简单的音乐播放器

在这里插入图片描述

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>音乐播放器</title>
    <style>
        *{
            margin-left: 0px;
            padding-left: 0px;
        }
        ul li{
            list-style: none;
            line-height: 30px;
            color:#999999;
        }
        ul li.active{
            background-color: indianred;
            color: #FFFFFF;
        }
        audio{
            width: 100%;
        }
    </style>
</head>
<body>
<div id="music">
    <!--@ended事件,当媒体播放完成 会自动调用该方法,执行该脚本,自动播放下一首-->
    <audio :src="musicData[currentIndex].songSrc" controls autoplay @ended = 'nextHanlder'></audio>
    <ul>
        <!-- 循环歌名,展示 -->
        <li @click = 'songHandler(index)' v-for = '(item,index) in musicData' :key="item.id" :class = '{active:index===currentIndex}'>
           {{item.id}}.{{ item.author }}-{{ item.name }}
        </li>
    </ul>
</div>
<script src="js/vue.js"></script>
<script>
    var musicData = [
        {
	        id: 1,
	        name: '遇见',
	        author: '孙燕姿',
	        songSrc: 'static/孙燕姿 - 遇见.mp3'
        },
        {
            id: 2,
            name: '斑马,斑马',
            author: '宋冬野',
            songSrc: 'static/宋冬野 - 斑马,斑马.mp3'
        },
        {
            id: 3,
            name: '夜空中最亮的星',
            author: '逃跑计划',
            songSrc: 'static/逃跑计划 - 夜空中最亮的星.mp3'
        },
        {
            id: 4,
            name: '匆匆那年',
            author: '王菲',
            songSrc: 'static/王菲 - 匆匆那年.mp3'
        }
    ];
    new Vue({
        el: '#music',
        data() {
            return {
                musicData:[],
                currentIndex:0
            }
        },
        methods:{
            //点击那首歌播放哪一首
            songHandler(i){
                this.currentIndex = i;
            },
            //播放下一首
            nextHanlder(){
                this.currentIndex++;
            }
        },
        created(){
           //赋值变量
            this.musicData = musicData
        }
    })
</script>
</body>
</html>

欢迎大家阅读,本人见识有限,写的博客难免有错误或者疏忽的地方,还望各位指点,在此表示感激不尽。文章持续更新中…

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐小侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值