Skip to content

Commit d3a2f09

Browse files
committed
原生js
1 parent df068ea commit d3a2f09

13 files changed

Lines changed: 152 additions & 0 deletions

File tree

‎音乐播放器/css/style.css‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.music {
2+
width: 200px;
3+
height: 300px;
4+
background: #333;
5+
border-radius: 5px;
6+
box-shadow: 3px -3px 3px #666;
7+
position: relative;
8+
}
9+
.music .screen {
10+
height:70%;
11+
width:96%;
12+
background: #ccc;
13+
margin-left:2%;
14+
margin-top: 2%;
15+
}
16+
17+
.music .buttons {
18+
height:25%;
19+
width:96%;
20+
background: wheat;
21+
margin-left:2%;
22+
margin-top: 2%;
23+
}
24+
.music .screen img{
25+
width: 100px;
26+
height: 100px;
27+
margin-top: 50px;
28+
margin-left: 40px;
29+
cursor: pointer;
30+
transform:rotate(30deg);
31+
-webkit-transform:rotate(30deg);
32+
-o-transform:rotate(30deg);
33+
-ms-transform:rotate(30deg);
34+
-moz-transform:rotate(30deg);
35+
}
36+
.music .buttons img {
37+
width: 45px;
38+
height: 45px;
39+
margin-left: 10px;
40+
margin-top: 15px;
41+
cursor: pointer;
42+
border-radius: 8px;
43+
}
44+
.music .buttons img:hover{
45+
background-color: rgba(0, 0, 0, 0.13);
46+
}

‎音乐播放器/img/1.svg‎

Lines changed: 1 addition & 0 deletions
Loading

‎音乐播放器/img/netx1.svg‎

Lines changed: 1 addition & 0 deletions
Loading

‎音乐播放器/img/next.svg‎

Lines changed: 1 addition & 0 deletions
Loading

‎音乐播放器/img/pause.svg‎

Lines changed: 1 addition & 0 deletions
Loading

‎音乐播放器/img/start.svg‎

Lines changed: 1 addition & 0 deletions
Loading

‎音乐播放器/index.html‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>音乐小站</title>
6+
<link rel="stylesheet" href="css/style.css">
7+
</head>
8+
<body>
9+
<div id="music" class="music">
10+
<div class="screen">
11+
<img src="img/1.svg" alt="">
12+
</div>
13+
<div class="buttons">
14+
<img id="prev" src="img/netx1.svg" alt="">
15+
<img id="start" src="img/start.svg" alt="">
16+
<img id="next" src="img/next.svg" alt="">
17+
</div>
18+
</div>
19+
</body>
20+
<script src="js/main.js"></script>
21+
</html>

‎音乐播放器/js/main.js‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//元素剧中的方法
2+
function _center(dom){
3+
dom.style.position="absolute";
4+
dom.style.top="50%";
5+
dom.style.left="50%";
6+
dom.style.marginTop=-dom.offsetHeight/2+"px";
7+
dom.style.marginLeft=-dom.offsetWidth/2+"px";
8+
}
9+
function dom(id){
10+
if(id.toString().indexOf("#")!=1){
11+
id=id.replace("#","");
12+
}
13+
return document.getElementById(id);
14+
}
15+
var musicDom=dom("#music");
16+
_center(musicDom);
17+
//元素居中方法结束
18+
var i=0;
19+
//封装操作音乐
20+
var musicBox={
21+
musicDom:null,
22+
songs:[],
23+
init:function(){
24+
this.musicDom=document.createElement('audio');
25+
},
26+
add:function(src){
27+
this.songs.push(src);
28+
},
29+
play:function(index){
30+
this.musicDom.src=this.songs[index];
31+
this.musicDom.play();
32+
},
33+
stop:function(){
34+
this.musicDom.pause();
35+
},
36+
next:function(){
37+
this.play(i+1);
38+
},
39+
prev:function(){
40+
this.play(i-1);
41+
}
42+
}
43+
//添加两首歌曲
44+
musicBox.add("mp3/1.mp3");
45+
musicBox.add("mp3/2.mp3");
46+
musicBox.add("mp3/3.mp3");
47+
musicBox.add("mp3/4.mp3");
48+
musicBox.add("mp3/5.mp3");
49+
50+
//点击播放或者暂停
51+
var oStart=document.getElementById("start");
52+
musicBox.init();
53+
var onOff = true;
54+
oStart.onclick=function() {
55+
if(onOff){
56+
oStart.src="img/pause.svg";
57+
musicBox.play(i);
58+
onOff=false;
59+
}
60+
else {
61+
oStart.src="img/start.svg";
62+
onOff=true;
63+
musicBox.stop();
64+
}
65+
}
66+
//点击播放或暂停结束
67+
68+
//点击播放下一首
69+
var oNext=document.getElementById("next");
70+
oNext.onclick=function() {
71+
musicBox.next();
72+
i++;
73+
}
74+
//点击播放上一首
75+
var oPrev=document.getElementById("prev");
76+
oPrev.onclick=function() {
77+
musicBox.prev();
78+
}
79+
80+

‎音乐播放器/mp3/1.mp3‎

2.62 MB
Binary file not shown.

‎音乐播放器/mp3/2.mp3‎

3.1 MB
Binary file not shown.

0 commit comments

Comments
 (0)