-
Notifications
You must be signed in to change notification settings - Fork 12
/
bg.js
104 lines (83 loc) · 2.48 KB
/
bg.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(function() {
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
var bg = document.getElementById('background')
var wrap = document.querySelector('.wrap')
if (!(isChrome) && !(isSafari)) {
return
}
if (isSafari) {
bg.style.minHeight = "100%"
//bg.style.width = window.innerWidth + 'px'
//setTimeout(function() {
//drawBg()
//})
}
var needsDraw = true
window.addEventListener('scroll', function() {
needsDraw = true
})
window.addEventListener('resize', function() {
needsDraw = true
bg.style.width = window.innerWidth + 'px'
})
window.addEventListener('load', function() {
needsDraw = true
bg.style.width = window.innerWidth + 'px'
})
function drawBg() {
var windowScroll = window.scrollY
var windowHeight = window.innerHeight
var bodyHeight = document.body.clientHeight - windowHeight
var bgHeight = (bg.height || bg.clientHeight) - windowHeight
var scrollFraction = bgHeight / bodyHeight
var newTop = Math.floor(
windowScroll * (1 - scrollFraction)
)
translateY(bg, newTop)
}
function render() {
var scrollY = window.scrollY
if (render.lastScroll != scrollY) needsDraw = true
render.lastScroll = scrollY
if (needsDraw) drawBg()
needsDraw = false
raf(render)
}
var transformKey = null
if (isChrome || isSafari) transformKey = 'webkitTransform'
if (isFirefox) transformKey = 'mozTransform'
function translateY(el, y){
var transform = 'translate3d(0, '+y+'px, 0)'
el.style.transform = transform
if (transformKey) el.style[transformKey] = transform
}
var raf = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| fallback;
/**
* Fallback implementation.
*/
var prev = +new Date();
function fallback(fn) {
var curr = +new Date();
var ms = Math.max(0, 16 - (curr - prev));
var req = setTimeout(fn, ms);
prev = curr;
return req;
}
setTimeout(function() {
bg.style.position = 'absolute'
bg.style.width = window.innerWidth + 'px'
drawBg()
render()
})
})()