Skip to content

Commit 0efcbd6

Browse files
committed
WIP - feat($anchorScroll): add support for configurable scroll offset
Add support for a configurable scroll offset to $anchorScrollProvider. The offset is expressed in pixels and can be specified either as a fixed value or as a function that return the offset it pixels dynamically. (This is a POC and a WIP: no docs, no tests) Related to angular#9368 Closes angular#2070, angular#9360
1 parent 7b10232 commit 0efcbd6

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

docs/app/src/app.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ angular.module('docsApp', [
1717
'ui.bootstrap.dropdown'
1818
])
1919

20-
2120
.config(['$locationProvider', function($locationProvider) {
2221
$locationProvider.html5Mode(true).hashPrefix('!');
22+
}])
23+
24+
.config(['$anchorScrollProvider', function($anchorScrollProvider) {
25+
var header;
26+
$anchorScrollProvider.setScrollOffset(function () {
27+
header = header || window.document.querySelector('header');
28+
if (!header) return 0;
29+
30+
var style = window.getComputedStyle(header);
31+
return (style.position === 'fixed') ? header.offsetHeight : 0;
32+
});
2333
}]);

src/ng/anchorScroll.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,23 @@
5353
*/
5454
function $AnchorScrollProvider() {
5555

56+
var DEFAULT_OFFSET = 0;
57+
5658
var autoScrollingEnabled = true;
59+
var scrollOffsetGetter = function() { return DEFAULT_OFFSET; };
5760

5861
this.disableAutoScrolling = function() {
5962
autoScrollingEnabled = false;
6063
};
6164

65+
this.setScrollOffset = function(newScrollOffset) {
66+
if (isFunction(newScrollOffset)) {
67+
scrollOffsetGetter = function() { return newScrollOffset(); };
68+
} else if (isNumber(newScrollOffset)) {
69+
scrollOffsetGetter = function() { return newScrollOffset; };
70+
}
71+
};
72+
6273
this.$get = ['$window', '$location', '$rootScope', function($window, $location, $rootScope) {
6374
var document = $window.document;
6475

@@ -76,20 +87,33 @@ function $AnchorScrollProvider() {
7687
return result;
7788
}
7889

90+
function scrollTo(elem) {
91+
if (elem) {
92+
elem.scrollIntoView();
93+
} else {
94+
$window.scrollTo(0, 0);
95+
}
96+
97+
var offset = scrollOffsetGetter();
98+
if (offset) {
99+
$window.scrollBy(0, -1 * offset);
100+
}
101+
}
102+
79103
function scroll() {
80104
var hash = $location.hash(), elm;
81105

82106
// empty hash, scroll to the top of the page
83-
if (!hash) $window.scrollTo(0, 0);
107+
if (!hash) scrollTo(null);
84108

85109
// element with given id
86-
else if ((elm = document.getElementById(hash))) elm.scrollIntoView();
110+
else if ((elm = document.getElementById(hash))) scrollTo(elm);
87111

88112
// first anchor with given name :-D
89-
else if ((elm = getFirstAnchor(document.getElementsByName(hash)))) elm.scrollIntoView();
113+
else if ((elm = getFirstAnchor(document.getElementsByName(hash)))) scrollTo(elm);
90114

91115
// no element and hash == 'top', scroll to the top of the page
92-
else if (hash === 'top') $window.scrollTo(0, 0);
116+
else if (hash === 'top') scrollTo(null);
93117
}
94118

95119
// does not scroll when user clicks on anchor link that is currently on
@@ -107,4 +131,3 @@ function $AnchorScrollProvider() {
107131
return scroll;
108132
}];
109133
}
110-

0 commit comments

Comments
 (0)