目录
1. HTML5 Media-Video
2. HTML5 Media-Audio
3. 拖拽操作
4. 获取位置信息
5. 使用Google 地图获取位置信息
多媒体是互联网中的最重要的一部分,无论访问的是哪种类型的网页,视频或音频触手可及,在之前实现这些功能对开发人员来说可能非常痛苦,必须依赖Object 标签,调用第三方软件来加载,如Flash等,如果有些设备不支持Flash,我们对此就束手无策了。但是HTML5的出现让多媒体网页开发变得异常简单,也形成了新的标准。
1. 使用Video 元素。
在本节中学习如何在HTML5中使用Video 元素
1.准备视频资源
2. 创建HTML 页面
新建HTML ,并命名为“Media.html”,输入以下内容:
<font size="3"><video controls width="500px" id="vid"> <source src="vid.mp4" /> </video></font>
可以观察到的是video 标签中包含“Controls”,添加该标签可以使得播放器工具栏可见。Control bar 和我们平常所见到的一样,非常简单,包含暂停,播放,停止等按钮。
注意:
要确保video 和html 文件存放到同一目录下。如果想放置在不同的目录下,需要设置src 属性。
HTML5 Video 元素只支持MP4,webm,3gpp,m4v mpeg,ogg ,quicktime,x-ms-wmvright格式。
输出:
2. 使用脚本控制Video 元素
1. 创建HTML 页面
新建HTML 页面“Media01.html”设置Video 资源 src属性。在本节中不使用Controls 属性来设置,使用JS代码来实现。
<video width="500px" id="vid"> <source src="vid.mp4" /> </video>
2. 添加播放,暂停,和声音调节按钮。
<input type="button" name="name" value="Play" id="BtnPlay" /> <input type="button" name="name" value="Stop" id="btnStop" /> <input type="button" name="name" value="End" id="btnEnd" /> <input type="range" name="name" value="0.1" min="0" max="1" step="0.1" id="slideVolume" />
3. 创建JS 函数来控制Video播放。
function PlayOrPause() { var v = document.getElementById('vid'); if (v.paused || v.ended) { v.play(); document.getElementById('BtnPlay').value = "Pause"; } else { v.pause(); document.getElementById('BtnPlay').value = "Play"; } }
设置CurrentTime为6,则表示在第六秒时视频停止播放。
function Stop() { var v = document.getElementById('vid'); v.pause(); v.currentTime = 6; document.getElementById('BtnPlay').value = "Play"; }
如下是设置当视频播放完成之后停止播放:
function End() { var v = document.getElementById('vid'); v.pause(); v.currentTime = v.duration; document.getElementById('BtnPlay').value = "Play"; }
以下代码是将声音调节控制到0-1之间:
function ChangeVolume(element) { var v = document.getElementById('vid'); v.volume = element.value;//For mute set it to 0 }
输出:
3. Audio 元素
HTML5使得在页面中加载音频元素变得非常简单。
1. 准备音频资源
2. 新建HTML页面,输入以下内容:
<audio id="audctrl" controls> <source src="aud.mp3" type="audio/mp3" /> </audio>
3. 输出:
4. 使用脚本添加音频元素
1.新建HTML页面
<audio id="audctrl"> <source src="aud.mp3" type="audio/mp3" /> </audio>
2. 添加播放,暂停及音量键
<innput type="button" name="name" value="Play" id="BtnPlay" /> <input type="button" name="name" value="Stop" id="btnStop" /> <input type="button" name="name" value="End" id="btnEnd" /> <input type="range" name="name" value="0.1" min="0" max="1" step="0.1" id="slideVolume" />
3. 创建JS 函数来控制音频播放。代码如下:
function PlayOrPause() { var v = document.getElementById('audctrl'); if (v.paused || v.ended) { v.play(); document.getElementById('BtnPlay').value = "Pause"; } else { v.pause(); document.getElementById('BtnPlay').value = "Play"; } } 同上,设置在第6秒停止播放: function Stop() { var v = document.getElementById('audctrl'); v.pause(); v.currentTime = 6; document.getElementById('BtnPlay').value = "Play"; }
5. 拖拽操作的实现
在之前,实现拖拽操作都是开发人员自定义逻辑来实现,但是HTML5提供了拖拽API ,使得拖拽操作的实现变得如此简单。
1. 准备资源(图片资源)
2. 设置draggable 属性
<img src="/static/imghwm/default1.png" data-src="fish.png" class="lazy" style="max-width:90%" draggable="true" id="img11" ondragstart="drag(event)" / alt="三天学会HTML5 ——多媒体元素的使用" >
3. 输出
4. 实现drag 事件
function drag(ev) { ev.dataTransfer.setData("text", ev.target.id); }
5. drog 操作
<div id="div1" class="bowl" ondrop="drop(event)" ondragover="allowDrop(event)"> </div>
输出:
ondragover 事件制定被拖拽的数据。
function allowDrop(ev) { ev.preventDefault(); }
当拖拽的元素被鼠标释放时,自动调用ondrop 事件
function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); ev.target.appendChild(document.getElementById(data)); }
输出:
6. 复杂的拖拽操作实现
新建HTML页面,HTML & Css 代码如下:
<style> body { cursor: pointer; text-align: center; } .divdrag { position: relative; border: 0px solid rgba(0, 0, 0, .25); width: 300px; height: 300px; padding: 10px 10px10px10px; float: left; } .face { background-image: url('face.jpg'); background-repeat: no-repeat; width: 424px; height: 510px; border: 1px dotted grey; padding: 0 0 0 0; } .facetr td { text-align: center; border: 1px dotted #f7ecec; } </style> <h2 id="Create-the-face">Create the face</h2> <div class="divdrag"> <img src="/static/imghwm/default1.png" data-src="eye1.png" class="lazy" alt="eye" draggable="true" id="eye1" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="eye2.png" class="lazy" alt="eye" draggable="true" id="eye2" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="nose2.png" class="lazy" alt="nose" draggable="true" id="nose2" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="eye4.png" class="lazy" alt="eye" draggable="true" id="eye4" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="nose1.png" class="lazy" alt="nose" draggable="true" id="nose1" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="eye3.png" class="lazy" alt="eye" draggable="true" id="eye3" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="smile1.png" class="lazy" alt="smile" draggable="true" id="smile1" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="smile3.png" class="lazy" alt="smile" draggable="true" id="smile2" ondragstart="drag(event)" /> <img src="/static/imghwm/default1.png" data-src="smile2.png" class="lazy" alt="smile" draggable="true" id="smile3" ondragstart="drag(event)" /> </div> <div style="float:left;"> <a href="DragnDrop.html" title="Click here to reset" style="text-decoration:none;"> <img src="/static/imghwm/default1.png" data-src="direction.png" class="lazy" style="max-width:90%" style="max-width:90%" onclick="" / alt="三天学会HTML5 ——多媒体元素的使用" > </a> </div> <div id="div1" style="width:300px;height:300px;float:left;"> <table class="face"> <tr> <td colspan="2" style="width:100%;"> </td> </tr> <tr> <td colspan="2" style="width:100%;"> </td> </tr> <tr> <td id="eye" style="width:50%" ondrop="drop(event)" ondragover="allowDrop(event)"></td> <td id="eye" style="width:50%" ondrop="drop(event)" ondragover="allowDrop(event)"></td> </tr> <tr> <td id="nose" ondrop="drop(event)" ondragover="allowDrop(event)" colspan="2"></td> </tr> <tr> <td id="smile" ondrop="drop(event)" ondragover="allowDrop(event)" colspan="2"></td> </tr> </table> </div>
输出:
JS 代码:
function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.effectAllowed = 'copy'; ev.dataTransfer.setData("text", ev.target.id); } function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); if (data.indexOf(ev.target.id) == -1) { ev.dataTransfer.clearData(); } else { ev.target.appendChild(document.getElementById(data)); } }
运行:
7. 地理位置信息的获取
HTML5 可以共享位置信息,精度和维度都可以通过JS事件来捕捉并返回给服务器来在google 地图中定位。
初始化:
1. 创建html 页面 Geolocation.html;
2. 添加页面元素:
JS 代码:
<script type=”text/Javascript”> var x = document.getElementById("lblDisplay"); function getLocation() { document.getElementById("header").value = "Static Location"; if (navigator.geolocation) { var opt = { timeout: 6000, maximumAge: 60000, enableHighAccuracy: true }; navigator.geolocation.getCurrentPosition(showPosition, errorCallBack, opt); } else { alert('No support for geolocation'); } } function showPosition(position) { x.innerHTML = "Latitude: " + position.coords.latitude + "Longitude: " + position.coords.longitude; } function errorCallBack(e) { switch (e) { case e.PERMISSION_DENIED: x.innerHTML = "User denied geolocation request"; break; case e.POSITION_UNAVAILABLE: x.innerHTML = "No position information available"; break; case e.TIMEOUT: x.innerHTML = "Timeout occured"; break; case e.UNKNOWN_ERROR: x.innerHTML = "Unknown error"; break; } } </script>
执行:
如何实现自定更新位置信息呢?
1. 初始化
<input type="button" value="Get My Location Updated" />
2. JS代码
varwatchid; function getUpdatedLocation() { document.getElementById("header").value = "Dynamic Location"; if (navigator.geolocation) { var opt = { timeout: 500, maximumAge: 1000, enableHighAccuracy: true }; watchid = navigator.geolocation.watchPosition(showPosition, errorCallBack, opt); } else { // no native support; maybe try a fallback? } }
持续更新位置信息
JS代码:
function stopUpdatingLocation() { if (navigator.geolocation) { navigator.geolocation.clearWatch(watchid); } }
输出:
7. 使用Google地图
1. 创建HTML 页面
2. 添加GOOGLE 地图的引用
3. 添加div 元素,并加载地图
4. 添加点击按钮来加载地图并输入目的地
5. js 代码:
<script type="text/javascript"> function GetMyDirection() { if (navigator.geolocation) { var opt = { timeout: 500, maximumAge: 1000, enableHighAccuracy: true }; navigator.geolocation.getCurrentPosition(showPosition, errorCallBack, opt); } else { alert('No support for geolocation'); } } function showPosition(position) { showInMap(position.coords.latitude, position.coords.longitude); } function showInMap(lat, lang) { vardirectionsService = new google.maps.DirectionsService(); vardirectionsRenderer = new google.maps.DirectionsRenderer(); var route = { origin: new google.maps.LatLng(lat, lang), destination: document.getElementById('txtDestination').value, travelMode: google.maps.DirectionsTravelMode.DRIVING }; varmapOptions = { zoom: 10, center: new google.maps.LatLng(50.8504500, 4.3487800),mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("divmap"), mapOptions); directionsRenderer.setMap(map); directionsRenderer.setPanel(document.getElementById("divDriveDirection")); directionsService.route(route, function (result, status) { if (status === google.maps.DirectionsStatus.OK) { directionsRenderer.setDirections(result); } }); } function errorCallBack(e) { switch (e) { case e.PERMISSION_DENIED: x.innerHTML = "User denied geolocation request"; break; case e.POSITION_UNAVAILABLE: x.innerHTML = "No position information available"; break; case e.TIMEOUT: x.innerHTML = "Timeout occured"; break; case e.UNKNOWN_ERROR: x.innerHTML = "Unknown error"; break; } } </script>
运行:

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.


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 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

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

Zend Studio 13.0.1
Powerful PHP integrated development environment
