用jquery寫一個屬于自己的音樂播放器

看到一個用css3實現的CD的動畫,演示在這兒http://codepen.io/_kieran/pen/QNRmep

突然那我就想說給自己做一個音樂播放器吧,說做就做。演示在https://echolsx.github.io/music/

Github傳送門:https://github.com/EchoLsx/music

圖片描述

主要代碼:

<div id="background"></div>
<div id="player">

<div class="cover"></div>
<div class="ctrl"><div class="tag"><strong>Title</strong><span class="artist">Artist</span><span class="album">Album</span></div><div class="control"><div class="left"><div class="rewind icon"></div><div class="playback icon"></div><div class="fastforward icon"></div></div><div class="volume right"><div class="mute icon left"></div><div class="slider left"><div class="pace"></div></div></div></div><div class="progress"><div class="slider"><div class="loaded"></div><div class="pace"></div></div><div class="timer left">0:00</div><div class="right"><div class="repeat icon"></div><div class="shuffle icon"></div></div></div></div>
</div>
<ul id="playlist"></ul>

JS部分:

   (function($){// Settingsvar repeat = localStorage.repeat || 0,shuffle = localStorage.shuffle || 'false',continous = true,autoplay = true,playlist = [{title: 'Lost Stars',artist: 'Adam Levine',album: 'Begin Again (Music From and Inspired By the Original Motion Picture)',cover:'img/1.jpg',mp3: 'mp3/Adam Levine - Lost Stars.mp3',ogg: ''},{title: 'Color Blind',artist: 'Matt B',album: 'LOVE AND WAR',cover: 'img/8.jpg',mp3: 'mp3/Matt B - Color Blind.mp3',ogg: ''},];// Load playlistfor (var i=0; i<playlist.length; i++){var item = playlist[i];$('#playlist').append('<li>'+item.artist+' - '+item.title+'</li>');}var time = new Date(),currentTrack = shuffle === 'true' ? time.getTime() % playlist.length : 0,trigger = false,audio, timeout, isPlaying, playCounts;var play = function(){audio.play();$('.playback').addClass('playing');timeout = setInterval(updateProgress, 500);isPlaying = true;}var pause = function(){audio.pause();$('.playback').removeClass('playing');clearInterval(updateProgress);isPlaying = false;}// Update progressvar setProgress = function(value){var currentSec = parseInt(value%60) < 10 ? '0' + parseInt(value%60) : parseInt(value%60),ratio = value / audio.duration * 100;$('.timer').html(parseInt(value/60)+':'+currentSec);$('.progress .pace').css('width', ratio + '%');$('.progress .slider a').css('left', ratio + '%');}var updateProgress = function(){setProgress(audio.currentTime);}// Progress slider$('.progress .slider').slider({step: 0.1, slide: function(event, ui){$(this).addClass('enable');setProgress(audio.duration * ui.value / 100);clearInterval(timeout);}, stop: function(event, ui){audio.currentTime = audio.duration * ui.value / 100;$(this).removeClass('enable');timeout = setInterval(updateProgress, 500);}});// Volume slidervar setVolume = function(value){audio.volume = localStorage.volume = value;$('.volume .pace').css('width', value * 100 + '%');$('.volume .slider a').css('left', value * 100 + '%');}var volume = localStorage.volume || 0.5;$('.volume .slider').slider({max: 1, min: 0, step: 0.01, value: volume, slide: function(event, ui){setVolume(ui.value);$(this).addClass('enable');$('.mute').removeClass('enable');}, stop: function(){$(this).removeClass('enable');}}).children('.pace').css('width', volume * 100 + '%');$('.mute').click(function(){if ($(this).hasClass('enable')){setVolume($(this).data('volume'));$(this).removeClass('enable');} else {$(this).data('volume', audio.volume).addClass('enable');setVolume(0);}});// Switch trackvar switchTrack = function(i){if (i < 0){track = currentTrack = playlist.length - 1;} else if (i >= playlist.length){track = currentTrack = 0;} else {track = i;}$('audio').remove();loadMusic(track);if (isPlaying == true) play();}// Shuffle
var shufflePlay = function(){var time = new Date(),lastTrack = currentTrack;currentTrack = time.getTime() % playlist.length;if (lastTrack == currentTrack) ++currentTrack;switchTrack(currentTrack);
}// Fire when track ended
var ended = function(){pause();audio.currentTime = 0;playCounts++;if (continous == true) isPlaying = true;if (repeat == 1){play();} else {if (shuffle === 'true'){shufflePlay();} else {if (repeat == 2){switchTrack(++currentTrack);} else {if (currentTrack < playlist.length) switchTrack(++currentTrack);}}}
}var beforeLoad = function(){var endVal = this.seekable && this.seekable.length ? this.seekable.end(0) : 0;$('.progress .loaded').css('width', (100 / (this.duration || 1) * endVal) +'%');
}// Fire when track loaded completely
var afterLoad = function(){if (autoplay == true) play();
}// Load track
var loadMusic = function(i){var item = playlist[i],newaudio = $('<audio>').html('<source src="'+item.mp3+'"><source src="'+item.ogg+'">').appendTo('#player');$('.cover').html('<img src="'+item.cover+'" alt="'+item.album+'">');$('.tag').html('<strong>'+item.title+'</strong><span class="artist">'+item.artist+'</span><span class="album">'+item.album+'</span>');$('#playlist li').removeClass('playing').eq(i).addClass('playing');audio = newaudio[0];audio.volume = $('.mute').hasClass('enable') ? 0 : volume;audio.addEventListener('progress', beforeLoad, false);audio.addEventListener('durationchange', beforeLoad, false);audio.addEventListener('canplay', afterLoad, false);audio.addEventListener('ended', ended, false);
}loadMusic(currentTrack);
$('.playback').on('click', function(){if ($(this).hasClass('playing')){pause();} else {play();}
});
$('.rewind').on('click', function(){if (shuffle === 'true'){shufflePlay();} else {switchTrack(--currentTrack);}
});
$('.fastforward').on('click', function(){if (shuffle === 'true'){shufflePlay();} else {switchTrack(++currentTrack);}
});
$('#playlist li').each(function(i){var _i = i;$(this).on('click', function(){switchTrack(_i);});
});if (shuffle === 'true') $('.shuffle').addClass('enable');
if (repeat == 1){$('.repeat').addClass('once');
} else if (repeat == 2){$('.repeat').addClass('all');
}$('.repeat').on('click', function(){if ($(this).hasClass('once')){repeat = localStorage.repeat = 2;$(this).removeClass('once').addClass('all');} else if ($(this).hasClass('all')){repeat = localStorage.repeat = 0;$(this).removeClass('all');} else {repeat = localStorage.repeat = 1;$(this).addClass('once');}
});$('.shuffle').on('click', function(){if ($(this).hasClass('enable')){shuffle = localStorage.shuffle = 'false';$(this).removeClass('enable');} else {shuffle = localStorage.shuffle = 'true';$(this).addClass('enable');}});
})(jQuery);

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/458278.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/458278.shtml
英文地址,請注明出處:http://en.pswp.cn/news/458278.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

