Skip to content

Instantly share code, notes, and snippets.

@ruiwen
Created August 11, 2015 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruiwen/22050c55eaa849a4f01f to your computer and use it in GitHub Desktop.
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
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