Skip to content

Commit 2c62851

Browse files
committed
Avoiding calling scrollTop() twice
When there is an anchor element referenced by the hash in the URL, we would call `scrollTop()` first with zero, then with the element position. Now, ensure that we only call scrollTop once at most.
1 parent 94daeed commit 2c62851

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

jquery.pjax.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,17 @@ function pjax(options) {
317317

318318
executeScriptTags(container.scripts)
319319

320-
// Scroll to top by default
321-
if (typeof options.scrollTo === 'number')
322-
$(window).scrollTop(options.scrollTo)
320+
var scrollTo = options.scrollTo
323321

324-
// If the URL has a hash in it, make sure the browser
325-
// knows to navigate to the hash.
322+
// Ensure browser scrolls to the element referenced by the URL anchor
326323
if (hash) {
327324
var name = decodeURIComponent(hash.slice(1))
328325
var target = document.getElementById(name) || document.getElementsByName(name)[0]
329-
if (target) $(window).scrollTop($(target).offset().top)
326+
if (target) scrollTo = $(target).offset().top
330327
}
331328

329+
if (typeof scrollTo == 'number') $(window).scrollTop(scrollTo)
330+
332331
fire('pjax:success', [data, status, xhr, options])
333332
}
334333

0 commit comments

Comments
 (0)