四年一閏 隨筆

今天日子比較特殊&#xff0c;碰到閏年的2月29日。好久沒有記錄隨筆了&#xff0c;今天隨便記上幾筆吧 1、上家公司居然沒幫我交社保&#xff0c;一整年了&#xff0c;發工資時還照扣社保的錢。。。現在說會補差額給我&#xff0c;算下來一年XXXX&#xff0c;也只是個數字&…

Qt 串口類QSerialPort 使用筆記

Qt 串口類QSerialPort 使用筆記雖然現在大多數的家用PC機上已經不提供RS232接口了。但是由于RS232串口操作簡單、通訊可靠&#xff0c;在工業領域中仍然有大量的應用。Qt以前的版本中&#xff0c;沒有提供官方的對RS232串口的支持&#xff0c;編寫串口程序很不方便。現在好了&a…

什么是H標簽?H1,H2,H3標簽?以及和strong標簽使用的方法及重要性

大家都知道&#xff0c;seo的一個很重要的一點就是要把網站做的條理清晰&#xff0c;讓搜索引擎很容易的讀明白&#xff0c;這個條理清晰不僅體現在網站的物理路徑&#xff0c;url等地 方。在<h1><h2><h3>等方面也是這樣。并不是<h1>對于關鍵字排名有幫…

OC語言中的便利初始化函數和便利構造器

便利遍歷初始化函數與便利構造器&#xff08;以Student類為例&#xff09;&#xff1b; main函數 Student.h&#xff08;聲明&#xff09; 。。。。。。。。。。。。。。。。。。。 Student.m&#xff08;實現&#xff09; 。。。。。。。。。。。。。。。。。 轉載于:https://…

MySQL 性能監控 4 大指標

【編者按】本文作者為 John Matson&#xff0c;主要介紹 mysql 性能監控應該關注的 4 大指標。 文章系國內 ITOM 管理平臺 OneAPM 編譯呈現。 MySQL 是什么&#xff1f; MySQL 是現而今最流行的開源關系型數據庫服務器。由 Oracle 所有&#xff0c;MySQL 提供了可以免費下載的社…

【深度相機系列四】深度相機原理揭秘--結構光(iPhone X 齊劉海原理)

from&#xff1a;https://blog.csdn.net/electech6/article/details/78707839導讀 結構光法&#xff1a;為解決雙目匹配問題而生 深度圖效果&#xff1a;結構光vs.雙目 投射圖案的編碼方式直接編碼時分復用編碼空分復用編碼 Kinect1原理 iPhone X原深感相機是縮小版的更強大的K…

iOS開發中對于一些常用的相對路徑(持續更新)

1.iOS開發的證書的描述文件放置地點 ~/Library/MobileDevice/Provisioning Profiles 2.$(SRCROOT)代表的是這個項目文件夾所在的位置 $(PROJECT_DIR) 表示的包含可執行文件的哪一個文件夾 3.對于pod導入的第三方庫&#xff0c;引用不自動補全問題。選擇target -> BuildSet…

Android倒計時工具類

