Hongmu Notes
Home Language Notes Web page playing music scrolling lyrics effect
Language Notes JavaScript

Web page playing music scrolling lyrics effect

Web page playing music scrolling lyrics effect

When a webpage plays audio, you can use jQuery and HTML to create a lyric scrolling effect. Here is a simple implementation:

HTML:

<div id="lyrics">
  <p data-time="0:00.00">作词 : 娃娃</p>
  <p data-time="0:01.00">作曲 : 陶喆</p>
  <p data-time="0:04.50">Love... Baby LoveLoveLove baby</p>
  <p data-time="0:12.78"></p>
  <p data-time="0:24.76">请原谅我有决定会让你烦恼</p>
  <p data-time="0:29.29">你要了解并不是你追得我想逃</p>
</div>
<audio id="audio" src="your-audio-file.mp3"></audio>

jQuery:

$(document).ready(function() {
  var audio = $("#audio")[0];
  var lyricDiv = $("#lyrics");

  audio.addEventListener("timeupdate", function() {
    var currentTime = audio.currentTime;
    var lyricP = lyricDiv.find("p");

    lyricP.each(function() {
      var lyricTime = $(this).data("time");
      var lyricText = $(this).text();

      if (currentTime >= convertToSeconds(lyricTime)) {
        $(this).addClass("active").siblings().removeClass("active");
        lyricDiv.animate({scrollTop: $(this).offset().top - lyricDiv.offset().top + lyricDiv.scrollTop() - 100}, 1000);
      }
    });
  });

  function convertToSeconds(time) {
    var parts = time.split(":");
    var minutes = parseInt(parts[0], 10);
    var seconds = parseFloat(parts[1]);
    return minutes * 60 + seconds;
  }
});

CSS:

#lyrics {
  height: 400px;
  overflow: auto;
  text-align: center;
  margin: 0 auto;
}

#lyrics p.active {
  color: red;
}

Explain the above code:

  1. First, add an element to each line of lyrics. data-time Attribute: Represents the time position in the audio file corresponding to this line of lyrics.
  2. In the jQuery code, the value was retrieved. audio The location where elements and lyrics are situated div Element; added one more. timeupdate Event listener: This event is triggered whenever the audio playback time is updated.
  3. In the event listener, first retrieve the current audio playback time, then iterate through each lyric line and determine whether that lyric line should be activated (i.e., whether it should be scrolled to the center of the screen). If so, add it. active Class, using jQuery animate Method: Scroll this line of lyrics to the center of the screen.
  4. Finally, an auxiliary function is defined. convertToSecondsUsed to convert a time string into seconds for comparison purposes.
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

(PC+WAP) Construction Cost Financial Audit Website Template; Enterprise Management Website Source Code Download – 1025

(PC+WAP) Construction Cost Financial Audit Website Template; Enterprise Management Website Source Code Download – 1025 Practical Collection pbootcms Template

A PbootCMS website template for engineering cost estimation and financial auditing as well as enterprise management, compatible with both PC and WAP devices. The professional and rigorous design is ideal for engineering cost estimation and financial auditing firms to showcase their services and case studies. This template helps professional service providers build a strong online presence and attract clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 44
acupuncture treatment

acupuncture treatment Hobbies Memo notes

Acupuncture treatment of the head General headache: Baihui point (nearly selected), Yongquan point Migraine: Hegu on the contralateral side, Taiyanggu on the ipsilateral side Nausea and vomiting: Zhongwan, Neiguan, Zusanli, Gongsun Post-auricular pain: First pain: Tianjing Long-term pain: Zhongzhu Forehead pain: Zhongwan point Eyebrow bone pain: one inch below the contralateral Yinlingquan Pain in the back of the head: Tianzhu First pain: Shugu point Long-term pain: Weizhong point. Brain tumor: whole body shakes, trembles, and spasms. Prescription (Whole Scorpion, Centipede) to calm wind and relieve spasm. Next...
👁 233
Internet Information Data Security Protection Website Template 0575

Internet Information Data Security Protection Website Template 0575 Practical Collection Yiyou template

An eYouCMS website template designed for the internet information, data security, and protection industries. Its modern, tech-driven design effectively showcases data security products, information security services, solutions, and technical expertise. This template helps cybersecurity companies present their brand online and attract corporate clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: EasyYouCMS Installation Guide...
👁 43
(Adaptive mobile version) Responsive ceramic grinding disc equipment – PBootCMS website template; Download source code for polishing equipment websites – 0321

(Adaptive mobile version) Responsive ceramic grinding disc equipment – PBootCMS website template; Download source code for polishing equipment websites – 0321 Practical Collection pbootcms Template

An adaptive, responsive ceramic grinding disc and polishing equipment website template for the PbootCMS platform. This professional, precision-designed template is ideal for showcasing grinding products, polishing techniques, and industrial applications. It enables abrasive manufacturing enterprises to demonstrate their technical expertise online and attract industrial clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 46
(Adaptive mobile version) Responsive jewelry website template (PBootCMS) – Luxury brand website source code download – 1026

(Adaptive mobile version) Responsive jewelry website template (PBootCMS) – Luxury brand website source code download – 1026 Practical Collection pbootcms Template

A responsive jewelry and luxury goods PbootCMS website template compatible with both PC and WAP devices. Featuring a high-end, luxurious design style, it is ideal for showcasing jewelry collections, luxury brand stories, and products. This template helps luxury brands effectively communicate their brand value online and attract high-net-worth customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn
👁 42
Responsive web design, website promotion and optimization templates – 0576

Responsive web design, website promotion and optimization templates – 0576 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the website development, design, marketing, and SEO optimization industry. Its modern, tech-driven design style makes it perfect for showcasing website development projects, design works, marketing strategies, and SEO optimization services. It helps digital marketing agencies attract corporate clients online. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 61