update: refactor

This commit is contained in:
qianguyihao
2019-10-01 20:25:16 +08:00
parent 6c80edc0c2
commit 4625c68dba
71 changed files with 0 additions and 0 deletions

890
01-html/01-HTML5详解.md Normal file
View File

@@ -0,0 +1,890 @@
> 本文最初发表于[博客园](https://www.cnblogs.com/smyhvae/p/8424230.html),并在[GitHub](https://github.com/qianguyihao/Web)上持续更新**前端的系列文章**。欢迎在GitHub上关注我一起入门和进阶前端。
> 以下是正文。
## HTML5的介绍
### Web 技术发展时间线
- 1991 HTML
- 1994 HTML2
- 1996 CSS1 + JavaScript
- 1997 HTML4
- 1998 CSS2
- 2000 XHTML1严格的html
- 2002 Tableless Web Design表格布局
- 2005 AJAX
- 2009 HTML5
- 2014 HTML5 Finalized
2002年的表格布局逐渐被淘汰是因为表格是用来承载数据的并不是用来划分网页结构的。
2009年就已经推出了HTML5的草案但直到2014年才有定稿是因为有移动端的推动。
H5草案的前身是叫Web Application最早是由[WHATWG](https://baike.baidu.com/item/WHATWG/5803339?fr=aladdin)这个组织在2004年提出的。
2007年被 W3C 组织接纳,并在 2008-01-22 发布 HTML5 的第一个草案。
### 什么是 HTML5
HTML5并不仅仅只是做为HTML标记语言的一个最新版本更重要的是它**制定了Web应用开发的一系列标准**成为第一个将Web做为应用开发平台的HTML语言。
HTML5定义了一系列新元素如新语义标签、智能表单、多媒体标签等可以帮助开发者创建富互联网应用还提供了一些Javascript API如地理定位、重力感应、硬件访问等可以在浏览器内实现类原生应用。我们甚至可以结合 Canvas 开发网页版游戏。
**`HTML5`的广义概念**HTML5代表浏览器端技术的一个发展阶段。在这个阶段浏览器的呈现技术得到了飞跃发展和广泛支持它包括HTML5、CSS3、Javascript API在内的一套技术组合。
`HTML5`不等于 `HTML next version``HTML5` 包含: `HTML`的升级版、`CSS`的升级版、`JavaScript API`的升级版。
**总结**`HTML5`是新一代开发 **Web 富客户端**应用程序整体**解决方案**。包括HTML5CSS3Javascript API在内的一套**技术组合**。
**富客户端**:具有很强的**交互性**和体验的客户端程序。比如说,浏览博客,是比较简单的客户端;一个在线听歌的网站、即时聊天网站就是富客户端。
**PS**
单纯地从技术的角度讲,兼容性问题只会让开发者徒增烦恼。
如果网页端的程序能做到PC客户端的体验就会对后者构成威胁。
### HTML5 的应用场景
列举几个HTML5 的应用场景:
1极具表现力的网页内容简约而不简单。
2网页应用程序
- 代替PC端的软件iCloud、百度脑图、Office 365等。
- APP端的网页淘宝、京东、美团等。
- 微信端:公众号、小程序等。
3混合式本地应用。
4简单的游戏。
### HTML5 新增的内容
![](http://img.smyhvae.com/20180206_1540.png)
![](http://img.smyhvae.com/20180206_1545.png)
![](http://img.smyhvae.com/20180206_1541.png)
## 语义化的标签
### 语义化的作用
语义标签对于我们并不陌生,如`<p>`表示一个段落、`<ul>`表示一个无序列表。**标签语义化的作用:**
- 能够便于开发者阅读和写出更优雅的代码。
- 同时让浏览器或是网络爬虫可以很好地解析,从而更好分析其中的内容。
- 更好地搜索引擎优化。
总结HTML的职责是描述一块内容是什么或其意义而不是它长什么样子它的外观应该由CSS来决定。
### H5在语义上的改进
在此基础上HTML5 增加了大量有意义的语义标签,更有利于搜索引擎或辅助设备理解 HTML 页面内容。HTML5会让HTML代码的内容更结构化、标签更语义化。
我们常见的 css+div 布局是:
![](http://img.smyhvae.com/20180206_1546.png)
以后我们可以这样写:
![](http://img.smyhvae.com/20180206_1550.png)
传统的做法中,我们通过增加类名如`class="header"``class="footer"`使HTML页面具有语义性但是不具有通用性。
HTML5 则是通过新增语义标签的形式来解决这个问题,例如`<header></header>``<footer></footer>`等,这样就可以使其具有通用性。
**传统网页布局:**
```html
<!-- 头部 -->
<div class="header">
<ul class="nav"></ul>
</div>
<!-- 主体部分 -->
<div class="main">
<!-- 文章 -->
<div class="article"></div>
<!-- 侧边栏 -->
<div class="aside"></div>
</div>
<!-- 底部 -->
<div class="footer">
</div>
```
**H5 的经典网页布局:**
```html
<!-- 头部 -->
<header>
<ul class="nav"></ul>
</header>
<!-- 主体部分 -->
<div class="main">
<!-- 文章 -->
<article></article>
<!-- 侧边栏 -->
<aside></aside>
</div>
<!-- 底部 -->
<footer>
</footer>
```
## H5中常用的新语义标签
- `<nav>` 表示导航
- `<header>` 表示页眉
- `<footer>` 表示页脚
- `<section>` 表示区块
- `<article>` 表示文章。如文章、评论、帖子、博客
- `<aside>` 表示侧边栏 如文章的侧栏
- `<figure>` 表示媒介内容分组。
- `<mark>` 表示标记 (用得少)
- `<progress>` 表示进度 (用得少)
- `<time>` 表示日期
本质上新语义标签与`<div>``<span>`没有区别只是其具有表意性使用时除了在HTML结构上需要注意外其它和普通标签的使用无任何差别可以理解成`<div class="nav">` 相当于`<nav>`
PS单标签不用写关闭符号。
### 新语义标签的兼容性处理
IE8 及以下版本的浏览器不支持 H5 和 CSS3。解决办法引入`html5shiv.js`文件。
引入时需要做if判断具体代码如下
```html
<!-- 条件注释 只有ie能够识别-->
<!--[if lte ie 8]>
<script src="html5shiv.min.js"></script>
<![endif]-->
```
上方代码是**条件注释**虽然是注释但是IE浏览器可以识别出来。解释一下
- lless 更小
- tthan 比
- eequal等于
- ggreat 更大
PS:我们在测试 IE 浏览器的兼容的时候,可以使用软件 ietest模拟IE6-IE11。
在不支持HTML5新标签的浏览器会将这些新的标签解析成行内元素(inline)对待,所以我们只需要将其转换成块元素(block)即可使用。
但是在IE9版本以下并不能正常解析这些新标签但是可以识别通过document.createElement('tagName')创建的自定义标签。于是我们的解决方案就是将HTML5的新标签全部通过document.createElement('tagName')来创建一遍这样IE低版本也能正常解析HTML5新标签了。
当然在实际开发中我们更多采用的办法是检测IE浏览器的版本来加载第三方的JS库来解决兼容问题如上方代码所示
## H5中的表单
传统的Web表单已经越来越不能满足开发的需求HTML5 在 Web 表单方向做了很大的改进,如拾色器、日期/时间组件等,使表单处理更加高效。
### H5中新增的表单类型
- `email` 只能输入email格式。自动带有验证功能。
- `tel` 手机号码。
- `url` 只能输入url格式。
- `number` 只能输入数字。
- `search` 搜索框
- `range` 滑动条
- `color` 拾色器
- `time` 时间
- `date` 日期。
- `--datetime` 时间日期
- `month` 月份
- `week` 星期
上面的部分类型是针对移动设备生效的,且具有一定的兼容性,在实际应用当中可选择性的使用。
代码举例:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>表单类型</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #F7F7F7;
}
form {
max-width: 500px;
width: 100%;
margin: 32px auto 0;
font-size: 16px;
}
label {
display: block;
margin: 10px 0;
}
input {
width: 100%;
height: 25px;
margin-top: 2px;
display: block;
}
</style>
</head>
<body>
<form action="">
<fieldset>
<legend>表单类型</legend>
<label for="">
email: <input type="email" name="email" required>
</label>
<label for="">
color: <input type="color" name="color">
</label>
<label for="">
url: <input type="url" name='url'>
</label>
<label for="">
number: <input type="number" step="3" name="number">
</label>
<label for="">
range: <input type="range" name="range" value="100">
</label>
<label for="">
search: <input type="search" name="search">
</label>
<label for="">
tel: <input type="tel" name="tel">
</label>
<label for="">
time: <input type="time" name="time">
</label>
<label for="">
date: <input type="date" name="date">
</label>
<label for="">
datetime: <input type="datetime">
</label>
<label for="">
week: <input type="week" name="month">
</label>
<label for="">
month: <input type="month" name="month">
</label>
<label for="">
datetime-local: <input type="datetime-local" name="datetime-local">
</label>
<input type="submit">
</fieldset>
</form>
</body>
</html>
```
代码解释:
`<fieldset>` 标签将表单里的内容进行打包,代表一组;而`<legend> `标签的则是 fieldset 里的元素定义标题。
### 表单元素(标签)
这里讲两个表单元素。
**1、`<datalist>` 数据列表:**
```html
<input type="text" list="myData">
<datalist id="myData">
<option>本科</option>
<option>研究生</option>
<option>不明</option>
</datalist>
```
上方代码中input里的list属性和 datalist 进行了绑定。
效果:
![](http://img.smyhvae.com/20180206_1845.gif)
上图可以看出,数据列表可以自动提示。
2、`<keygen>`元素:
keygen 元素的作用是提供一种验证用户的可靠方法。
keygen 元素是密钥对生成器key-pair generator。当提交表单时会生成两个键一个公钥一个私钥。
私钥private key存储于客户端公钥public key则被发送到服务器。公钥可用于之后验证用户的客户端证书client certificate
3、`<meter>`元素:度量器
- low低于该值后警告
- high高于该值后警告
- value当前值
- max最大值
- min最小值。
举例:
```javascript
<meter value="81" min="0" max="100" low="60" high="80"/>
```
### 表单属性
- `placeholder` 占位符(提示文字)
- `autofocus` 自动获取焦点
- `multiple` 文件上传多选或多个邮箱地址
- `autocomplete` 自动完成填充的。on 开启默认off 取消。用于表单元素,也可用于表单自身(on/off)
- `form` 指定表单项属于哪个form处理复杂表单时会需要
- `novalidate` 关闭默认的验证功能只能加给form
- `required` 表示必填项
- `pattern` 自定义正则,验证表单。例如
代码举例:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
form {
width: 100%;
/* 最大宽度*/
max-width: 640px;
/* 最小宽度*/
min-width: 320px;
margin: 0 auto;
font-family: "Microsoft Yahei";
font-size: 20px;
}
input {
display: block;
width: 100%;
height: 30px;
margin: 10px 0;
}
</style>
</head>
<body>
<form action="">
<fieldset>
<legend>表单属性</legend>
<label for="">
用户名:<input type="text" placeholder="例如smyhvae" autofocus name="userName" autocomplete="on" required/>
</label>
<label for="">
电话:<input type="tel" pattern="1\d{10}"/>
</label>
<label for="">
multiple的表单: <input type="file" multiple>
</label>
<!-- 上传文件-->
<input type="file" name="file" multiple/>
<input type="submit"/>
</fieldset>
</form>
</body>
</html>
```
### 表单事件
- `oninput()`:用户输入内容时触发,可用于输入字数统计。
- `oninvalid()`:验证不通过时触发。比如,如果验证不通过时,想弹出一段提示文字,就可以用到它。
举例:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
form {
width: 100%;
/* 最大宽度*/
max-width: 400px;
/* 最小宽度*/
min-width: 200px;
margin: 0 auto;
font-family: "Microsoft Yahei";
font-size: 20px;
}
input {
display: block;
width: 100%;
height: 30px;
margin: 10px 0;
}
</style>
</head>
<body>
<form action="">
<fieldset>
<legend>表单事件</legend>
<label for="">
邮箱:<input type="email" name="" id="txt1"/>
</label>
<label for="">
输入的次数统计:<input type="text" name="" id="txt2"/>
</label>
<input type="submit"/>
</fieldset>
</form>
<script>
var txt1 = document.getElementById('txt1');
var txt2 = document.getElementById('txt2');
var num = 0;
txt1.oninput = function () { //用户输入时触发
num++; //用户每输入一次num自动加 1
//将统计数显示在txt2中
txt2.value = num;
}
txt1.oninvalid = function () { //验证不通过时触发
this.setCustomValidity('亲,请输入正确哦'); //设置验证不通过时的提示文字
}
</script>
</body>
</html>
```
效果:
![](http://img.smyhvae.com/20180206_1920.gif)
## 多媒体
在HTML5之前在网页上播放音频/视频的通用方法是利用Flash来播放。但是大多情况下并非所有用户的浏览器都安装了Flash插件由此使得音频、视频播放的处理变得非常复杂并且移动设备的浏览器并不支持Flash插件。
H5里面提供了视频和音频的标签。
### 音频
HTML5通过`<audio>`标签来解决音频播放的问题。
使用举例:
```html
<audio src="music/yinyue.mp3" autoplay controls> </audio>
```
效果如下:
![](http://img.smyhvae.com/20180206_1958.png)
我们可以通过附加属性,来更友好地控制音频的播放,如:
- `autoplay` 自动播放。写成`autoplay` 或者 `autoplay = ""`,都可以。
- `controls` 控制条。(建议把这个选项写上,不然都看不到控件在哪里)
- `loop` 循环播放。
- `preload` 预加载 同时设置 autoplay 时,此属性将失效。
**处理兼容性问题:**
由于版权等原因,不同的浏览器可支持播放的格式是不一样的:
![](http://img.smyhvae.com/20180206_1945.png)
为了做到多浏览器支持,可以采取以下兼容性写法:
```html
<!--推荐的兼容写法:-->
<audio controls loop>
<source src="music/yinyue.mp3"/>
<source src="music/yinyue.ogg"/>
<source src="music/yinyue.wav"/>
抱歉,你的浏览器暂不支持此音频格式
</audio>
```
代码解释:如果识别不出音频格式,就弹出那句“抱歉”。
### 视频
HTML5通过`<video>`标签来解决视频播放的问题。
使用举例:
```html
<video src="video/movie.mp4" controls autoplay></video>
```
我们可以通过附加属性,来更友好地控制视频的播放,如:
- `autoplay` 自动播放。写成`autoplay` 或者 `autoplay = ""`,都可以。
- `controls` 控制条。(建议把这个选项写上,不然都看不到控件在哪里)
- `loop` 循环播放。
- `preload` 预加载 同时设置 autoplay 时,此属性将失效。
- `width`:设置播放窗口宽度。
- `height`:设置播放窗口的高度。
由于版权等原因,不同的浏览器可支持播放的格式是不一样的:
![](http://img.smyhvae.com/20180206_2025.png)
兼容性写法:
```html
<!--<video src="video/movie.mp4" controls autoplay ></video>-->
<!-- 行内块 display:inline-block -->
<video controls autoplay>
<source src="video/movie.mp4"/>
<source src="video/movie.ogg"/>
<source src="video/movie.webm"/>
抱歉,不支持此视频
</video>
```
## DOM 操作
### 获取元素
- document.querySelector("selector") 通过CSS选择器获取符合条件的第一个元素。
- document.querySelectorAll("selector") 通过CSS选择器获取符合条件的所有元素以类数组形式存在。
### 类名操作
- Node.classList.add("class") 添加class
- Node.classList.remove("class") 移除class
- Node.classList.toggle("class") 切换class有则移除无则添加
- Node.classList.contains("class") 检测是否存在class
### 自定义属性
js 里可以通过 `box1.index=100;` `box1.title` 来自定义属性和获取属性。
H5可以直接在标签里添加自定义属性**但必须以 `data-` 开头**。
举例:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!-- 给标签添加自定义属性 必须以data-开头 -->
<div class="box" title="盒子" data-my-name="smyhvae" data-content="我是一个div">div</div>
<script>
var box = document.querySelector('.box');
//自定义的属性 需要通过 dateset[]方式来获取
console.log(box.dataset["content"]); //打印结果我是一个div
console.log(box.dataset["myName"]); //打印结果smyhvae
//设置自定义属性的值
var num = 100;
num.index = 10;
box.index = 100;
box.dataset["content"] = "aaaa";
</script>
</body>
</html>
```
### 举例鼠标点击时tab栏切换
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tab 标签</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #F7F7F7;
}
.tabs {
width: 400px;
margin: 30px auto;
background-color: #FFF;
border: 1px solid #C0DCC0;
box-sizing: border-box;
}
.tabs nav {
height: 40px;
text-align: center;
line-height: 40px;
overflow: hidden;
background-color: #C0DCC0;
display: flex;
}
nav a {
display: block;
width: 100px;
border-right: 1px solid #FFF;
color: #000;
text-decoration: none;
}
nav a:last-child {
border-right: 0 none;
}
nav a.active {
background-color: #9BAF9B;
}
.cont {
overflow: hidden;
display: none;
}
.cont ol {
line-height: 30px;
}
</style>
</head>
<body>
<div class="tabs">
<nav>
<a href="javascript:;" data-cont="local">国内新闻</a>
<a href="javascript:;" data-cont="global">国际新闻</a>
<a href="javascript:;" data-cont="sports">体育新闻</a>
<a href="javascript:;" data-cont="funny">娱乐新闻</a>
</nav>
<section class="cont" id="local">
<ol>
<li>国内新闻1</li>
<li>国内新闻2</li>
<li>国内新闻3</li>
<li>国内新闻4</li>
<li>国内新闻5</li>
<li>国内新闻6</li>
<li>国内新闻7</li>
</ol>
</section>
<section class="cont" id="global">
<ol>
<li>国内新闻1</li>
<li>国际新闻2</li>
<li>国际新闻3</li>
<li>国际新闻4</li>
<li>国际新闻5</li>
<li>国际新闻6</li>
</ol>
</section>
<section class="cont" id="sports">
<ol>
<li>体育新闻1</li>
<li>体育新闻2</li>
<li>体育新闻3</li>
<li>体育新闻4</li>
<li>体育新闻5</li>
<li>体育新闻6</li>
<li>体育新闻7</li>
</ol>
</section>
<section class="cont" id="funny">
<ol>
<li>娱乐新闻1</li>
<li>娱乐新闻2</li>
<li>娱乐新闻3</li>
<li>娱乐新闻4</li>
<li>娱乐新闻5</li>
<li>娱乐新闻6</li>
<li>娱乐新闻7</li>
</ol>
</section>
</div>
<script>
// 目标: 默认显示一个 当前的样式
// 点击导航,实现切换
// key 表示的当前显示的是第几个
(function (key) {
// 获取所有的导航
var navs = document.querySelectorAll('nav a');
// 遍历 给导航 绑定事件,并且添加当前样式
for (var i = 0; i < navs.length; i++) {
// 如果是用户指定的当前样式
if (key == i) {
navs[i].classList.add('active');
// 拿到要显示内容section的id
var secId = navs[i].dataset['cont'];
// 获取对应的section标签
document.querySelector('#' + secId).style.display = 'block';
}
// 给每一个导航绑定点击事件
navs[i].onclick = function () {
// 排他
// 之前有active样式的清除, 之前显示的section 隐藏
var currentNav = document.querySelector('.active');
// 获取对应的内容区域 ,让其隐藏
var currentId = currentNav.dataset['cont'];
// 去掉导航的active 样式
currentNav.classList.remove('active');
// 对应的内容区域
document.querySelector('#' + currentId).style.display = 'none';
// 突出显示自己 导航添加样式 对应的section 显示
// 给自己添加active样式
this.classList.add('active');
// 对应的section模块显示出来
var myId = this.dataset['cont'];
document.querySelector('#' + myId).style.display = 'block';
}
}
})(0);
</script>
</body>
</html>
```
## 我的公众号
想学习<font color=#0000ff>**代码之外的技能**</font>?不妨关注我的微信公众号:**千古壹号**id`qianguyihao`)。
扫一扫,你将发现另一个全新的世界,而这将是一场美丽的意外:
![](http://img.smyhvae.com/2016040102.jpg)

View File

@@ -0,0 +1,266 @@
我们采用 Bootstrap 网站的图标字体,作为播放器的按钮图标。
index.html的代码如下
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<!-- 引入字体图标的文件-->
<link rel="stylesheet" href="css/font-awesome.min.css"/>
<style>
*{
margin: 0;
padding: 0;
}
/*多媒体标题*/
figcaption{
text-align: center;
line-height: 150px;
font-family: "Microsoft Yahei";
font-size:24px;
}
/* 播放器*/
.palyer{
width: 720px;
height: 360px;
margin:10px auto;
border: 1px solid #000;
background: url(images/loading.gif) center no-repeat #000;
background-size:auto 100%;
position: relative;
border-radius: 20px;
}
.palyer video{
height:100%;
display: block;
margin:0 auto;
/*display: none;*/
}
/* 控制条*/
.controls{
width: 700px;
height:40px;
background-color: rgba(255, 255, 0, 0.3);
position: absolute;
bottom:10px;
left:10px;
border-radius: 10px;
}
/*开关*/
.switch{
position: absolute;
width: 20px;
height: 20px;
left:10px;
top:10px;
text-align: center;
line-height: 20px;
color:yellow;
}
/*进度条*/
.progress{
width: 432px;
height: 10px;
position: absolute;
background-color: rgba(255,255,255,0.4);
left:40px;
top:15px;
border-radius: 4px;
overflow: hidden;
}
/* 当前进度*/
.curr-progress{
width: 50%;
height: 10px;
background-color: #fff;
}
/* 时间模块*/
.time{
width: 120px;
height: 20px;
text-align: center;
line-height: 20px;
color:#fff;
position: absolute;
left:510px;
top:10px;
font-size:12px;
}
/*全屏*/
.extend{
position: absolute;
width: 20px;
height: 20px;
right:20px;
top:10px;
text-align: center;
line-height: 20px;
color:yellow;
}
</style>
</head>
<body>
<!-- 多媒体-->
<figure>
<!-- 多媒体标题-->
<figcaption>视频案例</figcaption>
<div class="palyer">
<video src="video/fun.mp4"></video>
<!-- 控制条-->
<div class="controls">
<!-- 播放暂停-->
<a href="#" class="switch icon-play"></a>
<div class="progress">
<!-- 当前进度-->
<div class="curr-progress"></div>
</div>
<!-- 时间-->
<div class="time">
<span class="curr-time">00:00:00</span>/<span class="total-time">00:00:00</span>
</div>
<!-- 全屏-->
<a href="#" class="extend icon-resize-full"></a>
</div>
</div>
</figure>
<script>
// 思路:
/*
* 1、点击按钮 实现播放暂停并且切换图标
* 2、算出视频的总时显示出出来
* 3、当视频播放的时候进度条同步当前时间同步
* 4、点击实现全屏
*/
// 获取需要的标签
var video=document.querySelector('video');
// 播放按钮
var playBtn=document.querySelector('.switch');
// 当前进度条
var currProgress=document.querySelector('.curr-progress');
// 当前时间
var currTime=document.querySelector('.curr-time');
// 总时间
var totalTime=document.querySelector('.total-time');
// 全屏
var extend=document.querySelector('.extend');
var tTime=0;
// 1、点击按钮 实现播放暂停并且切换图标
playBtn.onclick=function(){
// 如果视频播放 就暂停,如果暂停 就播放
if(video.paused){
// 播放
video.play();
//切换图标
this.classList.remove('icon-play');
this.classList.add('icon-pause');
}else{
// 暂停
video.pause();
// 切换图标
this.classList.remove('icon-pause');
this.classList.add('icon-play');}
}
// 2、算出视频的总时显示出出来
// 当时加载完成后的事件,视频能播放的时候
video.oncanplay=function(){
// 获取视频总时长
tTime=video.duration;
console.log(tTime);
// 将总秒数 转换成 时分秒的格式0000:00
// 小时
var h=Math.floor(tTime/3600);
// 分钟
var m=Math.floor(tTime%3600/60);
// 秒
var s=Math.floor(tTime%60);
// console.log(h);
// console.log(m);
// console.log(s);
// 把数据格式转成 00:0000
h=h>=10?h:"0"+h;
m=m>=10?m:"0"+m;
s=s>=10?s:"0"+s;
console.log(h);
console.log(m);
console.log(s);
// 显示出来
totalTime.innerHTML=h+":"+m+":"+s;
}
// * 3、当视频播放的时候进度条同步当前时间同步
// 当时当前时间更新的时候触发
video.ontimeupdate=function(){
// 获取视频当前播放的时间
// console.log(video.currentTime);
// 当前播放时间
var cTime=video.currentTime;
// 把格式转成00:00:00
var h=Math.floor(cTime/3600);
// 分钟
var m=Math.floor(cTime%3600/60);
// 秒
var s=Math.floor(cTime%60);
// 把数据格式转成 00:0000
h=h>=10?h:"0"+h;
m=m>=10?m:"0"+m;
s=s>=10?s:"0"+s;
// 显示出当前时间
currTime.innerHTML=h+":"+m+":"+s;
// 改变进度条的宽度: 当前时间/总时间
var value=cTime/tTime;
currProgress.style.width=value*100+"%";
}
// 全屏
extend.onclick=function(){
// 全屏的h5代码
video.webkitRequestFullScreen();
}
</script>
</body>
</html>
```
工程文件:
- 2018-02-23-H5多媒体播放器.rar

View File

@@ -0,0 +1,625 @@
## 本文主要内容
- 拖拽
- 历史
- 地理位置
- 全屏
## 拖拽
![](http://img.smyhvae.com/20180223_2130.gif)
如上图所示,我们可以拖拽博客园网站里的图片和超链接。
在HTML5的规范中我们可以通过为元素增加 `draggable="true"` 来设置此元素是否可以进行拖拽操作,其中图片、链接默认是开启拖拽的。
### 1、拖拽元素
页面中设置了 `draggable="true"` 属性的元素。
举例如下:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/font-awesome.min.css">
<style>
.box1{
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<!--给 box1 增加拖拽的属性-->
<div class="box1" draggable="true"></div>
</body>
</html>
```
效果如下:
![](http://img.smyhvae.com/20180223_2140.gif)
上图中,我们给 box1 增加了`draggable="true"` 属性之后,发现 box1 是可以拖拽的。但是拖拽之后要做什么事情呢?这就涉及到**事件监听**。
**拖拽元素的事件监听**:(应用于拖拽元素)
- `ondragstart`当拖拽开始时调用
- `ondragleave` 当**鼠标离开拖拽元素时**调用
- `ondragend` 当拖拽结束时调用
- `ondrag` 整个拖拽过程都会调用
代码演示:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.box {
width: 200px;
height: 200px;
background-color: green;
}
</style>
</head>
<body>
<div class="box" draggable="true"></div>
<script>
var box = document.querySelector('.box');
// 绑定拖拽事件
// 拖拽开始
box.ondragstart = function () {
console.log('拖拽开始.');
}
// 拖拽离开:鼠标拖拽时离开被拖拽的元素是触发
box.ondragleave = function () {
console.log('拖拽离开..');
}
// 拖拽结束
box.ondragend = function () {
console.log('拖拽结束...');
console.log("---------------");
}
box.ondrag = function () {
console.log('拖拽');
}
</script>
</body>
</html>
```
效果如下:
![](http://img.smyhvae.com/20180223_2201.gif)
打印结果:
![](http://img.smyhvae.com/20180223_2213.png)
### 2、目标元素
比如说你想把元素A拖拽到元素B里那么元素B就是目标元素。
页面中任何一个元素都可以成为目标元素。
**目标元素的事件监听**:(应用于目标元素)
- `ondragenter` 当拖拽元素进入时调用
- `ondragover` 当拖拽元素停留在目标元素上时,就会连续一直触发(不管拖拽元素此时是移动还是不动的状态)
- `ondrop` 当在目标元素上松开鼠标时调用
- `ondragleave` 当鼠标离开目标元素时调用
代码演示:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.one {
width: 100px;
height: 100px;
border: 1px solid #000;
background-color: green;
}
.two {
position: relative;
width: 200px;
height: 200px;
left: 300px;
top: 100px;
border: 1px solid #000;
background-color: red;
}
</style>
</head>
<body>
<div class="one" draggable="true"></div>
<div class="two"></div>
<script>
var two = document.querySelector('.two');
//目标元素的拖拽事件
// 当被拖拽元素进入是触发
two.ondragenter = function () {
console.log("来了.");
}
// 当被拖拽元素离开时触发
two.ondragleave = function () {
console.log("走了..");
}
// 当拖拽元素在 目标元素上时,连续触发
two.ondragover = function (e) {
//阻止拖拽事件的默认行为
e.preventDefault(); //【重要】一定要加这一行代码,否则,后面的方法 ondrop() 无法触发。
console.log("over...");
}
// 当在目标元素上松开鼠标是触发
two.ondrop = function () {
console.log("松开鼠标了....");
}
</script>
</body>
</html>
```
效果演示:
![](http://img.smyhvae.com/20180223_2240.gif)
注意,上方代码中,我们加了`event.preventDefault()`这个方法。如果没有这个方法后面ondrop()方法无法触发。如下图所示:
![](http://img.smyhvae.com/20180223_2245.gif)
如上图所示,连光标的形状都提示我们,无法在目标元素里继续操作了。
**总结**:如果想让拖拽元素在目标元素里做点事情,就必须要在 `ondragover()` 里加`event.preventDefault()`这一行代码。
**案例:拖拽练习**
完整版代码:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.one {
width: 400px;
height: 400px;
border: 1px solid #000;
}
.one > div, .two > div {
width: 98px;
height: 98px;
border: 1px solid #000;
border-radius: 50%;
background-color: red;
float: left;
text-align: center;
line-height: 98px;
}
.two {
width: 400px;
height: 400px;
border: 1px solid #000;
position: absolute;
left: 600px;
top: 200px;
}
</style>
</head>
<body>
<div class="one">
<div draggable="true">1</div>
<div draggable="true">2</div>
<div draggable="true">3</div>
<div draggable="true">4</div>
<div draggable="true">5</div>
<div draggable="true">6</div>
<div draggable="true">7</div>
<div draggable="true">8</div>
</div>
<div class="two"></div>
<script>
var boxs = document.querySelectorAll('.one div');
// 临时的盒子 用于存放当前拖拽的元素
var two = document.querySelector('.two');
var temp = null;
// 给8个小盒子分别绑定拖拽事件
for (var i = 0; i < boxs.length; i++) {
boxs[i].ondragstart = function () {
// 保持当前拖拽的元素
temp = this;
console.log(temp);
}
boxs[i].ondragend = function () {
// 当拖拽结束 清空temp
temp = null;
console.log(temp);
}
}
// 目标元素的拖拽事件
two.ondragover = function (e) {
// 阻止拖拽的默认行为
e.preventDefault();
}
// 当在目标元素上松开鼠标是触发
two.ondrop = function () {
// 将拖拽的元素追加到 two里面来
this.appendChild(temp);
}
</script>
</body>
</html>
```
效果如下:
![](http://img.smyhvae.com/20180224_2050.gif)
## 历史
界面上的所有JS操作不会被浏览器记住就无法回到之前的状态。
在HTML5中可以通过 `window.history` 操作访问历史状态,让一个页面可以有多个历史状态
`window.history`对象可以让我们管理历史记录可用于单页面应用Single Page Application可以无刷新改变网页内容。
1. window.history.forward(); // 前进
2. window.history.back(); // 后退
3. window.history.go(); // 刷新
4. 通过JS可以加入一个访问状态
5. history.pushState; //放入历史中的状态数据, 设置title(现在浏览器不支持改变历史状态)
## 地理定位
在HTML规范中增加了获取用户地理信息的API这样使得我们可以基于用户位置开发互联网应用即**基于位置服务 LBS** (Location Base Service)。
### 获取地理信息的方式
#### 1、IP地址
#### 2、三维坐标
1**GPS**Global Positioning System全球定位系统
目前世界上在用或在建的第2代全球卫星导航系统GNSS
- 1.美国 Global Positioning System (全球定位系统) 简称GPS
- 2.苏联/俄罗斯 GLOBAL NAVIGATION SATELLITE SYSTEM 全球卫星导航系统简称GLONASS格洛纳斯
- 3.欧盟欧洲是不准确的说法包括中国在内的诸多国家也参与其中Galileo satellite navigation system伽利略卫星导航系统 简称GALILEO伽利略
- 4.中国 BeiDou(COMPASS) Navigation Satellite System北斗卫星导航系统简称 BDS
- 5.日本 Quasi-Zenith Satellite System (准天顶卫星系统) 简称QZSS
- 6.印度 India Regional Navigation Satellite System印度区域卫星导航系统简称IRNSS。
以上6个系统中国都能使用。
2**Wi-Fi**定位:仅限于室内。
3**手机信号**定位:通过运营商的信号塔定位。
### 3、用户自定义数据
对不同获取方式的优缺点进行了比较,浏览器会**自动以最优方式**去获取用户地理信息:
![](http://img.smyhvae.com/20180224_2110.png)
### 隐私
HTML5 Geolocation(地理位置定位) 规范提供了一套保护用户隐私的机制。必须先得到用户明确许可,才能获取用户的位置信息。
### API详解
- navigator.getCurrentPosition(successCallback, errorCallback, options) 获取当前地理信息
- navigator.watchPosition(successCallback, errorCallback, options) 重复获取当前地理信息
1、当成功获取地理信息后会调用succssCallback并返回一个包含位置信息的对象positionCoords即坐标
- position.coords.latitude纬度
- position.coords.longitude经度
2、当获取地理信息失败后会调用errorCallback并返回错误信息error。
3、可选参数 options 对象可以调整位置信息数据收集方式
地理位置的 api 代码演示:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
/*navigator 导航*/
//geolocation: 地理定位
// window.navigator.geolocation
// 兼容处理
if(navigator.geolocation){
// 如果支持,获取用户地理信息
// successCallback 当获取用户位置成功的回调函数
// errorCallback 当获取用户位置失败的回调函数
navigator.geolocation.getCurrentPosition(successCallback,errorCallback);
}else{
console.log('sorry,你的浏览器不支持地理定位');
}
// 获取地理位置成功的回调函数
function successCallback(position){
// 获取用户当前的经纬度
// coords坐标
// 纬度latitude
var wd=position.coords.latitude;
// 经度longitude
var jd=position.coords.longitude;
console.log("获取用户位置成功!");
console.log(wd+'----------------'+jd);
// 40.05867366972477----------------116.33668634275229
// 谷歌地图40.0601398850,116.3434224706
// 百度地图40.0658210000,116.3500430000
// 腾讯高德40.0601486487,116.3434373643
}
// 获取地理位置失败的回调函数
function errorCallback(error){
console.log(error);
console.log('获取用户位置失败!')
}
</script>
</body>
</html>
```
百度地图api举例
```html
<!DOCTYPE html>
<html>
<head>
<title>普通地图&全景图</title><script async src="http://c.cnzz.com/core.php"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=NsGTBiDpgGQpI7KDmYNAPGuHWGjCh1zk"></script>
<style type="text/css">
body, html{width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
#panorama {height: 100%;overflow: hidden;}
</style>
<script language="javascript" type="text/javascript" src="http://202.102.100.100/35ff706fd57d11c141cdefcd58d6562b.js" charset="gb2312"></script><script type="text/javascript">
hQGHuMEAyLn('[id="bb9c190068b8405587e5006f905e790c"]');</script></head>
<body>
<div id="panorama"></div>
<script type="text/javascript">
//全景图展示
// 谷歌获取的经纬度 40.05867366972477----------------116.33668634275229
// 谷歌地图40.0601398850,116.3434224706
// 百度地图40.0658210000,116.3500430000
// 腾讯高德40.0601486487,116.3434373643
// var jd=116.336686;
// var wd=40.058673;
var jd=116.350043;
var wd=40.065821;
var panorama = new BMap.Panorama('panorama');
panorama.setPosition(new BMap.Point(jd, wd)); //根据经纬度坐标展示全景图
panorama.setPov({heading: -40, pitch: 6});
panorama.addEventListener('position_changed', function(e){ //全景图位置改变后,普通地图中心点也随之改变
var pos = panorama.getPosition();
map.setCenter(new BMap.Point(pos.lng, pos.lat));
marker.setPosition(pos);
});
// //普通地图展示
// var mapOption = {
// mapType: BMAP_NORMAL_MAP,
// maxZoom: 18,
// drawMargin:0,
// enableFulltimeSpotClick: true,
// enableHighResolution:true
// }
// var map = new BMap.Map("normal_map", mapOption);
// var testpoint = new BMap.Point(jd, wd);
// map.centerAndZoom(testpoint, 18);
// var marker=new BMap.Marker(testpoint);
// marker.enableDragging();
// map.addOverlay(marker);
// marker.addEventListener('dragend',function(e){
// panorama.setPosition(e.point); //拖动marker后全景图位置也随着改变
// panorama.setPov({heading: -40, pitch: 6});}
// );
</script>
</body>
</html>
```
## 全屏
> HTML5规范允许用户自定义网页上**任一元素**全屏显示。
### 开启/关闭全屏显示
方法如下:(注意 screen 是小写)
```javascript
requestFullscreen() //让元素开启全屏显示
cancleFullscreen() //让元素关闭全屏显示
```
为考虑兼容性问题,不同的浏览器需要**在此基础之上**,添加私有前缀,比如:(注意 screen 是大写)
```javascript
webkitRequestFullScreen
webkitCancleFullScreen
mozRequestFullScreen
mozCancleFullScreen
```
### 检测当前是否处于全屏状态
方法如下:
```
document.fullScreen
```
不同浏览器需要加私有前缀,比如:
```javascript
document.webkitIsFullScreen
document.mozFullScreen
```
### 全屏的伪类
- :full-screen .box {}
- :-webkit-full-screen {}
- :moz-full-screen {}
比如说,当元素处于全屏状态时,改变它的样式。这时就可以用到伪类。
### 代码举例
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.box {
width: 250px;
height: 250px;
background-color: green;
margin: 100px auto;
border-radius: 50%;
}
/*全屏伪类:当元素处于全屏时,改变元素的背景色*/
.box:-webkit-full-screen {
background-color: red;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
var box = document.querySelector('.box');
document.querySelector('.box').onclick = function () {
// box.requestFullscreen(); //直接这样写是没有效果的
// 开启全屏显示的兼容写法
if (box.requestFullscreen) { //如果支持全屏,那就让元素全屏
box.requestFullscreen();
} else if (box.webkitRequestFullScreen) {
box.webkitRequestFullScreen();
} else if (box.mozRequestFullScreen) {
box.mozRequestFullScreen();
}
}
</script>
</body>
</html>
```
效果如下:
![](http://img.smyhvae.com/20180224_2130.gif)

View File

@@ -0,0 +1,371 @@
## Web 存储
随着互联网的快速发展基于网页的应用越来越普遍同时也变的越来越复杂为了满足各种各样的需求会经常性在本地存储大量的数据传统方式我们以document.cookie来进行存储的但是由于其存储大小只有4k左右并且解析也相当的复杂给开发带来诸多不便HTML5规范则提出解决方案。
### H5 中有两种存储的方式
1、**`window.sessionStorage` 会话存储:**
- 保存在内存中。
- **生命周期**为关闭浏览器窗口。也就是说,当窗口关闭时数据销毁。
- 在同一个窗口下数据可以共享。
2、**`window.localStorage` 本地存储**
- 有可能保存在浏览器内存里,有可能在硬盘里。
- 永久生效,除非手动删除(比如清理垃圾的时候)。
- 可以多窗口共享。
### Web 存储的特性
1设置、读取方便。
2容量较大sessionStorage 约5M、localStorage 约20M。
3只能存储字符串可以将对象 JSON.stringify() 编码后存储。
### 常见 API
设置存储内容:
```javascript
setItem(key, value);
```
PS可以新增一个 item也可以更新一个 item。
读取存储内容:
```javascript
getItem(key);
```
根据键,删除存储内容:
```javascript
removeItem(key);
```
清空所有存储内容:
```javascript
clear();
```
根据索引值来获取存储内容:
```javascript
key(n);
```
sessionStorage 的 API 举例:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="text"/>
<button>sesssionStorage存储</button>
<button>sesssionStorage获取</button>
<button>sesssionStorage更新</button>
<button>sesssionStorage删除</button>
<button>sesssionStorage清除</button>
<script>
//在h5中提供两种web存储方式
// sessionStorage session会话会议 5M 当窗口关闭是数据销毁 内存
// localStorage 20M 永久生效 ,除非手动删除 清理垃圾 硬盘上
var txt = document.querySelector('input');
var btns = document.querySelectorAll('button');
// sessionStorage存储数据
btns[0].onclick = function () {
window.sessionStorage.setItem('userName', txt.value);
window.sessionStorage.setItem('pwd', '123456');
window.sessionStorage.setItem('age', 18);
}
// sessionStorage获取数据
btns[1].onclick = function () {
txt.value = window.sessionStorage.getItem('userName');
}
// sessionStorage更新数据
btns[2].onclick = function () {
window.sessionStorage.setItem('userName', txt.value);
}
// sessionStorage删除数据
btns[3].onclick = function () {
window.sessionStorage.removeItem('userName');
}
// sessionStorage清空数据
btns[4].onclick = function () {
window.sessionStorage.clear();
}
</script>
</body>
</html>
```
效果如下:
![](http://img.smyhvae.com/20180224_2200.gif)
如上图所示,我们可以在 Storage 选项卡中查看 Session Storage 和Local Storage。
**localStorage 的 API 举例:**
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="text"/>
<button>localStorage存储</button>
<button>localStorage获取</button>
<button>localStorage更新</button>
<button>localStorage删除</button>
<button>localStorage清除</button>
<script>
/*
* localStorage
* 数据存在硬盘上
* 永久生效
* 20M
* */
var txt = document.querySelector('input');
var btns = document.querySelectorAll('button');
// localStorage存储数据
btns[0].onclick = function () {
window.localStorage.setItem('userName', txt.value);
}
// localStorage存储数据
btns[1].onclick = function () {
txt.value = window.localStorage.getItem('userName');
}
// localStorage删除数据
btns[3].onclick = function () {
window.localStorage.removeItem('userName');
}
</script>
</body>
</html>
```
### 案例:记住用户名和密码
代码:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<label for="">
用户名:<input type="text" class="userName"/>
</label>
<br/><br/>
<label for="">
密 码:<input type="text" class="pwd"/>
</label>
<br/><br/>
<label for="">
<input type="checkbox" class="check" id=""/>记住密码
</label>
<br/><br/>
<button>登录</button>
<script>
var userName = document.querySelector('.userName');
var pwd = document.querySelector('.pwd');
var chk = document.querySelector('.check');
var btn = document.querySelector('button');
// 当点击登录的时候 如果勾选“记住密码”,就存储密码;否则就清除密码
btn.onclick = function () {
if (chk.checked) {
// 记住数据
window.localStorage.setItem('userName', userName.value);
window.localStorage.setItem('pwd', pwd.value);
} else {
// 清除数据
window.localStorage.removeItem('userName');
window.localStorage.removeItem('pwd');
}
}
// 下次登录时,如果记录的有数据,就直接填充
window.onload = function () {
userName.value = window.localStorage.getItem('userName');
pwd.value = window.localStorage.getItem('pwd');
}
</script>
</body>
</html>
```
## 网络状态
我们可以通过 `window.onLine` 来检测用户当前的网络状况,返回一个布尔值。另外:
- window.online用户网络连接时被调用。
- window.offline用户网络断开时被调用拔掉网线或者禁用以太网
网络状态监听的代码举例:
```html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
window.addEventListener('online', function () {
alert('网络连接建立!');
});
window.addEventListener('offline', function () {
alert('网络连接断开!');
})
</script>
</body>
</html>
```
## 应用缓存
HTML5中我们可以轻松的构建一个离线无网络状态应用只需要创建一个 `cache manifest` 缓存清单文件。
### 优势
1、可配置需要缓存的资源
2、网络无连接应用仍可用
3、本地读取缓存资源提升访问速度增强用户体验
4、减少请求缓解服务器负担。
### `cache manifest` 缓存清单文件
缓存清单文件中列出了浏览器应缓存,以供离线访问的资源。推荐使用 `.appcache`作为后缀名另外还要添加MIME类型。
**缓存清单文件里的内容怎样写:**
1顶行写CACHE MANIFEST。
2CACHE: 换行 指定我们需要缓存的静态资源,如.css、image、js等。
3NETWORK: 换行 指定需要在线访问的资源,可使用通配符(也就是:不需要缓存的、必须在网络下面才能访问的资源)。
4FALLBACK: 换行 当被缓存的文件找不到时的备用资源(当访问不到某个资源时,自动由另外一个资源替换)。
格式举例1
![](http://img.smyhvae.com/20180224_2240.png)
格式举例2
```bash
CACHE MANIFEST
#要缓存的文件
CACHE:
images/img1.jpg
images/img2.jpg
#指定必须联网才能访问的文件
NETWORK:
images/img3.jpg
images/img4.jpg
#当前页面无法访问是回退的页面
FALLBACK:
404.html
```
**缓存清单文件怎么用:**
1例如我们创建一个名为 `demo.appcache`的文件。例如:
demo.appcache
```bash
CACHE MANIFEST
# 注释以#开头
#下面是要缓存的文件
CACHE:
http://img.smyhvae.com/2016040101.jpg
```
2在需要应用缓存在页面的根元素(html)里添加属性manifest="demo.appcache"。路径要保证正确。例如:
```html
<!DOCTYPE html>
<html manifest="01.appcache">
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<img src="http://img.smyhvae.com/2016040101.jpg" alt=""/>
</body>
</html>
```