这两天学习了JavaScript入门,按照老师要求,按照虾米播放器的布局,制作一个动态生成音乐列表的页面,并且支持播放。主要不在于模仿,而在于用JavaScript动态生成列表。
首先看看虾米播放器布局:
整体分为头部head,中部main和底部foot
而头部head又分为logo部分,menu导航栏,search搜索框,和登录框login
所以先编写头部的框架
<!-- 头部 -->
<div class="head">
<!-- logo -->
<div class="logo"><img src="img/LOGO.png" class="logo"></div>
<!-- 导航栏 -->
<div class="nav">
<ul class="menu">
<li><a href="">发现</a></li>
<li><a href="">我的音乐</a></li>
<li><a href="">音乐人</a></li>
<li><a href="">客户端下载</a></li>
<li><a href="">会员中心</a></li>
</ul>
</div>
<!-- 搜索框 -->
<div class="search">
<div class="input">
<input type="text" placeholder="请输入音乐名" name="search" class="input">
</div>
<img src="img/搜索.png" class="search">
</div>
<!-- 登录框 -->
<div class="login">
<span class="login">
<a href="">登录</a>/<a href="">注册</a>
</span>
</div>
</div>
而主体部分又分为左边部分和右半部分,左半部分包括专辑背景和专辑图片
<!-- 左边 -->
<div class="left">
<!-- 歌曲图片 -->
<div class="music-img-box">
<div class="music-img"><img src="" id="music-img"></div>
</div>
<!-- 歌曲信息 -->
<div class="music-info">歌曲名</div>
</div>
右边主要就是list歌曲列表了
<!-- 右边列表 -->
<div class="right">
<!-- 内容页 -->
<div class="main">
</div>
</div>
最后底部加上一个foot栏,并添加audio音频插件
<!-- 底部 -->
<div class="foot">
<audio src="" controls class="audio" id="audio"></audio>
</div>
</div>
编写css样式
div.root{
position: absolute;
width: 90%;
left: 50%;
margin-left: -45%;
}
div.head{
position: relative;
width: 100%;
height: 80px;
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
}
div.logo{
position: relative;
margin-left: 50px;
width: 200px;
height: 100%;
float: left;
}
img.logo{
position: relative;
height: 50px;
padding-top: 17px;
}
div.nav{
position: relative;
height: 100%;
width: 600px;
float: left;
}
ul.menu>li>a{
display: block;
height: 80px;
width: 90px;
line-height: 80px;
text-align: center;
color: gray;
box-sizing: border-box;
float: left;
}
ul.menu>li>a:hover{
color: red;
}
div.search{
position: relative;
width: 250px;
height: 100%;
float: left;
}
div.input{
position: relative;
width: 60%;
height: 40px;
border-bottom: 2px solid rgba(0, 0, 0, 0.2);
top: 50%;
margin-top: -20px;
margin-left: 20px;
float: left;
}
input.input{
position: relative;
width: 100%;
height: 100%;
font-size: 15px;
box-sizing: border-box;
}
img.search{
position: relative;
width: 40px;
top: 50%;
margin-top: -20px;
float: left;
}
div.login{
position: relative;
width: 120px;
height: 100%;
float: left;
}
span.login{
display: block;
position: relative;
width: 80px;
height: 30px;
left: 50%;
top: 50%;
margin-left: -40px;
margin-top: -15px;
}
span.login>a{
color: gray;
}
span.login>a:hover{
color: red;
}
div.left{
clear: both;
position: relative;
width: 30%;
height: 550px;
box-sizing: border-box;
float: left;
}
div.right{
position: relative;
width: 70%;
height: 550px;
box-sizing: border-box;
border-left: 1px solid gray;
float: left;
}
div.music-img-box{
position: relative;
height: 200px;
width: 200px;
background-color: black;
left: 50%;
margin-left: -100px;
margin-top: 50px;
}
div.music-img{
position: absolute;
width: 200px;
height: 140px;
margin-top: 30px;
box-sizing: border-box;
border-top: 3px solid gray;
border-bottom: 3px solid gray;
background-color: black;
}
div.music-info{
position: relative;
width: 200px;
height: 50px;
left: 50%;
margin-left: -100px;
text-align: center;
font-weight: bold;
font-size: 20px;
line-height: 50px;
}
div.main{
position: absolute;
width: 100%;
height: 100%;
overflow-y: scroll;
}
div.main::-webkit-scrollbar{
width: 5px;
background: none;
}
div.main::-webkit-scrollbar-thumb{
width: 5px;
background: rgba(0, 0, 0, 0.5);
border-radius: 2px;
}
div.main>div:nth-child(2n){
background-color: rgba(0, 0, 0, 0.2);
}
div.list{
position: relative;
width: 100%;
height: 50px;
border-bottom: 1px solid gray;
}
div.list:hover{
color: red;
font-weight: bold;
cursor: pointer;
}
div.foot{
clear: both;
position: relative;
width: 100%;
height: 60px;
border-top: 1px solid gray;
}
audio.audio{
position: relative;
width: 100%;
}
span{
position: relative;
line-height: 50px;
margin-left: 30px;
float: left;
}
img.list{
position: relative;
height: 35px;
width: 35px;
margin-top: 10px;
margin-left: 5px;
float: left;
}
img#music-img{
position: relative;
margin: -20px auto 0 auto;
width: 180px;
height: 180px;
border: 2px double gray;
border-radius: 180px;
}
.circle{
animation: circle 20s linear infinite;
}
@keyframes circle{
0%{
transform: rotate(0deg);
}
100%{
transform: rotate(360deg);
}
}
效果图:
然后编写JavaScript脚本,先定义data数据文件
{
"img":'img/1.jpg',
"ablum": "我们不一样",
"artist": "大壮",
"id": 1,
"name": "大壮 - 我们不一样",
"path": "music/music1.mp3",
"size": 10935843,
"style": "流行",
"uid": 8,
"uploadtime": 1551273973000
},
{
"img":'img/2.jpg',
"ablum": "小青蛙",
"artist": "青蛙",
"id": 2,
"name": "青蛙 - 小跳蛙",
"path": "music/music2.mp3",
"size": 3893552,
"style": "民谣,流行",
"uid": 8,
"uploadtime": 1551273992000
},
{
"img":'img/3.jpg',
"ablum": "未知",
"artist": "xx",
"id": 3,
"name": "其实都没有",
"path": "music/music3.mp3",
"size": 9487673,
"style": "流行",
"uid": 8,
"uploadtime": 1551274029000
},
{
"img":'img/4.jpg',
"ablum": "xx",
"artist": "xx",
"id": 4,
"name": "校长 - 带你去旅行",
"path": "musics/8/1551274050945.mp3",
"size": 9135714,
"style": "流行",
"uid": 8,
"uploadtime": 1551274050000
},
{
"img":'img/5.png',
"ablum": "晴天",
"artist": "周杰伦",
"id": 5,
"name": "周杰伦-晴天",
"path": "musics/8/1551274071712.mp3",
"size": 4320631,
"style": "英伦,古典,摇滚",
"uid": 8,
"uploadtime": 1551274071000
},
{
"img":'img/6.jpg',
"ablum": "xx",
"artist": "毛不易",
"id": 6,
"name": "消愁-毛不易",
"path": "musics/8/1551274094026.mp3",
"size": 7176384,
"style": "流行,古典",
"uid": 8,
"uploadtime": 1551274094000
},
{
"img":'img/7.jpg',
"ablum": "xx",
"artist": "毛不易",
"id": 7,
"name": "消愁-毛不易",
"path": "musics/8/1551274094026.mp3",
"size": 7176384,
"style": "流行,古典",
"uid": 8,
"uploadtime": 1551274094000
}
当页面加载完后,执行:
window.onload=function(){
//设置初始图片
document.getElementById('music-img').src = data[0].img;
//有多少歌就循环多少次
for(var i=0;i<data.length;i++){
// 新建一个div.list
var list = document.createElement('div');
list.className='list';
list.value=data[i].path;
//获取本条信息
var temp = new Date(data[i].uploadtime).toLocaleTimeString();
var info='';
info+='\t'+data[i].id;
info+='\t'+data[i].name;
info+='\t'+temp;
// span
var span = document.createElement('span');
span.innerText=info;
//img
var img = document.createElement('img');
img.className='list';
img.src=data[i].img;
//加入到list
list.appendChild(img);
list.appendChild(span);
// // 在内容页追加
document.getElementsByClassName('main')[0].appendChild(list);
//给当前list添加监听
list.addEventListener('click',function(){
document.getElementById('music-img').src = this.querySelector('img').src;
// 设置音乐
document.getElementById('audio').src=this.value;
document.getElementById('audio').play();
})
//监听播放器是否播放
document.getElementById('audio').addEventListener('pause',function(){
document.getElementById('music-img').className='';
})
document.getElementById('audio').addEventListener('play',function(){
document.getElementById('music-img').className ='circle';
})
}
}
最后的效果就是: