-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
60 lines (55 loc) · 1.42 KB
/
index.js
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
/**
* Check if device is mobile
*
* @returns {Boolean}
*/
function isMobile() {
let check = false
;(function (a) {
if (/(android|webOS|ip(hone|ad|od)|blackberry|windows phone)/i.test(a)) check = true
})(navigator.userAgent || navigator.vendor || window.opera)
return check
}
/**
* Resize handler
*
* @arg cb {Event}
*/
function onResize(cb) {
// Create resize handler
function handler(e) {
return cb(window.innerWidth, window.innerHeight, e)
}
// Add resize listeners
window.addEventListener('resize', handler)
if (screen.orientation) {
screen.orientation.addEventListener('change', handler)
} else {
window.addEventListener('orientationchange', handler)
}
// Immediately execute the resize handler
handler()
}
/**
* Main execution function
*
* @void
*/
function main(width, height, e) {
requestAnimationFrame(function () {
const changedOrientation = e && (e.type === 'orientationchange' || e.type === 'change')
if (!isMobile() || changedOrientation || changedOrientation === undefined) {
const calculatedHeight = window.matchMedia('(orientation: portrait)')
.matches
? Math.max(width, height)
: Math.min(width, height)
document.documentElement.style.setProperty('--vh', `${calculatedHeight}px`)
window.vh = calculatedHeight
}
})
}
// Fire main function with resize listeners
function init() {
onResize(main)
}
export default { init }