Home >Web Front-end >JS Tutorial >How to implement scrolling playback of lyrics based on JavaScript_javascript skills
Various music players have a function to automatically scroll and play lyrics. The currently scrolled lyrics will be highlighted and centered. Even if the lyrics are wrapped, they can be centered normally. So how can this function be implemented based on JavaScript to scroll the lyrics? Woolen cloth? Please see details below.
Generally, the lyrics format used by music players is lrc. In order to facilitate processing, we use lyrics in XML format here. Introducing a website: Chinese lyrics library. It provides lyrics in xml format.
Let’s take a look at the final effect of this example:
The following is the specific code based on jQuery:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Player</title> <style type="text/css"> #audio-wrapper{ border:1px solid; text-align:center; } .activated{ color:#33b; font-weight:bold; background:#ddf; } #lrc{ text-align:center; width:360px; height:400px; overflow:hidden; border:2px solid #ddd; box-shadow:2px 2px 2px silver; } .lyrics-container{ position:relative; width:99%; height:80%; border:1px solid red; overflow:hidden; } .lyrics-container2{ position:absolute; width:355px; } #lrc p{ text-indent:0; margin:0; padding:6px; } .music-title,.album,.artist{ margin:0; padding:4px; text-indent:0; text-align:left; } </style> <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script> </head> <body> <div id="#audio-wrapper"> <p><audio src="data/aimei.mp3" controls></audio></p> </div> <div id="lrc"></div> <script type="text/javascript"> $(document).ready(function(){ var $lrc = $('#lrc'); var html =''; $('audio').on('play',function(){ var start = new Date(); if($lrc.html() == ''){ $.ajax({ url:'data/aimei.xml', type:'get', dataType:'xml', success:function(data){ html += '<div class="info">'; if($(data).find('TITLE').length > 0){ html += '<p class="music-title">歌曲:' + $(data).find('TITLE').text()+'</p>'; } if($(data).find('ALBUM').length > 0){ html += '<p class="album">专辑:' + $(data).find('ALBUM').text()+'</p>'; } if($(data).find('ARTIST').length > 0){ html += '<p class="artist">演唱:' + $(data).find('ARTIST').text()+'</p>'; } html += '</div>'; html += '<div class="lyrics-container">' html += '<div class="lyrics-container2">' $(data).find('LRC').each(function(){ html += '<p class="lyrics" tag="'+ $(this).attr('TAG') +'">' + $(this).text() +'</p>'; }); html += '</div></div>'; $lrc.html(html); //alert($(data).find('LRC').length); } }); } var timer = setInterval(function(){ var now = new Date(); var elapsed = now - start; if($lrc.find('.lyrics').length){ $lrc.find('.lyrics').each(function(){ var isOK = elapsed - $(this).attr('tag'); if(isOK < 13 && isOK > 0){ $lrc.find('.lyrics').removeClass('activated'); $(this).addClass('activated'); if($(this).prevAll('.lyrics').length > 3){ $('.lyrics-container2').animate({ 'top':'-=30px' }); //console.log($(this).prevAll('.lyrics').length); } } }); } },10); }); }); </script> </body> </html>
The above content is a detailed explanation of how to implement scrolling playback of lyrics based on JavaScript in this article. I hope you like it.