Created
May 20, 2014 19:32
-
-
Save elado/7365a96dd6c6f58480db to your computer and use it in GitHub Desktop.
Responsive Boxes: Keep Ratio of Width and Height
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
# By Elad Ossadon | |
app = angular.module 'eo-keepratio' | |
SPACERS = | |
'1:1': "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" | |
'16:10': "R0lGODlhEAAKAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAoAAAILhI+py+0Po5y0rgIAOw==" | |
'16:9': "R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7" | |
'4:3': "R0lGODlhBAADAIAAAP///wAAACH5BAEAAAAALAAAAAAEAAMAAAIDhI9WADs=" | |
'3:1': "R0lGODlhAwABAIAAAP///wAAACH5BAEAAAAALAAAAAADAAEAAAIChAsAOw==" | |
app.directive 'keepRatio', ($compile) -> | |
restrict: 'A' | |
link: (scope, element, attrs) -> | |
size = SPACERS[attrs['keepRatio'] || '1:1'] | |
spacer = """ | |
<img src="data:image/gif;base64,#{size}" class="keep-ratio"> | |
""" | |
element.addClass('keep-ratio-container').prepend(spacer) | |
# <div keep-ratio="16:10"> | |
# <img src=...> | |
# </div> |
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
// By Elad Ossadon | |
.keep-ratio-container { | |
display: block; | |
position: relative; | |
overflow: hidden; | |
.keep-ratio { | |
width: 100%; height: auto; | |
position: static; top: auto; left: auto; right: auto; bottom: auto; z-index: 0; | |
} | |
> * { | |
position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment