/* =================================================== * jqlyric.js v0.1 * shawnk@qq.com * 使用方法 * var $container=$('#lyriccontainer'); //用于显示歌词的容器对象,样式自己定义 * $container.jqlyric({ * lyric:'\ [ti:存在] \ [ar:汪峰] \ [al:存在] \ [by:Love] \ [00:00.00]汪峰 - 存在 \ [00:00.68]多少人走着却困在原地 \ [00:07.93]多少人活着却如同死去', // 歌词字符串,标准lrc文件格式 * speed:1000, // 歌词滚动间隔 (毫秒) * getPosition:function(){ // 获取当前播放位置的函数(返回秒数), 请定义外部函数,不指定本参数则默认从调用插件开始自动播放 * return position; * } * }); * =================================================== * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ========================================================== */ (function($) { var gtime=0; var lyric_listener; $.fn.jqlyric = function(options) { var opts = $.extend({}, $.fn.jqlyric.defaults, options); return this.each(function(){ var o = $.meta ? $.extend({}, opts, $this.data()) : opts; var $this=$(this); var arrLyric=new Array(); //放存汉字的歌词 var arrTime=new Array(); //存放时间 var currentLine=0; //当前活动的歌词行号 // 开始解析歌词 var lyric=o.lyric; lyric=lyric.replace(/\]\n/g,'\]\n\|\|'); lyric=lyric.replace(/\]\s\[/g,'\]\['); lyric=lyric.replace(/\]\[/g,'===='); lyric=lyric.replace(/\[/g,'\|\|\['); lyric=lyric.replace(/====/g,'\]\['); lyric=lyric.replace(/\|\|\|\|\|\|\|\|/g,'\|\|'); lyric=lyric.replace(/\|\|\|\|\|\|/g,'\|\|'); lyric=lyric.replace(/\|\|\|\|/g,'\|\|'); //将歌词解析成数组 var arrly=lyric.split('||'); for(var i=0;iarrTime[j+1]){ temp=arrTime[j]; arrTime[j]=arrTime[j+1]; arrTime[j+1]=temp; temp=arrLyric[j]; arrLyric[j]=arrLyric[j+1]; arrLyric[j+1]=temp; } } } lyric=''; for(var i=0;i'+arrLyric[i]+''; } $this.html('
    '+lyric+'
'); var $box=$this.find('.fixed-lyric-box'); var $list=$this.find('ul.lyric-list'); var height=$box.height(); var lineHeight=$list.height()/arrLyric.length; $list.css({'left':'0px','top':(height-lineHeight)/2+'px'}); clearInterval(lyric_listener); lyric_listener=setInterval(function(){ // 定时检查当前播放进度,作出滚动动作 var pos=o.getPosition(); if(arrTime[currentLine]<=pos){ // 当前显示行号时间小于歌曲进度,需要正常向前移动 for(var i=currentLine;ipos||i+1==arrLyric.length){ var height=$box.height(); currentLine=i; $list.find('li.active').removeClass('active'); $list.find('li#line-'+currentLine).addClass('active'); var topPos=(height-lineHeight)/2-(currentLine)*lineHeight; $list.animate({ 'top':topPos+'px' },400,function(){ }); break; } } }else{ for(var i=currentLine;i>0;i--){ if(arrTime[i-1]