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

Commit 731c8b5

Browse files
committedApr 2, 2015
feat($anchorScroll): allow scrolling to a specified element
Add an optional argument to `$anchorScroll()` to enable scrolling to an anchor element different than that related to the current value of `$location.hash()`. If the argument is omitted or is not a string, the value of `$location.hash()` will be used instead. Closes #4568 Closes #9596
1 parent 06a9f0a commit 731c8b5

File tree

2 files changed

+112
-38
lines changed

2 files changed

+112
-38
lines changed
 

‎src/ng/anchorScroll.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ function $AnchorScrollProvider() {
3838
* @requires $rootScope
3939
*
4040
* @description
41-
* When called, it checks the current value of {@link ng.$location#hash $location.hash()} and
42-
* scrolls to the related element, according to the rules specified in the
43-
* [Html5 spec](http://dev.w3.org/html5/spec/Overview.html#the-indicated-part-of-the-document).
41+
* When called, it scrolls to the element related to the specified `hash` or (if omitted) to the
42+
* current value of {@link ng.$location#hash $location.hash()}, according to the rules specified
43+
* in the
44+
* [HTML5 spec](http://dev.w3.org/html5/spec/Overview.html#the-indicated-part-of-the-document).
4445
*
4546
* It also watches the {@link ng.$location#hash $location.hash()} and automatically scrolls to
4647
* match any anchor whenever it changes. This can be disabled by calling
@@ -49,6 +50,9 @@ function $AnchorScrollProvider() {
4950
* Additionally, you can use its {@link ng.$anchorScroll#yOffset yOffset} property to specify a
5051
* vertical scroll-offset (either fixed or dynamic).
5152
*
53+
* @param {string=} hash The hash specifying the element to scroll to. If omitted, the value of
54+
* {@link ng.$location#hash $location.hash()} will be used.
55+
*
5256
* @property {(number|function|jqLite)} yOffset
5357
* If set, specifies a vertical scroll-offset. This is often useful when there are fixed
5458
* positioned elements at the top of the page, such as navbars, headers etc.
@@ -232,8 +236,9 @@ function $AnchorScrollProvider() {
232236
}
233237
}
234238

235-
function scroll() {
236-
var hash = $location.hash(), elm;
239+
function scroll(hash) {
240+
hash = isString(hash) ? hash : $location.hash();
241+
var elm;
237242

238243
// empty hash, scroll to the top of the page
239244
if (!hash) scrollTo(null);

‎test/ng/anchorScrollSpec.js

+102-33
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ describe('$anchorScroll', function() {
2323
};
2424
}
2525

26-
2726
function addElements() {
2827
var elements = sliceArgs(arguments);
2928

@@ -49,9 +48,9 @@ describe('$anchorScroll', function() {
4948
};
5049
}
5150

52-
function callAnchorScroll() {
51+
function callAnchorScroll(hash) {
5352
return function($anchorScroll) {
54-
$anchorScroll();
53+
$anchorScroll(hash);
5554
};
5655
}
5756

@@ -141,50 +140,120 @@ describe('$anchorScroll', function() {
141140
beforeEach(createMockWindow());
142141

143142

144-
it('should scroll to top of the window if empty hash', inject(
145-
changeHashAndScroll(''),
146-
expectScrollingToTop));
143+
describe('and implicitly using `$location.hash()`', function() {
144+
145+
it('should scroll to top of the window if empty hash', inject(
146+
changeHashAndScroll(''),
147+
expectScrollingToTop));
148+
149+
150+
it('should not scroll if hash does not match any element', inject(
151+
addElements('id=one', 'id=two'),
152+
changeHashAndScroll('non-existing'),
153+
expectNoScrolling()));
154+
155+
156+
it('should scroll to anchor element with name', inject(
157+
addElements('a name=abc'),
158+
changeHashAndScroll('abc'),
159+
expectScrollingTo('a name=abc')));
160+
161+
162+
it('should not scroll to other than anchor element with name', inject(
163+
addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
164+
changeHashAndScroll('xxl'),
165+
expectNoScrolling()));
166+
167+
168+
it('should scroll to anchor even if other element with given name exist', inject(
169+
addElements('input name=some', 'a name=some'),
170+
changeHashAndScroll('some'),
171+
expectScrollingTo('a name=some')));
172+
173+
174+
it('should scroll to element with id with precedence over name', inject(
175+
addElements('name=abc', 'id=abc'),
176+
changeHashAndScroll('abc'),
177+
expectScrollingTo('id=abc')));
147178

148179

149-
it('should not scroll if hash does not match any element', inject(
150-
addElements('id=one', 'id=two'),
151-
changeHashAndScroll('non-existing'),
152-
expectNoScrolling()));
180+
it('should scroll to top if hash == "top" and no matching element', inject(
181+
changeHashAndScroll('top'),
182+
expectScrollingToTop));
153183

154184

155-
it('should scroll to anchor element with name', inject(
156-
addElements('a name=abc'),
157-
changeHashAndScroll('abc'),
158-
expectScrollingTo('a name=abc')));
185+
it('should scroll to element with id "top" if present', inject(
186+
addElements('id=top'),
187+
changeHashAndScroll('top'),
188+
expectScrollingTo('id=top')));
189+
});
190+
191+
192+
describe('and specifying a hash', function() {
193+
194+
it('should ignore the `hash` argument if not a string', inject(
195+
spyOnJQLiteDocumentLoaded(),
196+
addElements('id=one', 'id=two'),
197+
changeHashTo('one'), // won't scroll since `jqLiteDocumentLoaded()` is spied upon
198+
callAnchorScroll({}),
199+
expectScrollingTo('id=one'),
200+
unspyOnJQLiteDocumentLoaded()));
201+
202+
203+
it('should ignore `$location.hash()` if `hash` is passed as argument', inject(
204+
spyOnJQLiteDocumentLoaded(),
205+
addElements('id=one', 'id=two'),
206+
changeHashTo('one'), // won't scroll since `jqLiteDocumentLoaded()` is spied upon
207+
callAnchorScroll('two'),
208+
expectScrollingTo('id=two'),
209+
unspyOnJQLiteDocumentLoaded()));
210+
159211

212+
it('should scroll to top of the window if empty hash', inject(
213+
callAnchorScroll(''),
214+
expectScrollingToTop));
160215

161-
it('should not scroll to other than anchor element with name', inject(
162-
addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
163-
changeHashAndScroll('xxl'),
164-
expectNoScrolling()));
165216

217+
it('should not scroll if hash does not match any element', inject(
218+
addElements('id=one', 'id=two'),
219+
callAnchorScroll('non-existing'),
220+
expectNoScrolling()));
166221

167-
it('should scroll to anchor even if other element with given name exist', inject(
168-
addElements('input name=some', 'a name=some'),
169-
changeHashAndScroll('some'),
170-
expectScrollingTo('a name=some')));
171222

223+
it('should scroll to anchor element with name', inject(
224+
addElements('a name=abc'),
225+
callAnchorScroll('abc'),
226+
expectScrollingTo('a name=abc')));
172227

173-
it('should scroll to element with id with precedence over name', inject(
174-
addElements('name=abc', 'id=abc'),
175-
changeHashAndScroll('abc'),
176-
expectScrollingTo('id=abc')));
177228

229+
it('should not scroll to other than anchor element with name', inject(
230+
addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
231+
callAnchorScroll('xxl'),
232+
expectNoScrolling()));
178233

179-
it('should scroll to top if hash == "top" and no matching element', inject(
180-
changeHashAndScroll('top'),
181-
expectScrollingToTop));
182234

235+
it('should scroll to anchor even if other element with given name exist', inject(
236+
addElements('input name=some', 'a name=some'),
237+
callAnchorScroll('some'),
238+
expectScrollingTo('a name=some')));
183239

184-
it('should scroll to element with id "top" if present', inject(
185-
addElements('id=top'),
186-
changeHashAndScroll('top'),
187-
expectScrollingTo('id=top')));
240+
241+
it('should scroll to element with id with precedence over name', inject(
242+
addElements('name=abc', 'id=abc'),
243+
callAnchorScroll('abc'),
244+
expectScrollingTo('id=abc')));
245+
246+
247+
it('should scroll to top if hash == "top" and no matching element', inject(
248+
callAnchorScroll('top'),
249+
expectScrollingToTop));
250+
251+
252+
it('should scroll to element with id "top" if present', inject(
253+
addElements('id=top'),
254+
callAnchorScroll('top'),
255+
expectScrollingTo('id=top')));
256+
});
188257
});
189258

190259

0 commit comments

Comments
 (0)
This repository has been archived.