Skip to content

Commit f417627

Browse files
committed
feat($anchorScroll): add support for scrolling independently of $location.hash()
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, the value of `$location.hash()` will be used instead. Closes angular#4568
1 parent 2452e3c commit f417627

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/ng/anchorScroll.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ function $AnchorScrollProvider() {
5050
* Additionally, you can use its {@link ng.$anchorScroll#yOffset yOffset} property to specify a
5151
* vertical scroll-offset (either fixed or dynamic).
5252
*
53-
* @param {string} hash The hash specifying the element to scroll to. If omitted,
54-
* {@link ng.$location#hash $location.hash()} will be used.
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.
5555
*
5656
* @property {(number|function|jqLite)} yOffset
5757
* If set, specifies a vertical scroll-offset. This is often useful when there are fixed

test/ng/anchorScrollSpec.js

+69-3
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

@@ -137,12 +136,12 @@ describe('$anchorScroll', function() {
137136
}));
138137

139138

140-
describe('when implicitly using `$location.hash()`', function() {
139+
describe('when explicitly called', function() {
141140

142141
beforeEach(createMockWindow());
143142

144143

145-
describe('and no hash is specified', function() {
144+
describe('and implicitly using `$location.hash()`', function() {
146145

147146
it('should scroll to top of the window if empty hash', inject(
148147
changeHashAndScroll(''),
@@ -189,6 +188,73 @@ describe('$anchorScroll', function() {
189188
changeHashAndScroll('top'),
190189
expectScrollingTo('id=top')));
191190
});
191+
192+
193+
describe('and specifying a hash', function() {
194+
195+
it('should ignore the `hash` argument if not a string', inject(
196+
spyOnJQLiteDocumentLoaded(),
197+
addElements('id=one', 'id=two'),
198+
changeHashTo('one'), // won't scroll since `jqLiteDocumentLoaded()` is spied upon
199+
callAnchorScroll({}),
200+
expectScrollingTo('id=one'),
201+
unspyOnJQLiteDocumentLoaded()));
202+
203+
204+
it('should ignore `$location.hash()` if `hash` is passed as argument', inject(
205+
spyOnJQLiteDocumentLoaded(),
206+
addElements('id=one', 'id=two'),
207+
changeHashTo('one'), // won't scroll since `jqLiteDocumentLoaded()` is spied upon
208+
callAnchorScroll('two'),
209+
expectScrollingTo('id=two'),
210+
unspyOnJQLiteDocumentLoaded()));
211+
212+
213+
it('should scroll to top of the window if empty hash', inject(
214+
callAnchorScroll(''),
215+
expectScrollingToTop));
216+
217+
218+
it('should not scroll if hash does not match any element', inject(
219+
addElements('id=one', 'id=two'),
220+
callAnchorScroll('non-existing'),
221+
expectNoScrolling()));
222+
223+
224+
it('should scroll to anchor element with name', inject(
225+
addElements('a name=abc'),
226+
callAnchorScroll('abc'),
227+
expectScrollingTo('a name=abc')));
228+
229+
230+
it('should not scroll to other than anchor element with name', inject(
231+
addElements('input name=xxl', 'select name=xxl', 'form name=xxl'),
232+
callAnchorScroll('xxl'),
233+
expectNoScrolling()));
234+
235+
236+
it('should scroll to anchor even if other element with given name exist', inject(
237+
addElements('input name=some', 'a name=some'),
238+
callAnchorScroll('some'),
239+
expectScrollingTo('a name=some')));
240+
241+
242+
it('should scroll to element with id with precedence over name', inject(
243+
addElements('name=abc', 'id=abc'),
244+
callAnchorScroll('abc'),
245+
expectScrollingTo('id=abc')));
246+
247+
248+
it('should scroll to top if hash == "top" and no matching element', inject(
249+
callAnchorScroll('top'),
250+
expectScrollingToTop));
251+
252+
253+
it('should scroll to element with id "top" if present', inject(
254+
addElements('id=top'),
255+
callAnchorScroll('top'),
256+
expectScrollingTo('id=top')));
257+
});
192258
});
193259

194260

0 commit comments

Comments
 (0)