Last active
October 19, 2018 14:27
-
-
Save MaciejLisCK/3fe0ed59db7e069c6bd7 to your computer and use it in GitHub Desktop.
AngularJs onsizechanged, onresize, on element resize directive, angularjs responsiveness
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
<div on-size-changed="logResize"> | |
Foo | |
</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
module.controller('SomeCtrl', function ($scope) { | |
$scope.logResize = function () { | |
console.log('element resized'); | |
}; | |
} |
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
'use strict'; | |
module.directive('onSizeChanged', ['$window', function ($window) { | |
return { | |
restrict: 'A', | |
scope: { | |
onSizeChanged: '&' | |
}, | |
link: function (scope, $element, attr) { | |
var element = $element[0]; | |
cacheElementSize(scope, element); | |
$window.addEventListener('resize', onWindowResize); | |
function cacheElementSize(scope, element) { | |
scope.cachedElementWidth = element.offsetWidth; | |
scope.cachedElementHeight = element.offsetHeight; | |
} | |
function onWindowResize() { | |
var isSizeChanged = scope.cachedElementWidth != element.offsetWidth || scope.cachedElementHeight != element.offsetHeight; | |
if (isSizeChanged) { | |
var expression = scope.onSizeChanged(); | |
expression(); | |
} | |
}; | |
} | |
} | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this only fires on window resize, not on element size change