Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
fix(scrollfix): get scrollTop from scrollfix-target
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Feb 6, 2014
1 parent 69c993d commit 0724d1d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
16 changes: 9 additions & 7 deletions modules/scrollfix/scrollfix.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
* Takes 300 (absolute) or -300 or +300 (relative to detected)
*/
angular.module('ui.scrollfix',[]).directive('uiScrollfix', ['$window', function ($window) {
function getWindowScrollTop() {
if (angular.isDefined($window.pageYOffset)) {
return $window.pageYOffset;
} else {
var iebody = (document.compatMode && document.compatMode !== 'BackCompat') ? document.documentElement : document.body;
return iebody.scrollTop;
}
}
return {
require: '^?uiScrollfixTarget',
link: function (scope, elm, attrs, uiScrollfixTarget) {
Expand All @@ -25,13 +33,7 @@ angular.module('ui.scrollfix',[]).directive('uiScrollfix', ['$window', function

function onScroll() {
// 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;
}
var offset = uiScrollfixTarget ? $target[0].scrollTop : getWindowScrollTop();
if (!elm.hasClass('ui-scrollfix') && offset > attrs.uiScrollfix) {
elm.addClass('ui-scrollfix');
} else if (elm.hasClass('ui-scrollfix') && offset < attrs.uiScrollfix) {
Expand Down
17 changes: 17 additions & 0 deletions modules/scrollfix/test/scrollfixSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,21 @@ describe('uiScrollfix', function () {
expect(element.hasClass('ui-scrollfix')).toBe(false);
});
});
describe('scrolling the target', function() {
var target, element;
beforeEach(function() {
target = $compile('<div style="height:100px;overflow:auto;" ui-scrollfix-target><div ui-scrollfix="100"></div><div style="height: 400px;"></div></div>')(scope);
element = target.find('[ui-scrollfix]');
target.appendTo('body');
});
afterEach(function() {
target.remove();
});
it('should get scroll position from target', function() {
target[0].scrollTop = 150;
//force firing scroll event
target.trigger('scroll');
expect(element.hasClass('ui-scrollfix')).toBe(true);
});
});
});

0 comments on commit 0724d1d

Please sign in to comment.