UNPKG

strong-arc

Version:

A visual suite for the StrongLoop API Platform

40 lines (39 loc) 1.63 kB
/*global angular, $, document*/ /** * Adds a 'ui-scrollfix' class to the element when the page scrolls past it's position. * @param [offset] {int} optional Y-offset to override the detected offset. * Takes 300 (absolute) or -300 or +300 (relative to detected) */ angular.module('ui.directives').directive('uiScrollfix', ['$window', function ($window) { 'use strict'; return { link: function (scope, elm, attrs) { var top = elm.offset().top; if (!attrs.uiScrollfix) { attrs.uiScrollfix = top; } else { // chartAt is generally faster than indexOf: http://jsperf.com/indexof-vs-chartat if (attrs.uiScrollfix.charAt(0) === '-') { attrs.uiScrollfix = top - attrs.uiScrollfix.substr(1); } else if (attrs.uiScrollfix.charAt(0) === '+') { attrs.uiScrollfix = top + parseFloat(attrs.uiScrollfix.substr(1)); } } angular.element($window).on('scroll.ui-scrollfix', function () { // if pageYOffset is defined use it, otherwise use other crap for IE var offset; if (angular.isDefined($window.pageYOffset)) { offset = $window.pageYOffset; } else { var iebody = (document.compatMode && document.compatMode !== "BackCompat") ? document.documentElement : document.body; offset = iebody.scrollTop; } if (!elm.hasClass('ui-scrollfix') && offset > attrs.uiScrollfix) { elm.addClass('ui-scrollfix'); } else if (elm.hasClass('ui-scrollfix') && offset < attrs.uiScrollfix) { elm.removeClass('ui-scrollfix'); } }); } }; }]);