Skip to content

Commit ab611a1

Browse files
committed
Fix jump when no history.state
cferdinandi#441
1 parent 5159523 commit ab611a1

13 files changed

+161
-31
lines changed

dist/smooth-scroll.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v15.0.0: Animate scrolling to anchor links
2+
* smooth-scroll v15.0.1: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -294,6 +294,27 @@
294294
return speed;
295295
};
296296

297+
var setHistory = function (options) {
298+
299+
// Make sure this should run
300+
if (!history.replaceState || !options.updateURL || history.state) return;
301+
302+
// Get the hash to use
303+
var hash = window.location.hash;
304+
hash = hash ? hash : window.pageYOffset;
305+
306+
// Set a default history
307+
history.replaceState(
308+
{
309+
smoothScroll: JSON.stringify(options),
310+
anchor: hash ? hash : window.pageYOffset
311+
},
312+
document.title,
313+
hash ? hash : window.location.href
314+
);
315+
316+
};
317+
297318
/**
298319
* Update the URL
299320
* @param {Node} anchor The anchor that was scrolled to
@@ -526,6 +547,7 @@
526547
// If anchored element exists, scroll to it
527548
if (!anchor) return;
528549
event.preventDefault();
550+
setHistory(settings);
529551
smoothScroll.animateScroll(anchor, toggle);
530552

531553
};
@@ -543,11 +565,15 @@
543565
if (!history.state.smoothScroll || history.state.smoothScroll !== JSON.stringify(settings)) return;
544566

545567
// Only run if state includes an anchor
546-
if (!history.state.anchor) return;
568+
569+
// if (!history.state.anchor && history.state.anchor !== 0) return;
547570

548571
// Get the anchor
549-
var anchor = document.querySelector(escapeCharacters(decode(history.state.anchor)));
550-
if (!anchor) return;
572+
var anchor = history.state.anchor;
573+
if (anchor && anchor !== 0) {
574+
anchor = document.querySelector(escapeCharacters(decode(history.state.anchor)));
575+
if (!anchor) return;
576+
}
551577

552578
// Animate scroll to anchor link
553579
smoothScroll.animateScroll(anchor, null, {updateURL: false});

dist/smooth-scroll.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/smooth-scroll.polyfills.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v15.0.0: Animate scrolling to anchor links
2+
* smooth-scroll v15.0.1: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -364,6 +364,27 @@ if (window.Element && !Element.prototype.closest) {
364364
return speed;
365365
};
366366

367+
var setHistory = function (options) {
368+
369+
// Make sure this should run
370+
if (!history.replaceState || !options.updateURL || history.state) return;
371+
372+
// Get the hash to use
373+
var hash = window.location.hash;
374+
hash = hash ? hash : window.pageYOffset;
375+
376+
// Set a default history
377+
history.replaceState(
378+
{
379+
smoothScroll: JSON.stringify(options),
380+
anchor: hash ? hash : window.pageYOffset
381+
},
382+
document.title,
383+
hash ? hash : window.location.href
384+
);
385+
386+
};
387+
367388
/**
368389
* Update the URL
369390
* @param {Node} anchor The anchor that was scrolled to
@@ -596,6 +617,7 @@ if (window.Element && !Element.prototype.closest) {
596617
// If anchored element exists, scroll to it
597618
if (!anchor) return;
598619
event.preventDefault();
620+
setHistory(settings);
599621
smoothScroll.animateScroll(anchor, toggle);
600622

601623
};
@@ -613,11 +635,15 @@ if (window.Element && !Element.prototype.closest) {
613635
if (!history.state.smoothScroll || history.state.smoothScroll !== JSON.stringify(settings)) return;
614636

615637
// Only run if state includes an anchor
616-
if (!history.state.anchor) return;
638+
639+
// if (!history.state.anchor && history.state.anchor !== 0) return;
617640

618641
// Get the anchor
619-
var anchor = document.querySelector(escapeCharacters(decode(history.state.anchor)));
620-
if (!anchor) return;
642+
var anchor = history.state.anchor;
643+
if (anchor && anchor !== 0) {
644+
anchor = document.querySelector(escapeCharacters(decode(history.state.anchor)));
645+
if (!anchor) return;
646+
}
621647

622648
// Animate scroll to anchor link
623649
smoothScroll.animateScroll(anchor, null, {updateURL: false});

0 commit comments

Comments
 (0)