Created
August 11, 2015 10:01
-
-
Save ruiwen/22050c55eaa849a4f01f to your computer and use it in GitHub Desktop.
AngularJs directive for dynamically adjusting an element's height to fill the vertical height of the page
This file contains hidden or 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
angular.module('utils') | |
.directive('sq-content', [ | |
'$log' | |
'$window' | |
($log, $window) -> | |
restrict: 'A' | |
link: (scope, elem, attrs) -> | |
# Calculates and sets the height of element to fully take up | |
# the rest of the vertical height in a page | |
# Mainly used for activating overflow: scroll for md-content | |
# elements (since they don't work with flex for some reason) | |
# Requires: jQuery | |
# Requires: 'debounce' from underscore.js | |
@resize = () -> | |
$log.debug '[sqContent resize]' | |
if $(window).width() < $(window).height() # Portrait | |
offsetTop = $(elem).offset().top | |
windowHeight = $(window).height() | |
$(elem).height(windowHeight - offsetTop) | |
else if $(window).width() >= $(window).height() | |
$(elem).css('height', '100%') | |
$window.addEventListener('resize', _.debounce(@resize, 100)) | |
@resize() | |
return | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment