Table of Contents
Create the face
Home Web Front-end H5 Tutorial 三天学会HTML5 ——多媒体元素的使用

三天学会HTML5 ——多媒体元素的使用

May 17, 2016 am 09:06 AM

  目录

  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>
Copy after login

  可以观察到的是video 标签中包含“Controls”,添加该标签可以使得播放器工具栏可见。Control bar 和我们平常所见到的一样,非常简单,包含暂停,播放,停止等按钮。

  注意:

  要确保video 和html 文件存放到同一目录下。如果想放置在不同的目录下,需要设置src 属性。

  HTML5 Video 元素只支持MP4,webm,3gpp,m4v mpeg,ogg ,quicktime,x-ms-wmvright格式。

  输出:

三天学会HTML5 ——多媒体元素的使用

  2. 使用脚本控制Video 元素

  1. 创建HTML 页面

  新建HTML 页面“Media01.html”设置Video 资源 src属性。在本节中不使用Controls 属性来设置,使用JS代码来实现。

<video width="500px" id="vid">
<source src="vid.mp4" />
</video>
Copy after login

  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" />
Copy after login

  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";
}
}
Copy after login

  设置CurrentTime为6,则表示在第六秒时视频停止播放。

function Stop()
{
var v = document.getElementById('vid');
v.pause();
v.currentTime = 6;
document.getElementById('BtnPlay').value = "Play";
}
Copy after login

  如下是设置当视频播放完成之后停止播放:

function End()
{
var v = document.getElementById('vid');
v.pause();
v.currentTime = v.duration;
document.getElementById('BtnPlay').value = "Play";
}
Copy after login

  以下代码是将声音调节控制到0-1之间:

function ChangeVolume(element)
{
var v = document.getElementById('vid');
v.volume = element.value;//For mute set it to 0
}
Copy after login

  输出:

三天学会HTML5 ——多媒体元素的使用

  3. Audio 元素

  HTML5使得在页面中加载音频元素变得非常简单。

  1. 准备音频资源

  2. 新建HTML页面,输入以下内容:

<audio id="audctrl" controls>
<source src="aud.mp3" type="audio/mp3" />
</audio>
Copy after login

  3. 输出:

三天学会HTML5 ——多媒体元素的使用

  4. 使用脚本添加音频元素

  1.新建HTML页面

<audio id="audctrl">
<source src="aud.mp3" type="audio/mp3" />
</audio>
Copy after login

  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" />
Copy after login

  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";
}
Copy after login

  5. 拖拽操作的实现

  在之前,实现拖拽操作都是开发人员自定义逻辑来实现,但是HTML5提供了拖拽API ,使得拖拽操作的实现变得如此简单。

  1. 准备资源(图片资源)

  2. 设置draggable 属性

<img  src="/static/imghw/default1.png"  data-src="fish.png"  class="lazy"   style="max-width:90%" draggable="true" id="img11" ondragstart="drag(event)" / alt="三天学会HTML5 ——多媒体元素的使用" >
Copy after login

  3. 输出

三天学会HTML5 ——多媒体元素的使用

  4. 实现drag 事件

function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
Copy after login

  5. drog 操作

<div id="div1" class="bowl" ondrop="drop(event)" ondragover="allowDrop(event)">
</div>
Copy after login

  输出:

三天学会HTML5 ——多媒体元素的使用

  ondragover 事件制定被拖拽的数据。

function allowDrop(ev) {
ev.preventDefault();
}
Copy after login

  当拖拽的元素被鼠标释放时,自动调用ondrop 事件

function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
Copy after login

  输出:


三天学会HTML5 ——多媒体元素的使用三天学会HTML5 ——多媒体元素的使用

  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/imghw/default1.png"  data-src="eye1.png"  class="lazy" alt="eye" draggable="true" id="eye1" ondragstart="drag(event)" />
<img src="/static/imghw/default1.png"  data-src="eye2.png"  class="lazy" alt="eye" draggable="true" id="eye2" ondragstart="drag(event)" />
<img src="/static/imghw/default1.png"  data-src="nose2.png"  class="lazy" alt="nose" draggable="true" id="nose2" ondragstart="drag(event)" />
<img src="/static/imghw/default1.png"  data-src="eye4.png"  class="lazy" alt="eye" draggable="true" id="eye4" ondragstart="drag(event)" />
<img src="/static/imghw/default1.png"  data-src="nose1.png"  class="lazy" alt="nose" draggable="true" id="nose1" ondragstart="drag(event)" />
<img src="/static/imghw/default1.png"  data-src="eye3.png"  class="lazy" alt="eye" draggable="true" id="eye3" ondragstart="drag(event)" />
<img src="/static/imghw/default1.png"  data-src="smile1.png"  class="lazy" alt="smile" draggable="true" id="smile1" ondragstart="drag(event)" />
<img src="/static/imghw/default1.png"  data-src="smile3.png"  class="lazy" alt="smile" draggable="true" id="smile2" ondragstart="drag(event)" />
<img src="/static/imghw/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/imghw/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>
Copy after login

  输出:

三天学会HTML5 ——多媒体元素的使用

  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));
}
}
Copy after login

  运行:

三天学会HTML5 ——多媒体元素的使用

  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>
Copy after login

  执行:

三天学会HTML5 ——多媒体元素的使用

  如何实现自定更新位置信息呢?

  1. 初始化

<input type="button" value="Get My Location Updated" />
Copy after login

  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?
}
}
Copy after login

  持续更新位置信息

  JS代码:

function stopUpdatingLocation() {
if (navigator.geolocation) {
navigator.geolocation.clearWatch(watchid);
}
}
Copy after login

  输出:

三天学会HTML5 ——多媒体元素的使用

  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>
Copy after login

  运行:

三天学会HTML5 ——多媒体元素的使用

来源:http://www.cnblogs.com/powertoolsteam/p/5207818.html?utm_source=tuicool&utm_medium=referral
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How to make h5 click icon How to make h5 click icon Apr 06, 2025 pm 12:15 PM

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

What Does H5 Refer To? Exploring the Context What Does H5 Refer To? Exploring the Context Apr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

What is the H5 programming language? What is the H5 programming language? Apr 03, 2025 am 12:16 AM

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the &lt;canvas&gt; tag to draw graphics or using JavaScript to control interaction behavior.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

How to make pop-up windows with h5 How to make pop-up windows with h5 Apr 06, 2025 pm 12:12 PM

H5 pop-up window creation steps: 1. Determine the triggering method (click, time, exit, scroll); 2. Design content (title, text, action button); 3. Set style (size, color, font, background); 4. Implement code (HTML, CSS, JavaScript); 5. Test and deployment.

See all articles