Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5da40bd

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 #9368 Closes #2070, #9360
1 parent b1ee538 commit 5da40bd

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

@@ -74,20 +85,33 @@ function $AnchorScrollProvider() {
7485
return result;
7586
}
7687

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

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

83107
// element with given id
84-
else if ((elm = document.getElementById(hash))) elm.scrollIntoView();
108+
else if ((elm = document.getElementById(hash))) scrollTo(elm);
85109

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

89113
// no element and hash == 'top', scroll to the top of the page
90-
else if (hash === 'top') $window.scrollTo(0, 0);
114+
else if (hash === 'top') scrollTo(null);
91115
}
92116

93117
// does not scroll when user clicks on anchor link that is currently on
@@ -102,4 +126,3 @@ function $AnchorScrollProvider() {
102126
return scroll;
103127
}];
104128
}
105-

0 commit comments

Comments
 (0)