forked from Prinzhorn/skrollr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskrollr.ie.js
121 lines (97 loc) · 2.76 KB
/
skrollr.ie.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*!
* Plugin for skrollr.
* This plugin brings opacity and hsl colors to IE < 9.
*
* https://github.com/Prinzhorn/skrollr
*
* free to use under terms of MIT license
*/
(function(document, skrollr) {
var rxHSLAColor = /hsla?\(\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)%\s*,\s*(-?[\d.]+)%.*?\)/g;
var rxRGBAColor = /rgba?\(\s*(-?[\d.]+%?)\s*,\s*(-?[\d.]+%?)\s*,\s*(-?[\d.]+%?).*?\)/g;
var rxID = /^#[^\s]+$/;
var _setStyle = skrollr.setStyle;
//Monkeypatch the setStyle function.
skrollr.setStyle = function(el, prop, val) {
//Original function call.
_setStyle.apply(this, arguments);
var style = el.style;
var matched;
//IE opacity
if(prop === 'opacity') {
style.zoom = 1;
//Remove filter attribute in IE
if(val >= 1 && style.removeAttribute) {
style.removeAttribute('filter');
} else {
style.filter = 'alpha(opacity=' + val * 100 + ')';
}
return;
}
//Fast pre check
if(val.indexOf('hsl') > -1) {
matched = false;
//Replace hsl(a) with hex if needed (ignoring alpha).
val = val.replace(rxHSLAColor, function(x, h, s, l) {
matched = true;
return toHex.hsl(parseFloat(h), parseFloat(s), parseFloat(l));
});
if(matched) {
try {
style[prop] = val;
} catch(ignore) {}
return;
}
}
//Fast pre check
if(val.indexOf('rgb') > -1) {
matched = false;
//Replace rgba with hex if needed (ignoring alpha).
val = val.replace(rxRGBAColor, function(s, r, g, b) {
matched = true;
r = parseFloat(r, 10);
g = parseFloat(g, 10);
b = parseFloat(b, 10);
//rgba allows percentage notation.
if(s.indexOf('%') > -1) {
r = (r / 100) * 255;
g = (g / 100) * 255;
b = (b / 100) * 255;
}
return toHex.rgb(r | 0, g | 0, b | 0);
});
if(matched) {
try {
style[prop] = val;
} catch(ignore) {}
return;
}
}
};
/**
* Converts rgb or hsl color to hex color.
*/
var toHex = {
//Credits to aemkei, jed and others
//Based on https://gist.github.com/1325937 and https://gist.github.com/983535
hsl: function(a,b,c,y){
a%=360;
a=a/60;c=c/100;b=[c+=b*=(c<0.5?c:1-c)/100,c-a%1*b*2,c-=b*=2,c,c+a%1*b,c+b];
y = [b[~~a%6],b[(a|16)%6],b[(a|8)%6]];
return toHex.rgb(parseInt(y[0] * 255, 10), parseInt(y[1] * 255, 10), parseInt(y[2] * 255, 10));
},
//https://gist.github.com/983535
rgb: function(a,b,c){
return'#' + ((256+a<<8|b)<<8|c).toString(16).slice(1);
}
};
/*
A really bad polyfill. But the main use-case for data-anchor-target are IDs.
*/
document.querySelector = document.querySelector || function(selector) {
if(!rxID.test(selector)) {
throw 'Unsupported selector "' + selector + '". The querySelector polyfill only works for IDs.';
}
return document.getElementById(selector.substr(1));
};
}(document, window.skrollr));