h5 new API

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/litcangosh/article/details/102724426

H5- new API

Full screen method

HTML5 specification allows a user to customize a page element took full screen.

  • Node.requestFullScreen () to open full-screen display
  • Node.cancelFullScreen () Close full screen
  • Due to its compatibility reasons, different browsers need to add a prefix, such as:
    WebKit browser core: webkitRequestFullScreen, webkitCancelFullScreen, such as chrome browser.
    Gecko browser kernel: mozRequestFullScreen, mozCancelFullScreen, such as Firefox.
  • document.fullScreen detection is currently in full-screen
    different browsers need to add the prefix
    document.webkitIsFullScreen, document.mozFullScreen
multimedia

Custom Player

method

method description
addTextTrack() / Video add new text to an audio track
canPlayType() Detecting whether the browser can play the specified audio / video type
load() Reload the audio / video elements
play() Start playing the audio / video
pause() Pause the audio / video currently playing

Attributes

Attributes description
audioTracks Returns a track objects available AudioTrackList
autoplay Sets or returns immediately if the audio / video playback after loading is completed
buffered Back represent audio / video portion of the object buffered TimeRanges
controller Returns an audio / video media objects MediaController current controller
controls Sets or returns the audio / video controls whether to display (such as play / pause, etc.)
crossOrigin Sets or returns the audio / CORS video settings
currentSrc Returns the current URL of audio / video
currentTime Sets or returns the audio / video of the current playback position (in seconds)
defaultMuted Sets or returns the audio / video is not muted by default
defaultPlaybackRate Sets or returns the audio / video playback speed of the default
duration Returns the current audio / video length (in seconds)
ended Whether to return the audio / video playback has ended
error Returns MediaError object audio / video error state
loop Sets or returns the audio / video whether it should be replayed at the end of
mediaGroup Sets or returns combined audio / video belongs (for connecting a plurality of audio / video element)
muted Sets or returns the audio / video is muted
networkState Return to the audio / video of the current network status
paused Sets or returns the audio / video is paused
playbackRate Set or return to the audio / video playback speed
played Returns an audio / video playback portion of the object has TimeRanges
preload Sets or returns the audio / video whether it should be loaded after the page is loaded
readyState Return to the audio / video current readiness
seekable Returns TimeRanges object audio / video portion of the addressable
seeking Returns whether the user is an audio / video to find
src Sets or returns the audio / video elements of the current source
startDate Date object returns the current time offset
textTracks Returns a text track of objects available TextTrackList
Videotrchks Returns VideoTrackList objects available video tracks
volume Sets or returns the audio / video volume

event

event description
abort When load the audio / video of abandoned
canplay When the browser can play audio / video
canplaythrough When the browser can be played without pause due to buffering
durationchange When the length of the audio / video Changed
emptied 当目前的播放列表为空时
ended 当目前的播放列表已结束时
error 当在音频/视频加载期间发生错误时
loadeddata 当浏览器已加载音频/视频的当前帧时
loadedmetadata 当浏览器已加载音频/视频的元数据时
loadstart 当浏览器开始查找音频/视频时
pause 当音频/视频已暂停时
play 当音频/视频已开始或不再暂停时
playing 当音频/视频在已因缓冲而暂停或停止后已就绪时
progress 当浏览器正在下载音频/视频时
ratechange 当音频/视频的播放速度已更改时
seeked 当用户已移动/跳跃到音频/视频中的新位置时
seeking 当用户开始移动/跳跃到音频/视频中的新位置时
stalled 当浏览器尝试获取媒体数据,但数据不可用时
suspend 当浏览器刻意不获取媒体数据时
timeupdate 当目前的播放位置已更改时
volumechange 当音量已更改时
waiting 当视频由于需要缓冲下一帧而停止
地理定位

在HTML规范中,增加了获取用户地理信息的API,
这样使得我们可以基于用户位置开发互联网应用,
即基于位置服务 (Location Base Service)

  • 获取当前地理信息
