Skip to content

Commit 4cf86a2

Browse files
author
Christopher Hiller
committed
angular-ui#91: tests for scrollfix directive. tweaked scrollfix a bit for compatibility and performance.
1 parent f306c3d commit 4cf86a2

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed
Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
1+
/*global angular, $*/
22
/**
33
* Adds a 'ui-scrollfix' class to the element when the page scrolls past it's position.
44
* @param [offset] {int} optional Y-offset to override the detected offset.
55
* Takes 300 (absolute) or -300 or +300 (relative to detected)
66
*/
7-
angular.module('ui.directives').directive('uiScrollfix', [function() {
8-
return {
9-
link: function(scope, elm, attrs) {
10-
var top = elm.offset().top;
11-
if (!attrs.uiScrollfix) {
12-
attrs.uiScrollfix = top;
13-
} else {
14-
if (attrs.uiScrollfix.indexOf('-') === 0) {
15-
attrs.uiScrollfix = top - attrs.uiScrollfix.substr(1);
16-
} else if (attrs.uiScrollfix.indexOf('+') === 0) {
17-
attrs.uiScrollfix = top + parseInt(attrs.uiScrollfix.substr(1));
18-
}
19-
}
20-
$(window).bind('scroll.ui-scrollfix', function(){
21-
if (!elm.hasClass('ui-scrollfix') && window.pageYOffset > attrs.uiScrollfix) {
22-
elm.addClass('ui-scrollfix');
23-
} else if (elm.hasClass('ui-scrollfix') && window.pageYOffset < attrs.uiScrollfix) {
24-
elm.removeClass('ui-scrollfix');
25-
}
26-
});
27-
}
28-
};
7+
angular.module('ui.directives').directive('uiScrollfix', ['$window', function ($window) {
8+
'use strict';
9+
return {
10+
link: function (scope, elm, attrs) {
11+
var top = elm.offset().top;
12+
if (!attrs.uiScrollfix) {
13+
attrs.uiScrollfix = top;
14+
} else {
15+
// chartAt is generally faster than indexOf: http://jsperf.com/indexof-vs-chartat
16+
if (attrs.uiScrollfix.charAt(0) === '-') {
17+
attrs.uiScrollfix = top - attrs.uiScrollfix.substr(1);
18+
} else if (attrs.uiScrollfix.charAt(0) === '+') {
19+
attrs.uiScrollfix = top + attrs.uiScrollfix.substr(1);
20+
}
21+
}
22+
$($window).bind('scroll.ui-scrollfix', function () {
23+
// if pageYOffset is defined use it, otherwise use other crap for IE
24+
var offset;
25+
if (angular.isDefined($window.pageYOffset)) {
26+
offset = $window.pageYOffset;
27+
} else {
28+
var iebody = (document.compatMode && document.compatMode !== "BackCompat") ? document.documentElement : document.body;
29+
offset = iebody.scrollTop;
30+
}
31+
if (!elm.hasClass('ui-scrollfix') && offset > attrs.uiScrollfix) {
32+
elm.addClass('ui-scrollfix');
33+
} else if (elm.hasClass('ui-scrollfix') && offset < attrs.uiScrollfix) {
34+
elm.removeClass('ui-scrollfix');
35+
}
36+
});
37+
}
38+
};
2939
}]);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*global describe, beforeEach, module, inject, it, spyOn, expect, $ */
2+
describe('uiRemove', function () {
3+
'use strict';
4+
5+
var scope, $compile, $window;
6+
beforeEach(module('ui.directives'));
7+
beforeEach(inject(function (_$rootScope_, _$compile_, _$window_) {
8+
scope = _$rootScope_.$new();
9+
$compile = _$compile_;
10+
$window = _$window_;
11+
}));
12+
13+
describe('compiling this directive', function () {
14+
it('should bind to window "scroll" event with a ui-scrollfix namespace', function () {
15+
spyOn($.fn, 'bind');
16+
$compile('<div ui-scrollfix="100"></div>')(scope);
17+
expect($.fn.bind).toHaveBeenCalled();
18+
expect($.fn.bind.mostRecentCall.args[0]).toBe('scroll.ui-scrollfix');
19+
});
20+
});
21+
describe('scrolling the window', function () {
22+
it('should add the ui-scrollfix class if the offset is greater than specified', function () {
23+
var element = $compile('<div ui-scrollfix="-100"></div>')(scope);
24+
$($window).trigger('scroll.ui-scrollfix');
25+
expect(element.hasClass('ui-scrollfix')).toBe(true);
26+
});
27+
it('should remove the ui-scrollfix class if the offset is less than specified (using absolute coord)', function () {
28+
var element = $compile('<div ui-scrollfix="100" class="ui-scrollfix"></div>')(scope);
29+
$($window).trigger('scroll.ui-scrollfix');
30+
expect(element.hasClass('ui-scrollfix')).toBe(false);
31+
32+
});
33+
it('should remove the ui-scrollfix class if the offset is less than specified (using relative coord)', function () {
34+
var element = $compile('<div ui-scrollfix="+100" class="ui-scrollfix"></div>')(scope);
35+
$($window).trigger('scroll.ui-scrollfix');
36+
expect(element.hasClass('ui-scrollfix')).toBe(false);
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)