Due to the popularity of HTML5, audio can now be used to play audio for most needs on mobile terminals. However, you may only need a simple play/stop effect, but the audio styles on different browsers are not satisfactory. So how to change this style? In fact, its principle is relatively simple, that is, do not use the controls attribute when writing audio, hide the native audio, and then use tags such as p to define a css style to beautify it to display the effect of the player. , and finally use js to capture audio events, which are basically src path, pause, load, and play. The following are some related APIs of the audio tag:
Control function function description
load(): Load audio and video software, usually no need to call, unless it is dynamically generated Element, used to preload before playing
play(): Load and play audio and video files. Unless the file has been paused at other locations, playback will restart by default
pause(): Pause audio and video files in the playing state
audio Scriptable property value:
src: audio file path.
autoplay: Set whether the audio plays automatically (the default property is to automatically play loaded media files), or check whether it has been set to autoplay
autobuffer: Set whether to automatically buffer audio when the page loads. If autoplay is set, this feature will be ignored.
loop: Set whether the audio should be played in a loop. , or query whether it has been set to loop
currentTime: Returns the time taken from the start of playback to the present in s. You can also set the value of currentTime to jump to a specific position
controls : Show or hide the user control interface (properties for adding play, pause and volume controls.)
volume: Set the volume value between 0.0 and 1.0, or query the current volume value
muted: Set Whether to mute
Read-only attribute Attribute description
duration: Get the playback duration of the media file, in s, if it cannot be obtained, it will be NaN
paused: If the media file is paused, return true, otherwise return false
ended: If the media file is played, return true
startTime: Return the starting playback time, usually 0.0, unless it is a buffered media file and part of the content is no longer in the buffer
error: The error code returned after an error occurs
currentSrc: as a stringForm returns the file being played or loaded, corresponding to the file selected by the browser in the source element
All mainstream browsers support these attributes. But don't think that there is no compatibility. In audio playback streams, there are two camps. Firefox and Opera support ogg audio, Safari and IE support mp3. Fortunately, Google's chrome supports it.
<p class="btn-audio"><audio id="mp3Btn"><source src="images/audio.mp3" type="audio/mpeg" /></audio></p> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.10.0/jquery.min.js"></script>
body{ background:#2b2938; } .btn-audio{ margin: 90px auto; width: 186px; height: 186px; background:url(images/voice_stop.png) no-repeat center bottom; background-size:cover; }
<script type="text/javascript"> $(function(){ //播放完毕 $('#mp3Btn').on('ended', function() { console.log("音频已播放完成"); $('.btn-audio').css({'background':'url(images/voice_stop.png) no-repeat center bottom','background-size':'cover'}); }) //播放器控制 var audio = document.getElementById('mp3Btn'); audio.volume = .3; $('.btn-audio').click(function() { event.stopPropagation();//防止冒泡 if(audio.paused){ //如果当前是暂停状态 $('.btn-audio').css({'background':'url(images/voice_play.png) no-repeat center bottom','background-size':'cover'}); audio.play(); //播放 return; }else{//当前是播放状态 $('.btn-audio').css({'background':'url(images/voice_stop.png) no-repeat center bottom','background-size':'cover'}); audio.pause(); //暂停 } }); }) </script>
As a technical implementation, its principle is relatively simple, that is, to hide the native audio, then use p to display the effect of the player, and then call its click event to trigger play and stop, and then the duration. , this value can sometimes be obtained, but sometimes it cannot, which is a bit tricky, so it is recommended to customize the duration attribute storage time on the audio tag. At this time, if the component cannot obtain it, it will get this value.
this.settings.target.on('loadedmetadata', function() { _this.duration = _this.audio.duration; if (_this.duration != "Infinity") { _this.durationContent.html(Math.floor(_this.duration) + 's'); } else { var attr = $(_this.settings.target).attr('duration'); if(attr){ _this.durationContent.html($(_this.settings.target).attr('duration')+"s"); }else{ _this.durationContent.html(''); } } });
The above is the detailed content of How to modify the style of tag in html. For more information, please follow other related articles on the PHP Chinese website!

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.

Atom editor mac version download
The most popular open source editor

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.