navigator.geolocation.getCurrentPosition(successCallback, errorCallback) 
  • 重复获取当前地理信息
navigator. geolocation.watchPosition(successCallback, errorCallback)
  • 当成功获取地理信息后,会调用succssCallback,并返回一个包含位置信息的对象position。
    position.coords.latitude纬度
    position.coords.longitude经度
    position.coords.accuracy精度
    position.coords.altitude海拔高度
  • 当获取地理信息失败后,会调用errorCallback,并返回错误信息error
  • 在现实开发中,通过调用第三方API(如百度地图)来实现地理定位信息,这些API都是基于用户当前位置的,并将用位置位置(经/纬度)当做参数传递,就可以实现相应的功能。
本地存储

随着互联网的快速发展,基于网页的应用越来越普遍,
同时也变的越来越复杂,为了满足各种各样的需求,会经常性在本地存储大量的数据,
HTML5规范提出了相关解决方案。

  • 特性
    • 设置、读取方便
    • 容量较大,sessionStorage约5M、localStorage约20M
    • 只能存储字符串,可以将对象JSON.stringify() 编码后存储
  • window.sessionStorage
    • 生命周期为关闭浏览器窗口
    • 在同一个窗口(页面)下数据可以共享
  • window.localStorage
    • 永久生效,除非手动删除(服务器方式访问然后清除缓存)
    • 可以多窗口(页面)共享
  • 方法
    • setItem(key, value) 设置存储内容
    • getItem(key) 读取存储内容
    • removeItem(key) 删除键值为key的存储内容
    • clear() 清空所有存储内容
历史管理

提供window.history,对象我们可以管理历史记录,
可用于单页面应用,Single Page Application,可以无刷新改变网页内容。

  • pushState(data, title, url) 追加一条历史记录
    • data用于存储自定义数据,通常设为null
      ​+ title网页标题,基本上没有被支持,一般设为空
      ​+ url 以当前域为基础增加一条历史记录,不可跨域设置
  • replaceState(data, title, url) 与pushState()基本相同,
    不同之处在于replaceState(),只是替换当前url,不会增加/减少历史记录。
  • onpopstate事件,当前进或后退时则触发
离线应用

HTML5中我们可以轻松的构建一个离线(无网络状态)应用,只需要创建一个cache manifest文件。

  • 优势

    • 1、可配置需要缓存的资源
    • 2、网络无连接应用仍可用
    • 3、本地读取缓存资源,提升访问速度,增强用户体验
    • 4、减少请求,缓解服务器负担
  • 缓存清单

    • 一个普通文本文件,其中列出了浏览器应缓存以供离线访问的资源,推荐使用.appcache为后缀名
    • 例如我们创建了一个名为demo.appcache的文件,然后在需要应用缓存在页面的根元素(html)添加属性manifest=“demo.appcache”,路径要保证正确。
  • manifest文件格式

    • 1、顶行写CACHE MANIFEST
    • 2、CACHE: 换行 指定我们需要缓存的静态资源,如.css、image、js等
    • 3、NETWORK: 换行 指定需要在线访问的资源,可使用通配符
    • 4、FALLBACK: 换行 当被缓存的文件找不到时的备用资源
  • 其它

    • 1、CACHE: 可以省略,这种情况下将需要缓存的资源写在CACHE MANIFEST
    • 2、可以指定多个CACHE: NETWORK: FALLBACK:,无顺序限制
    • 3、#表示注释,只有当demo.appcache文件内容发生改变时或者手动清除缓存后,才会重新缓存。
    • 4、chrome 可以通过chrome://appcache-internals/工具和离线(offline)模式来调试管理应用缓存
文件读取

HTML5新增内建对象,可以读取本地文件内容。

网络状态
  • 我们可以通过window.onLine来检测,用户当前的网络状况,返回一个布尔值
    • window.online用户网络连接时被调用
    • window.offline用户网络断开时被调用

Guess you like

Origin blog.csdn.net/litcangosh/article/details/102724426