This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.js
53 lines (45 loc) · 1.48 KB
/
lib.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
var $ = function(selector) {
return document.querySelector(selector);
}
var $$ = function(selector) {
return document.querySelectorAll(selector);
}
// This function is taken from http://blog.stchur.com/2011/11/07/dealing-with-css-vendor-prefixes/
var getStyleName = function () {
var styleHash = {};
var vendorPrefixes = [ 'O', 'ms', 'Moz', 'Webkit' ];
return function (styleName, elem) {
elem = elem || document.createElement('div');
var s = styleHash[styleName];
if (!s) {
// hope the browser supports the "official" (non-vendor-prefixed) style first
var camStyle = camelize(styleName);
// if not, then ask getVendorStyle for help -- it will try the various vendor prefixes seeking
// one that works (if it doesn't find one, it will return an empty string, in which case we
// revert back to originally passed in styleName)
if (elem.style[camStyle] === undefined) {
camStyle = getVendorStyle(camStyle, elem) || styleName;
}
// finally, store the camelized & vendor-specific (if applicable) value in the styleHash where key = styleName that was passed in
s = styleHash[styleName] = camStyle;
}
return s;
}
function getVendorStyle(style, elem) {
var s = camelize('-' + style);
var i = vendorPrefixes.length;
while (i--) {
style = vendorPrefixes[i] + s;
if (elem.style[style] !== undefined) {
return style;
}
}
return '';
}
;
function camelize(s) {
return s.replace(/-(\w)/g, function (m, c) {
return c.toUpperCase();
});
}
}();