基于jQuery實(shí)現(xiàn)歌詞滾動(dòng)版音樂(lè)播放器的代碼
先給大家看下效果圖,感興趣的朋友可以參考實(shí)現(xiàn)代碼
核心代碼如下所示:
$.ajax({ url: "/music/music.txt", type: "get", success: function(data) { data = jQuery.parseJSON(data); var length = data.length; var now=0; for (i = 0; i < length; i++) { $("#musicText li").eq(i).after("<li>" + data[i].text + "</li>") } var player = { playButton: $(".play"), songText: $(".musicText"), state: 0, //0播放,1暫停 audio: $("#audio").get(0), bind: function() { //綁定按鈕 //播放或暫停 console.log($.type(this)) console.log($.type(this)) var obj = this; this.playButton.click(function() { obj.changeState(obj.state ? 0 : 1); }); //設(shè)置聲音 $("#voice").click(function(ex) { var percent = (ex.clientX - $(this).offset().left) / $(this).width(); obj.setVoice(percent); }); //默認(rèn)聲音 0.8 obj.setVoice(1.0); //靜音 $("#voiceOP").click(function() { if (obj.muted) { $(this).removeClass("muted"); obj.audio.muted = false; obj.muted = false; } else { $(this).addClass("muted"); obj.audio.muted = true; obj.muted = true; } }); //設(shè)置進(jìn)度 $("#MusicProgress").click(function(ex) { var percent = (ex.clientX - $(this).offset().left) / $(this).width(); obj.setProgress(percent, false); }); //上一首 $("#prev").click(function() { obj.nowIndex--; if (obj.nowIndex < 0) obj.nowIndex = obj.list.length - 1; obj.playSing(obj.nowIndex); }); //下一首 $("#next").click(function() { obj.nowIndex++; if (obj.nowIndex >= obj.list.length) obj.nowIndex = 0; obj.playSing(obj.nowIndex); player.audio.play(); }); //綁定事件 - 播放時(shí)間改變 this.audio.ontimeupdate = function() { obj.timeChange(); } //播放結(jié)束 this.audio.onended = function() { obj.singEnd(); } }, //切換播放狀態(tài) changeState: function(_state) { this.state = _state; if (!this.state) { this.playButton.removeClass("pause").addClass("play"); this.pause(); } else { this.playButton.removeClass("play").addClass("pause"); this.play(); } }, //播放 play: function() { this.audio.play(); }, //暫停 pause: function() { this.audio.pause(); }, timeChange: function() { var nowSec = Math.floor(this.audio.currentTime); console.log(nowSec) console.log(data[now].time) if(nowSec>data[now].time){ now = now + 1; console.log(now) $("#musicText li").eq(now).addClass("active").siblings("li").removeClass("active"); $("#musicText").css("top",-(24*now)+138) } var totalSec = Math.floor(this.audio.duration); //當(dāng)前進(jìn)度顯示 var secTip = secFormat(nowSec) + "/" + secFormat(totalSec); if (secTip.length == 11) $("#secTip").html(secTip); this.setProgress(nowSec / totalSec, true); }, setVoice: function(percent) { $("#voice").children(".bar").css("width", percent * 100 + "%"); $("#voice").children("a").css("left", percent * 100 + "%"); this.audio.volume = percent; }, setProgress: function(percent, justCss) { $("#MusicProgress").children(".bar").css("width", percent * 100 + "%"); $("#MusicProgress").children("a").css("left", percent * 100 + "%"); if (!justCss) this.audio.currentTime = this.audio.duration * percent; }, singEnd: function() { if (this.style == 0) { this.nowIndex++; if (this.nowIndex >= this.list.length) this.nowIndex = 0; this.playSing(this.nowIndex); } else { var index = Math.floor(Math.random() * (this.list.length + 1)) - 1; index = index < 0 ? 0 : index; index = index >= this.list.length ? (this.list.length - 1) : index; this.playSing(index); this.nowIndex = index; } }, }; player.bind(); function secFormat(num) { var m = Math.floor(num / 60); var s = Math.floor(num % 60); return makeFormat(m) + ":" + makeFormat(s); function makeFormat(n) { if (n >= 10) return n; else { return "0" + n; } } } } })
然后這里的代碼是alpha0.0.1版的,一直在升級(jí)ing.
以上所述是小編給大家介紹的基于jQuery實(shí)現(xiàn)歌詞滾動(dòng)版音樂(lè)播放器的代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的。
相關(guān)文章
jQuery控制input只能輸入數(shù)字和兩位小數(shù)的方法
這篇文章主要介紹了jQuery控制input只能輸入數(shù)字和兩位小數(shù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05jQuery EasyUI開(kāi)發(fā)技巧總結(jié)
這篇文章主要介紹了jQuery EasyUI開(kāi)發(fā)技巧總結(jié)的相關(guān)資料,希望通過(guò)本文大家能夠掌握EasyUI的開(kāi)發(fā)技巧,需要的朋友可以參考下2017-09-09jquery插件uploadify實(shí)現(xiàn)帶進(jìn)度條的文件批量上傳
這篇文章主要介紹了jquery插件uploadify實(shí)現(xiàn)帶進(jìn)度條的文件批量上傳,感興趣的小伙伴們可以參考一下2015-12-12jquery html添加元素/刪除元素操作實(shí)例詳解
這篇文章主要介紹了jquery html添加元素/刪除元素操作,結(jié)合實(shí)例形式詳細(xì)分析了jquery html添加元素/刪除元素相關(guān)函數(shù)功能、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05c#+jquery實(shí)現(xiàn)獲取radio和checkbox的值
這篇文章主要介紹了c#結(jié)合jquery實(shí)現(xiàn)獲取radio和checkbox的value值的方法,將代碼分享給大家,需要的朋友可以參考下2014-12-12jQuery使用JSONP實(shí)現(xiàn)跨域獲取數(shù)據(jù)的三種方法詳解
這篇文章主要介紹了jQuery使用JSONP實(shí)現(xiàn)跨域獲取數(shù)據(jù)的三種方法,結(jié)合實(shí)例形式對(duì)比分析了jsonp跨域獲取數(shù)據(jù)的3種常用操作技巧,需要的朋友可以參考下2017-05-05jquery實(shí)現(xiàn)簡(jiǎn)單每周輪換的日歷
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)簡(jiǎn)單每周輪換的日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09