-
-
Save drublic/1379503 to your computer and use it in GitHub Desktop.
Static polyfill for vw, vh, vm units
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Polyfill for the vw, vh, vm units including StyleFix | |
* @author Lea Verou | |
*/ | |
(function(){ | |
if(!window.addEventListener) { | |
return; | |
} | |
var self = window.StyleFix = { | |
link: function(link) { | |
try { | |
if(!/\bstylesheet\b/i.test(link.rel) || !link.sheet.cssRules) { | |
return; | |
} | |
} | |
catch(e) { | |
return; | |
} | |
var url = link.href || link.getAttribute('data-href'), | |
base = url.replace(/[^\/]+$/, ''), | |
parent = link.parentNode, | |
xhr = new XMLHttpRequest(); | |
xhr.open('GET', url); | |
xhr.onreadystatechange = function() { | |
if(xhr.readyState === 4) { | |
var css = xhr.responseText; | |
if(css && link.parentNode) { | |
css = self.fix(css, true, link); | |
// Convert relative URLs to absolute, if needed | |
if(base) { | |
css = css.replace(/url\((?:'|")?(.+?)(?:'|")?\)/gi, function($0, url) { | |
if(!/^([a-z]{3,10}:|\/)/i.test(url)) { // If url not absolute | |
// May contain sequences like /../ and /./ but those DO work | |
return 'url("' + base + url + '")'; | |
} | |
return $0; | |
}); | |
// behavior URLs shoudn’t be converted (Issue #19) | |
css = css.replace(RegExp('\\b(behavior:\\s*?url\\(\'?"?)' + base, 'gi'), '$1'); | |
} | |
var style = document.createElement('style'); | |
style.textContent = css; | |
style.media = link.media; | |
style.disabled = link.disabled; | |
style.setAttribute('data-href', link.getAttribute('href')); | |
parent.insertBefore(style, link); | |
parent.removeChild(link); | |
} | |
} | |
}; | |
xhr.send(null); | |
link.setAttribute('data-inprogress', ''); | |
}, | |
styleElement: function(style) { | |
var disabled = style.disabled; | |
style.textContent = self.fix(style.textContent, true, style); | |
style.disabled = disabled; | |
}, | |
styleAttribute: function(element) { | |
var css = element.getAttribute('style'); | |
css = self.fix(css, false, element); | |
element.setAttribute('style', css); | |
}, | |
process: function() { | |
// Linked stylesheets | |
$('link[rel~="stylesheet"]:not([data-inprogress])').forEach(StyleFix.link); | |
// Inline stylesheets | |
$('style').forEach(StyleFix.styleElement); | |
// Inline styles | |
$('[style]').forEach(StyleFix.styleAttribute); | |
}, | |
register: function(fixer, index) { | |
(self.fixers = self.fixers || []) | |
.splice(index === undefined? self.fixers.length : index, 0, fixer); | |
}, | |
fix: function(css, raw) { | |
for(var i=0; i<self.fixers.length; i++) { | |
css = self.fixers[i](css, raw) || css; | |
} | |
return css; | |
}, | |
camelCase: function(str) { | |
return str.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); }).replace('-',''); | |
}, | |
deCamelCase: function(str) { | |
return str.replace(/[A-Z]/g, function($0) { return '-' + $0.toLowerCase() }); | |
} | |
}; | |
/************************************** | |
* Process styles | |
**************************************/ | |
(function(){ | |
setTimeout(function(){ | |
$('link[rel~="stylesheet"]').forEach(StyleFix.link); | |
}, 10); | |
document.addEventListener('DOMContentLoaded', StyleFix.process, false); | |
})(); | |
function $(expr, con) { | |
return [].slice.call((con || document).querySelectorAll(expr)); | |
} | |
// Feature test | |
var dummy = document.createElement('_').style, | |
units = ['vw', 'vh', 'vm'].filter(function(unit) { | |
dummy.width = ''; | |
dummy.width = '10' + unit; | |
return !dummy.width; | |
}); | |
if(!units.length) { | |
return; | |
} | |
StyleFix.register(function(css) { | |
var w = innerWidth, h = innerHeight, m = Math.min(w,h); | |
return css.replace(RegExp('\\b(\\d+)(' + units.join('|') + ')\\b', 'gi'), function($0, num, unit) { | |
switch (unit) { | |
case 'vw': | |
return (num * w / 100) + 'px'; | |
case 'vh': | |
return num * h / 100 + 'px'; | |
case 'vm': | |
return num * m / 100 + 'px'; | |
} | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's not working with window resize in safari browser.