JS - music box (finished)

JS

document.querySelector("ul.player").addEventListener("click", function(e) {
    /* giving this element an ID. Now we can just check the document for the 
    existence of a player and if it does exist then I can do stuff to it. */
    var audioPlayer = document.querySelector("#player");
    if(audioPlayer) {
        var chosenSongSrc = e.target.getAttribute("data-src");
        if(chosenSongSrc == audioPlayer.src) {  // continue original song
            if(audioPlayer.paused) { 
                e.target.id = "playing";
                audioPlayer.play();
            } else {
                e.target.id = "paused";
                audioPlayer.pause();
            }           
        } else {  // switch song
            audioPlayer.pause();
            audioPlayer.src = chosenSongSrc;
            audioPlayer.play();
            if(document.querySelector("#playing")) {
                document.querySelector("#playing").id = "";
            } else {
                document.querySelector("#paused").id = "";
            }
            e.target.id = "playing";
        }

    } else {
        var audio = document.createElement("audio");
        audio.src = e.target.getAttribute("data-src");
        audio.id = "player";   // the audio element that plays music
        document.body.appendChild(audio);
        e.target.id = "playing";  // the thing you click on
        audio.play();
    
        audio.addEventListener('ended', function() {
            audio.parentNode.removeChild(audio);
            e.target.id = "";
        }, false);
    }

}, false);
body {
	font: normal 15px/15px Helvetica;
	background: #259286;
}

ul.player {
	width: 180px;
	margin: 0 auto;
	margin-top: 100px;
	list-style: none;
}

ul.player li {
	border-bottom: 1px solid #999;
	color: #444;
	padding: 9px 5px 10px 40px;
	background: url(images/media_btn.png) no-repeat 8px 7px;
	background-color: #EAE3CB;
	background-position: 9px 4px;
	cursor: pointer;
}

ul.player li:first-child {
	-webkit-border-top-left-radius: 10px;
	-webkit-border-top-right-radius: 10px;
	border-top: none;
}

ul.player li:last-child {
	-webkit-border-bottom-left-radius: 10px;
	-webkit-border-bottom-right-radius: 10px;
	border-bottom: none;
}

ul.player li:hover {
	background-color: #475B62;
	color: #FCF4DC;
}

ul.player li#playing {
	background: url(images/media_play_btn.png) no-repeat 8px 7px;
	background-color: #FCF4DC;
	color: #2176C7;
	font-weight: bold;
	background-position: 9px 4px;
}

ul.player li#paused {
	background: url(images/media_pause_btn.png) no-repeat 8px 7px;
	background-color: #666;
	color: #FFF;
	font-weight: bold;
	background-position: 9px 4px;
}

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Events</title>
	<link rel="stylesheet" href="style.css" />
</head>
<body>
	<ul class="player">
		<li data-src="audio/Phoebex.mp3">Phoebex</li>
		<li data-src="audio/AmazingLee.mp3">AmazingLee</li>
		<li data-src="audio/NightKitty.mp3">Night Kitty</li>
		<li data-src="audio/EqueKenox.mp3">Eque Kenox</li>
		<li data-src="audio/Shiloah.mp3">Shiloah</li>
	</ul>
<script src="script.js"></script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值