


The most powerful thing about HTML5 is the processing of media files. For example, video playback can be achieved by using a simple vedio tag. Similarly, there is a corresponding tag for processing audio files in HTML5, that is, the audio tag
HTML5 has been out for so long, but the audio tag in it has only been used once, and of course it is just to insert this tag. to the page. This time I just took advantage of helping a friend to create a few pages and practice using the audio tag.
First you need to insert an audio tag into the page. Note that it is best not to set loop='loop' here. This attribute is used to set loop playback, because when you need to use the ended attribute later, if loop is set to If looping, the ended attribute will always be false.
autoplay='autoplay' sets the page to automatically play music after loading. The preload and autoplay attributes have the same effect. If the autoplay attribute appears in the tag, the preload attribute will be ignored.
controls='controls' sets the control bar for displaying music.
- audio src="music/Yesterday Once More .mp3" id="aud" autoplay="autoplay" controls="controls" preload="auto">
- Your browser does not support the audio attribute, please change the browser to browse.
- audio>
After you have this tag, congratulations, your page can now play music. But this would make the page too monotonous, so I added some things to the page so that the lyrics can be displayed on the page synchronously and the music to be played can also be selected. So first to achieve this effect, we have to download some lyrics files in lrc format, and then you need to format the music. Because the music file at the beginning looked like this
We need to insert each lyric into a two-digit array. After formatting, the lyrics will become like this.
Attached here is the code for formatting lyrics
- //Lyrics synchronization part
- function parseLyric(text) {
- //Separate the text into line by line and store it in the array
- var lines = text.split('n'),
- //Regular expression used to match time, the matching result is similar to [xx:xx.xx]
- pattern = /[d{2}:d{2}.d{2}]/g,
- //Array to save the final result
- result = [];
- //Remove lines without time
- while (!pattern.test(lines[0])) {
- lineslines = lines.slice(1);
- };
- //When using 'n' to generate the array above, the last element in the result is an empty element, which will be removed here
- lines[lines.length - 1].length === 0 && lines.pop();
- lines.forEach(function(v /*array element value*/ , i /*element index*/ , a /*array itself*/ ) {
- //Extract time [xx:xx.xx]
- var time = v.match(pattern),
- //Extract lyrics
- vvalue = v.replace(pattern, '');
- //Because there may be multiple times in one line, time may be in the form of [xx:xx.xx][xx:xx.xx][xx:xx.xx], which needs to be further separated
- time.forEach(function(v1, i1, a1) {
- //Remove the square brackets in the time to get xx:xx.xx
- var t = v1.slice(1, -1).split(':');
- //Push the result into the final array
- result.push([parseInt(t[0], 10) * 60 parseFloat(t[1]), value]);
- });
- });
- //Finally, sort the elements in the result array by time so that the lyrics can be displayed normally after saving
- result.sort(function(a, b) {
- return a[0] - b[0];
- });
- return result;
- }
At this point we can easily use the lyrics of each piece of music. We need a function to obtain the lyrics and display them on the page synchronously, so that the music can be switched normally. The code is attached below.
- function fn(sgname){
- $.get('music/' sgname '.lrc',function(data){
- var str=parseLyric(data);
- for(var i=0,li;istr.length;i ){
- li=$('li>' str[i][1] 'li>');
- $('#gc ul').append(li);
- }
- $('#aud')[0].ontimeupdate=function(){//video Triggered when the current playback position of the audio changes
- for (var i = 0, l = str.length; i l; i ) {
- if (this.currentTime /*Current playback time*/ > str[i][0]) {
- //Display to page
- $('#gc ul').css('top',-i*40 200 'px'); //Move the lyrics up
- $('#gc ul li').css('color','#fff');
- $('#gc ul li:nth-child(' (i 1) ')').css('color','red'); //Highlight which lyric is currently being played
- }
- }
- if(this.ended){ //Determine whether the currently playing music has finished playing
- var songslen=$('.songs_list li').length;
- for(var i= 0,val;isongslen;i ){
- val=$('.songs_list li:nth-child(' (i 1) ')').text();
- if(val==sgname){
- i=(i==(songslen-1))?1:i 2;
- sgname=$('.songs_list li:nth-child(' i ')').text(); //Switch after the music is finished playing A piece of music
- $('#gc ul').empty(); //Clear lyrics
- $('#aud').attr('src','music/' sgname '.mp3'); fn(sgname);
- return;
- }
- }
- }
- };
- });
- } fn($('.songs_list li:nth-child(1)').text());
HTML code
- div class="songs_cnt">
- ul class="songs_list">
- li>Yesterday Once Moreli>
- li>You Are Beautifulli>
- ul>
- button class="sel_song">点br/>br/>我button>
- div>
- div id="gc">
- ul>ul>
- div>
css代码
- #gc{
- width: 400px;
- height: 400px;
- background: transparent;
- margin: 100px auto;
- color: #fff;
- font-size: 18px;
- overflow: hidden;
- position: relative;
- }
- #gc ul{
- position: absolute;
- top: 200px;
- }
- #gc ul li{
- text-align: center;
- height: 40px;
- line-height: 40px;
- }
- .songs_cnt{
- float: left;
- margin-top: 200px;
- position: relative;
- }
- .songs_list{
- background-color: rgba(0,0,0,.2);
- border-radius: 5px;
- float: left;
- width: 250px;
- padding: 15px;
- margin-left: -280px;
- }
- .songs_list li{
- height: 40px;
- line-height: 40px;
- font-size: 16px;
- color: rgba(255,255,255,.8);
- cursor: pointer;
- }
- .songs_list li:hover{
- font-size: 20px;
- color: rgba(255,23,140,.6);
- }
- .sel_song{
- position: absolute;
- top: 50%;
- width: 40px;
- height: 80px;
- margin-top: -40px;
- font-size: 16px;
- text-align: center;
- background-color: transparent;
- border: 1px solid #2DCB70;
- font-weight: bold;
- cursor: pointer;
- border-radius: 3px;
- font-family: sans-serif;
- transition:all 2s;
- -moz-transition:all 2s;
- -webkit-transition:all 2s;
- -o-transition:all 2s;
- }
- .sel_song:hover{
- color: #fff;
- background-color: #2DCB70;
- }
- .songs_list li.active{
- color: #f00;
- }
js代码
- $('.songs_list li:nth-child(1)').addClass('active');
- $('.songs_cnt').mouseenter(function () {
- var e=event||window.event;
- var tag= e.target||e.srcElement;
- if(tag.nodeName=='BUTTON'){
- $('.songs_list').animate({'marginLeft':'0px'},'slow');
- }
- });
- $('.songs_cnt').mouseleave(function () {
- $('.songs_list').animate({'marginLeft':'-280px'},'slow');
- });
- $('.songs_list li').each(function () {
- $(this).click(function () {
- $('#aud').attr('src','music/' $(this).text() '.mp3');
- $('#gc ul').empty();
- fn($(this).text());
- $('.songs_list li').removeClass('active');
- $(this).addClass('active');
- });
- })
好了,到了这里,那么你的这个歌词同步的效果的一些功能差不多都有了,关于HTML5使用Audio标签实现歌词同步的效果今天也就到这里了。更多信息请登录脚本之家网站了解更多!

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

因为html5不基于SGML(标准通用置标语言),不需要对DTD进行引用,但是需要doctype来规范浏览器的行为,也即按照正常的方式来运行,因此html5只需要写doctype即可。“!DOCTYPE”是一种标准通用标记语言的文档类型声明,用于告诉浏览器编写页面所用的标记的版本。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version
