利用jquery實現(xiàn)實時更新歌詞的方法
前言
最近自己寫了個移動端音樂播放器,用了很多框架,也踩了很多坑,感覺比較復(fù)雜的就是計算歌詞實時播放了,所以想著大概給整理了一下,分享出來,有問題的話朋友們可以指出,有需要的朋友們也可以學(xué)習(xí)學(xué)習(xí)。
布局
(歌詞都是后臺數(shù)據(jù)獲取,方便理解我把所有歌詞都一一列出來)
<section> <figure> <img id="animat" src="img/img3.jpg"/> </figure> <div class="lyric"> <div class="lrc-list"> <p data-play="audio-t-0" data-index="0">逃跑計劃 </p> <p data-play="audio-t-1" data-index="1">一萬次悲傷 </p> <p data-play="audio-t-2" data-index="2">oh honey </p> <p data-play="audio-t-3" data-index="3">我腦海里全都是你 </p> <p data-play="audio-t-8" data-index="4">oh無法抗拒的心情 </p> <p data-play="audio-t-13" data-index="5">難以呼吸</p> <p data-play="audio-t-15" data-index="6">tonight </p> <p data-play="audio-t-17" data-index="7">是否又要錯過一個夜晚</p> <p data-play="audio-t-23" data-index="8">是否還要掩飾最后的期待 </p> <p data-play="audio-t-29" data-index="9">oh tonight </p> <p data-play="audio-t-30" data-index="10">一萬次悲傷 </p> <p data-play="audio-t-33" data-index="11">依然會有意義</p> <p data-play="audio-t-35" data-index="12">我一直在最溫暖的地方等你</p> <p data-play="audio-t-40" data-index="13">似乎只能這樣停留一個方向</p> <p data-play="audio-t-46" data-index="14">已不能改變</p> <p data-play="audio-t-51" data-index="15">每一顆眼淚是遺忘的光 </p> <p data-play="audio-t-57" data-index="16">最昏暗的地方也變得明亮</p> <p data-play="audio-t-61" data-index="17">我奔涌的暖流尋找你的海洋 </p> <p data-play="audio-t-67" data-index="18">我注定這樣</p> <p data-play="audio-t-73" data-index="19">oh honey </p> <p data-play="audio-t-75" data-index="20">你目光里充滿憂郁 </p> <p data-play="audio-t-81" data-index="21">就像經(jīng)歷一遍飛行難以逃避</p> <p data-play="audio-t-87" data-index="22">tonight </p> <p data-play="audio-t-89" data-index="23">是否還要錯過這個夜晚</p> <p data-play="audio-t-91" data-index="24">是否還要熄滅所有的期待</p> <p data-play="audio-t-95" data-index="25">tonight</p> <p data-play="audio-t-100" data-index="26">一萬次悲傷</p> <p data-play="audio-t-102" data-index="27">依然會有意義</p> <p data-play="audio-t-106" data-index="28">我一直在最后的地方等你</p> <p data-play="audio-t-109" data-index="29">似乎只能這樣</p> <p data-play="audio-t-114" data-index="30">停留一個方向</p> <p data-play="audio-t-120" data-index="31">已不能改變</p> <p data-play="audio-t-123" data-index="32">每一顆眼淚是遺忘的光</p> <p data-play="audio-t-130" data-index="33">最昏暗的地方也變得明亮</p> <p data-play="audio-t-135" data-index="34">我奔涌的暖流尋找你的海洋</p> <p data-play="audio-t-141" data-index="35">我注定這樣</p> <p data-play="audio-t-155" data-index="36">一萬次悲傷 </p> <p data-play="audio-t-189" data-index="37">依然會有意義</p> <p data-play="audio-t-192" data-index="38">我一直在最后的地方等你</p> <p data-play="audio-t-197" data-index="39">似乎只能這樣</p> <p data-play="audio-t-200" data-index="40">停留一個方向 </p> <p data-play="audio-t-204" data-index="41">已不能改變</p> <p data-play="audio-t-208" data-index="42">每一顆眼淚是遺忘的光</p> <p data-play="audio-t-213" data-index="43">最昏暗的地方也變得明亮</p> <p data-play="audio-t-218" data-index="44">我奔涌的暖流尋找你的海洋</p> <p data-play="audio-t-225" data-index="45">我注定這樣</p> <p data-play="audio-t-236" data-index="46">oh honey </p> <p data-play="audio-t-238" data-index="47">我腦海里全都是你 </p> <p data-play="audio-t-243" data-index="48">oh無法抗拒的心情 </p> <p data-play="audio-t-247" data-index="49">難以呼吸</p> </div> </div> </section>
注意 : 給每一句歌詞都自定義兩個屬性,data-play屬性為了計算什么時候顯示該句歌詞,它的屬性值audio-t- “num” 是表示這句歌詞在num秒的時候高亮顯示; data-index為了計算什么時候歌詞往上走,就按順序定義就行。
以下是靜態(tài)效果圖:
(效果圖沒有歌詞是因為左滑才出現(xiàn)歌詞)
左滑出現(xiàn)歌詞
$('figure').on("swipeLeft" , function(){ $('figure').css('display' , 'none'); $('.lyric').css('display' , 'block').addClass("animated slideInRight"); }) $('.lyric').on("swipeRight" , function(){ $('.lyric').css('display' , 'none'); $('figure').css('display' , 'block').addClass("animated slideInLeft"); })
歌詞出現(xiàn):
重點來了
function lyricPlay(){ var timer2; var $cur = null; var $lrcbox = $(".lrc-list"); $lrcwrap = $lrcbox.parent(); clearInterval(timer2); timer2 = setInterval( function(){ var time = Math.ceil( $('audio')[0].currentTime); //currentTime 是獲取音樂實時播放時間 $cur = $("audio-t-"+time).selector;//拿到自定義屬性data-play $('p' , $lrcbox ).each(function(){ //循環(huán)拿到p var playT = $(this).data('play');//當前播放元素屬性值 if( playT == $cur ){ //給符合條件歌詞加上高亮并刪除兄弟元素的class名 $(this).stop().addClass('acitve').siblings().removeClass('acitve'); }else{ return; } var index = $(this).data("index"); //當前元素下標 var lineHeight =$(this).height() ; //一行歌詞高度 var boxHeight = $lrcwrap.height(); //歌詞顯示區(qū)域高度 var screensize = boxHeight / lineHeight; //一屏顯示多少句歌詞 var half = Math.floor(screensize / 2); //半屏歌詞數(shù)量 //當前歌詞超過半屏 if(index > half){ //計算出超過的高度 減去 一行歌詞的高度 var top = (half - index) * lineHeight + lineHeight $lrcbox.css({ "top" : parseInt(top) + "px" }); } }) } , 1000) } lyricPlay();
效果如下:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,當然以上方法都只是我自己的想法,如果不足之處歡迎大家指點,希望本文的內(nèi)容對大家能有一定的幫助。
- jquery實現(xiàn)input輸入框?qū)崟r輸入觸發(fā)事件代碼
- js與jquery實時監(jiān)聽輸入框值的oninput與onpropertychange方法
- JQuery 改變頁面字體大小的實現(xiàn)代碼(實時改變網(wǎng)頁字體大小)
- PHP+jquery實時顯示網(wǎng)站在線人數(shù)的方法
- jquery+ajax實現(xiàn)注冊實時驗證實例詳解
- jQuery選擇頭像并實時顯示的代碼
- jquery購物車實時結(jié)算特效實現(xiàn)思路
- javascript和jQuery實現(xiàn)網(wǎng)頁實時聊天的ajax長輪詢
- 基于Jquery插件Uploadify實現(xiàn)實時顯示進度條上傳圖片
- jquery 表格排序、實時搜索表格內(nèi)容(附圖)
相關(guān)文章
jQuery 循環(huán)遍歷改變a標簽的href(實例講解)
下面小編就為大家?guī)硪黄猨Query 循環(huán)遍歷改變a標簽的href(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Jquery創(chuàng)建一個層當鼠標移動到層上面不消失效果
使用Jquery創(chuàng)建一個層,實現(xiàn)當鼠標移動到層上面不消失,示例代碼如下,感興趣的朋友可以參考下2013-12-12jQuery實現(xiàn)移動端滑塊拖動選擇數(shù)字效果
這篇文章主要介紹了jQuery實現(xiàn)移動端滑塊拖動選擇數(shù)字效果,jQuery實現(xiàn)移動端滑塊拖動效果,感興趣的小伙伴們可以參考一下2015-12-12