Skip to content

Instantly share code, notes, and snippets.

@jefvel
Created June 25, 2015 10:37
Show Gist options
  • Select an option

  • Save jefvel/24b19bae3edebd4d6439 to your computer and use it in GitHub Desktop.

Select an option

Save jefvel/24b19bae3edebd4d6439 to your computer and use it in GitHub Desktop.
Adds a class to a element when it has a scrollbar.
//Adds class to element when it has a scrollbar visible. Slightly hacky but works.
/*
<div when-scrollable="scrollbar-visible">stuff</div>
<style>.scrollbar-visible { background-color:green; }</style>
*/
(function () {
angular.module("app.directives")
.directive('whenScrollable', ['$timeout', WhenScrollable]);
function WhenScrollable($timeout) {
var diff = true;
function onResized(e, scope) {
diff = false;
$timeout(function(){
var scrollbarVisible = e.scrollHeight > e.clientHeight;
$(e).toggleClass(scope.scrollClass, scrollbarVisible);
diff = true;
});
}
return {
restrict: "A",
scope: {
scrollClass: "@whenScrollable"
},
link: function ($scope, element, $attrs) {
$scope.$watch(
function () {
if (diff) {
onResized(element[0], $scope);
}
return 0;
},
function (e) {
}
);
$(window).bind("resize", function () {
onResized(element[0], $scope);
});
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment