Skip to content

Commit

Permalink
原生js
Browse files Browse the repository at this point in the history
  • Loading branch information
NewAllRed committed Aug 18, 2017
1 parent df068ea commit d3a2f09
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 音乐播放器/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.music {
width: 200px;
height: 300px;
background: #333;
border-radius: 5px;
box-shadow: 3px -3px 3px #666;
position: relative;
}
.music .screen {
height:70%;
width:96%;
background: #ccc;
margin-left:2%;
margin-top: 2%;
}

.music .buttons {
height:25%;
width:96%;
background: wheat;
margin-left:2%;
margin-top: 2%;
}
.music .screen img{
width: 100px;
height: 100px;
margin-top: 50px;
margin-left: 40px;
cursor: pointer;
transform:rotate(30deg);
-webkit-transform:rotate(30deg);
-o-transform:rotate(30deg);
-ms-transform:rotate(30deg);
-moz-transform:rotate(30deg);
}
.music .buttons img {
width: 45px;
height: 45px;
margin-left: 10px;
margin-top: 15px;
cursor: pointer;
border-radius: 8px;
}
.music .buttons img:hover{
background-color: rgba(0, 0, 0, 0.13);
}
1 change: 1 addition & 0 deletions 音乐播放器/img/1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions 音乐播放器/img/netx1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions 音乐播放器/img/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions 音乐播放器/img/pause.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions 音乐播放器/img/start.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions 音乐播放器/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>音乐小站</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="music" class="music">
<div class="screen">
<img src="img/1.svg" alt="">
</div>
<div class="buttons">
<img id="prev" src="img/netx1.svg" alt="">
<img id="start" src="img/start.svg" alt="">
<img id="next" src="img/next.svg" alt="">
</div>
</div>
</body>
<script src="js/main.js"></script>
</html>
80 changes: 80 additions & 0 deletions 音乐播放器/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//元素剧中的方法
function _center(dom){
dom.style.position="absolute";
dom.style.top="50%";
dom.style.left="50%";
dom.style.marginTop=-dom.offsetHeight/2+"px";
dom.style.marginLeft=-dom.offsetWidth/2+"px";
}
function dom(id){
if(id.toString().indexOf("#")!=1){
id=id.replace("#","");
}
return document.getElementById(id);
}
var musicDom=dom("#music");
_center(musicDom);
//元素居中方法结束
var i=0;
//封装操作音乐
var musicBox={
musicDom:null,
songs:[],
init:function(){
this.musicDom=document.createElement('audio');
},
add:function(src){
this.songs.push(src);
},
play:function(index){
this.musicDom.src=this.songs[index];
this.musicDom.play();
},
stop:function(){
this.musicDom.pause();
},
next:function(){
this.play(i+1);
},
prev:function(){
this.play(i-1);
}
}
//添加两首歌曲
musicBox.add("mp3/1.mp3");
musicBox.add("mp3/2.mp3");
musicBox.add("mp3/3.mp3");
musicBox.add("mp3/4.mp3");
musicBox.add("mp3/5.mp3");

//点击播放或者暂停
var oStart=document.getElementById("start");
musicBox.init();
var onOff = true;
oStart.onclick=function() {
if(onOff){
oStart.src="img/pause.svg";
musicBox.play(i);
onOff=false;
}
else {
oStart.src="img/start.svg";
onOff=true;
musicBox.stop();
}
}
//点击播放或暂停结束

//点击播放下一首
var oNext=document.getElementById("next");
oNext.onclick=function() {
musicBox.next();
i++;
}
//点击播放上一首
var oPrev=document.getElementById("prev");
oPrev.onclick=function() {
musicBox.prev();
}


Binary file added 音乐播放器/mp3/1.mp3
Binary file not shown.
Binary file added 音乐播放器/mp3/2.mp3
Binary file not shown.
Binary file added 音乐播放器/mp3/3.mp3
Binary file not shown.
Binary file added 音乐播放器/mp3/4.mp3
Binary file not shown.
Binary file added 音乐播放器/mp3/5.mp3
Binary file not shown.

0 comments on commit d3a2f09

Please sign in to comment.