為什么80%的碼農都做不了架構師&#xff1f;>>> 原文地址:http://my.oschina.net/reone/blog/710003 多謝touch_ping 的回應. 原來api有這個類 android.os.CountDownTimer , 具體實現很下面的差不多. import android.content.Context; import android.os.Handler…

深度相機原理揭秘--雙目立體視覺

歡迎關注計算機視覺life&#xff01;導讀 為什么非得用雙目相機才能得到深度&#xff1f; 雙目立體視覺深度相機的工作流程 雙目立體視覺深度相機詳細工作原理理想雙目相機成像模型極線約束圖像矯正技術基于滑動窗口的圖像匹配基于能量優化的圖像匹配 雙目立體視覺深度相機的優…

微信掃碼支付模式一和模式二的區別

http://www.baidu.com/link?urlAj_xhOM5Q6rpZXkTMBPq4o0UbCO4eLq0esX8B3K2v06bkRS8F8lC4k06rv-3uZARLLTEKJHMhwzI_cdcJiHfqK&wd&eqid904bc71f000181740000000356d7d9bf https://www.zhihu.com/question/35818812/answer/66086727 知乎頁面訪問存在502 Bad Gateway問題…

雙目視覺幾何框架詳解(玉米專欄8篇匯總)

一、圖像坐標&#xff1a;我想和世界坐標談談(A) 玉米竭力用輕松具體的描述來講述雙目三維重建中的一些數學問題。希望這樣的方式讓大家以一個輕松的心態閱讀玉米的《計算機視覺學習筆記》雙目視覺數學架構系列博客。這個系列博客旨在捋順一下已標定的雙目視覺中的數學主線。數…

(原)Ubuntu14中安裝GraphicsMagick

轉載請注明出處&#xff1a; http://www.cnblogs.com/darkknightzh/p/5661439.html 參考網址&#xff1a; http://comments.gmane.org/gmane.comp.video.graphicsmagick.core/514 http://www.graphicsmagick.org/INSTALL-unix.html https://github.com/clementfarabet/graphics…

js全局函數

1.parseInt(String,radix):返回轉換成整數的值。 注意&#xff1a;當參數radix的值為0&#xff0c;或者沒有設置這個參數&#xff0c;parseInt()會根據string來判斷數字的基數。 當忽略radix&#xff0c;JavaScript默認數字的基數規則為&#xff1a; 1.如果string以0x開頭&…

android之自定義廣播

布局文件 點擊按鈕發送廣播 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orientation"vertical" android:layout_width"match_parent&qu…

世界坐標系和相機坐標系,圖像坐標系的關系

from&#xff1a;https://blog.csdn.net/waeceo/article/details/50580607一、四個坐標系簡介和轉換相機模型為以后一切標定算法的關鍵&#xff0c;只有這邊有相當透徹的理解&#xff0c;對以后的標定算法才能有更好的理解。本人研究了好長時間&#xff0c;幾乎每天都重復看幾遍…

PythonOCC 3D圖形庫學習—創建立方體模型

Open CASCADE&#xff08;簡稱OCC&#xff09;平臺是是一個開源的C類庫&#xff0c;OCC主要用于開發二維和三維幾何建模應用程序&#xff0c;包括通用的或專業的計算機輔助設計CAD系統、制造或分析領域的應用程序、仿真應用程序或圖形演示工具。 PythonOCC是對Open CASCADE的封…

Struts2、SpringMVC、Servlet(Jsp)性能對比 測試 。 Servlet的性能應該是最好的,可以做為參考基準,其它測試都要向它看齊,參照...

2019獨角獸企業重金招聘Python工程師標準>>> Struts2、SpringMVC、Servlet(Jsp)性能對比 測試 。 Servlet的性能應該是最好的&#xff0c;可以做為參考基準&#xff0c;其它測試都要向它看齊&#xff0c;參照它。 做為一個程序員&#xff0c;對于各個框架的性能要有…

深度相機(三)--三種方案對比

from&#xff1a;https://blog.csdn.net/app_12062011/article/details/52511701RGBD方案對比&#xff1a;關鍵技術規格&#xff1a; 1.檢測范圍&#xff1b; 2.檢測精度&#xff1b; 3.檢測角度&#xff1b; 4.幀率。 5.模塊大小 6.功耗 目前主流的深度攝像頭方案在檢測距離上…

Activity隱式啟動IntentFilter

時間&#xff1a;2016年3月4日09:54:02IntentFilter過濾信息&#xff1a;action、category、dataaction&#xff1a;1.Intent中的action必須能夠和過濾條件中的action匹配&#xff0c;過濾條件中可以有多個action。2.Intent中如果設置多個action則過濾條件中必須至少有同樣數量…

基于圖像分割的立體匹配方法

1.緒論 立體匹配是三維重建系統的關鍵步驟&#xff0c;并且作為一種非接觸測量方法在工業以及科研領域具有重要的應用價值。為了完成匹配工作以及獲取場景的稠密視差圖&#xff0c;可以通過構建能量函數對應立體匹配的約束條件。復雜能量函數的全局最優解通常是NP難問題。相對于…