Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 51 lines (29 sloc) 959 Bytes
/*
# Aspect Ratio Mixin for reproCSS
## version 0.0.10
Define a height for HTML elements based on the element's width and a supplied aspect ratio.
### Syntax
aspectRatio(selector, ratio)
- `selector` is a CSS selector
- `ratio` is a ratio expressed as `width/height` or a number
### Example
aspectRatio('iframe', 16/9)
- https://github.com/tomhodgins/reprocss
Author: Tommy Hodgins
License: MIT
*/
function aspectRatio(selector, ratio) {
var tag = document.querySelectorAll(selector)
var style = ''
var count = 0
for (var i=0; i < tag.length; i++) {
var attr = selector.replace(/\W+/g, '')
tag[i].setAttribute('data-aspect-ratio-' + attr, count)
style += '\n/* ' + selector + ' { aspect-ratio: ' + ratio + '; } */\n'
+ '[data-aspect-ratio-' + attr + '="' + count + '"] {\n'
+ ' height: ' + (tag[i].offsetWidth / ratio) + 'px;\n'
+ '}\n'
count++
}
return style
}