utils.js
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* 将 dom 元素全屏
* @param {dom} element dom元素
* @example
* setFullscreen(document.documentElement) // 整个页面进入全屏
* setFullscreen(document.getElementById("id")) // 某个元素进入全屏
*/
export function setFullScreen (element) {
if (element.requestFullscreen) {
element.requestFullscreen()
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen()
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen()
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullScreen()
}
}
/**
* 退出全屏
* @example
* exitFullscreen();
*/
export function exitFullScreen () {
if (document.exitFullscreen) {
document.exitFullscreen()
} else if (document.msExitFullscreen) {
document.msExitFullscreen()
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen()
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen()
}
}
/**
* 从 window.location.href 中获取指定key的value
* @param {*} key 要获取的 key
* @returns window.location.href 中指定key对应的value
* @example
* const value = getUrlParam(key);
*/
export function getUrlParam (key) {
const url = window.location.href.replace(/^[^?]*\?/, '')
const regexp = new RegExp(`(^|&)${key}=([^&#]*)(&|$|)`, 'i')
const paramMatch = url.match(regexp)
return paramMatch ? paramMatch[2] : null
}
/**
* 检测数据是否为undefined
* @param {*} data 检测数据
*/
export function isUndefined (data) {
return typeof data === 'undefined'
}
/**
* 防抖函数
* @param {Function} fn 执行函数
* @param {Number} delay 延迟时间
*/
export function debounce (fn, delay) {
let timer = null
return function () {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(fn, delay)
}
}