赞
踩
目前并不是所有的浏览器都实现了API的无前缀版本,所以我们需要针对不同浏览器,做一下API的兼容:
- function getPropertyName(names, target) {
- return names.find(name => name in target)
- }
-
- const enterFullScreenName = getPropertyName([
- 'requestFullscreen',
- 'mozRequestFullScreen',
- 'webkitRequestFullscreen',
- 'msRequestFullscreen'
- ], document.documentElement)
-
- // 进入全屏
- export function enter(ele) {
- enterFullScreenName && ele[enterFullScreenName]()
- }
-
- const exitFullScreenName = getPropertyName([
- 'exitFullScreen',
- 'mozCancelFullScreen',
- 'webkitExitFullscreen',
- 'msExitFullscreen'
- ], document)
-
- // 退出全屏
- export function exit() {
- exitFullScreenName && document[exitFullScreenName]()
- }
-
- const fullElename = getPropertyName([
- 'fullscreenElement',
- 'mozFullScreenElement',
- 'msFullScreenElement',
- 'wenkitFullscreenElement'
- ], document)
-
- // 判断当前那个元素全屏
- export function fullEle() {
- return document[fullElename] || null
- }
-
-
- // 判断当前是否处于全屏状态
- export function isFull() {
- return !!fullEle()
- }
-
- // 全屏切换
- export function toggle(ele) {
- isFull() ? exit() : enter(ele)
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。