Created
June 25, 2015 10:37
-
-
Save jefvel/24b19bae3edebd4d6439 to your computer and use it in GitHub Desktop.
Adds a class to a element when it has a scrollbar.
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
| //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