相關技能 HTML5+CSS3(實現頁面佈局和動態效果) Iconfont(使用矢量圖標庫添加播放器相關圖標) LESS (動態CSS編寫) jQuery(快速編寫js腳本) gulp+webpack(自動化構建工具,實現LESS,CSS,JS等編譯和壓縮代碼) 實現的功能 播放暫停(點擊切換播放狀 ...
相關技能
HTML5+CSS3
(實現頁面佈局和動態效果)Iconfont
(使用矢量圖標庫添加播放器相關圖標)LESS
(動態CSS編寫)jQuery
(快速編寫js腳本)gulp+webpack
(自動化構建工具,實現LESS,CSS,JS等編譯和壓縮代碼)
實現的功能
- 播放暫停(點擊切換播放狀態)
- 下一曲(切換下一首)
- 隨機播放(當前歌曲播放完自動播放下一曲)
- 單曲迴圈(點擊隨機播放圖標可切換成單曲迴圈)
- 音量調節(滑鼠移入滑動設置音量大小)
- 歌曲進度條(可點擊切換進度直接跳,也可以點擊小圓點拖拽切換進度)
- 實時歌詞(點擊詞,切換歌詞界面,根據實時進度自動滾動歌詞)
- 喜歡(點擊添加了一個active效果)
- 分享(可以直接分享到新浪微博)
audio 標簽使用
autoplay
自動播放loop
迴圈播放volume
音量設置currentTime
當前播放位置duration
音頻的長度pause
暫停play
播放ended
返迴音頻是否已結束
播放和暫停代碼
_Music.prototype.playMusic = function(){
var _this = this;
this.play.on('click', function(){
if (_this.audio.paused) {
_this.audio.play();
$(this).html('');
} else {
_this.audio.pause();
$(this).html('')
}
});
}
音樂進度條代碼
_Music.prototype.volumeDrag = function(){
var _this = this;
this.btn.on('mousedown', function(){
_this.clicking = true;
_this.audio.pause();
})
this.btn.on('mouseup', function(){
_this.clicking = false;
_this.audio.play();
})
this.progress.on('mousemove click', function(e){
if(_this.clicking || e.type === 'click'){
var len = $(this).width(),
left = e.pageX - $(this).offset().left,
volume = left / len;
if(volume <= 1 || volume >= 0){
_this.audio.currentTime = volume * _this.audio.duration;
_this.progressLine.css('width', volume *100 +'%');
}
}
});
}
歌詞添加代碼
_Music.prototype.readyLyric = function(lyric){
this.lyricBox.empty();
var lyricLength = 0;
var html = '<div class="lyric-ani" data-height="20">';
lyric.forEach(function(element,index) {
var ele = element[1] === undefined ? '^_^歌詞錯誤^_^' : element[1];
html += '<p class="lyric-line" data-id="'+index+'" data-time="' + element[0] + '"> ' + ele + ' </p>';
lyricLength++;
});
html += '</div>';
this.lyricBox.append(html);
this.onTimeUpdate(lyricLength);
}
代碼還有很多就不一一添加了,覺得還行的話可以點下喜歡(也可以在我的GitHub給個Star),你的喜歡和Star是我繼續創作的動力,非常感謝!!!源碼加群
學習過程中遇到什麼問題或者想獲取學習資源的話,歡迎加入學習交流群
343599877,我們一起學前端!