Skip to content

Commit 88ba8c3

Browse files
committed
v15.1.0
Fixed header height is now calculated at time scroll initiates
1 parent 3e35e2d commit 88ba8c3

10 files changed

+23
-138
lines changed

dist/smooth-scroll.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v15.0.2: Animate scrolling to anchor links
2+
* smooth-scroll v15.1.0: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -399,7 +399,7 @@
399399
//
400400

401401
var smoothScroll = {}; // Object for public APIs
402-
var settings, anchor, toggle, fixedHeader, headerHeight, eventTimeout, animationInterval;
402+
var settings, anchor, toggle, fixedHeader, eventTimeout, animationInterval;
403403

404404

405405
//
@@ -436,10 +436,7 @@
436436
// Get the fixed header if not already set
437437
fixedHeader = document.querySelector(_settings.header);
438438
}
439-
if (!headerHeight) {
440-
// Get the height of a fixed header if one exists and not already set
441-
headerHeight = getHeaderHeight(fixedHeader);
442-
}
439+
var headerHeight = getHeaderHeight(fixedHeader);
443440
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof _settings.offset === 'function' ? _settings.offset(anchor, toggle) : _settings.offset), 10), _settings.clip); // Location to scroll to
444441
var distance = endLocation - startLocation; // distance to travel
445442
var documentHeight = getDocumentHeight();
@@ -580,18 +577,6 @@
580577

581578
};
582579

583-
/**
584-
* On window scroll and resize, only run events at a rate of 15fps for better performance
585-
*/
586-
var resizeThrottler = function (event) {
587-
if (!eventTimeout) {
588-
eventTimeout = setTimeout((function() {
589-
eventTimeout = null; // Reset timeout
590-
headerHeight = getHeaderHeight(fixedHeader); // Get the height of a fixed header if one exists
591-
}), 66);
592-
}
593-
};
594-
595580
/**
596581
* Destroy the current initialization.
597582
*/
@@ -602,7 +587,6 @@
602587

603588
// Remove event listeners
604589
document.removeEventListener('click', clickHandler, false);
605-
window.removeEventListener('resize', resizeThrottler, false);
606590
window.removeEventListener('popstate', popstateHandler, false);
607591

608592
// Cancel any scrolls-in-progress
@@ -613,7 +597,6 @@
613597
anchor = null;
614598
toggle = null;
615599
fixedHeader = null;
616-
headerHeight = null;
617600
eventTimeout = null;
618601
animationInterval = null;
619602

@@ -634,16 +617,10 @@
634617
// Selectors and variables
635618
settings = extend(defaults, options || {}); // Merge user options with defaults
636619
fixedHeader = settings.header ? document.querySelector(settings.header) : null; // Get the fixed header
637-
headerHeight = getHeaderHeight(fixedHeader);
638620

639621
// When a toggle is clicked, run the click handler
640622
document.addEventListener('click', clickHandler, false);
641623

642-
// If window is resized and there's a fixed header, recalculate its size
643-
if (fixedHeader) {
644-
window.addEventListener('resize', resizeThrottler, false);
645-
}
646-
647624
// If updateURL and popState are enabled, listen for pop events
648625
if (settings.updateURL && settings.popstate) {
649626
window.addEventListener('popstate', popstateHandler, 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: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* smooth-scroll v15.0.2: Animate scrolling to anchor links
2+
* smooth-scroll v15.1.0: Animate scrolling to anchor links
33
* (c) 2018 Chris Ferdinandi
44
* MIT License
55
* http://github.com/cferdinandi/smooth-scroll
@@ -469,7 +469,7 @@ if (window.Element && !Element.prototype.closest) {
469469
//
470470

471471
var smoothScroll = {}; // Object for public APIs
472-
var settings, anchor, toggle, fixedHeader, headerHeight, eventTimeout, animationInterval;
472+
var settings, anchor, toggle, fixedHeader, eventTimeout, animationInterval;
473473

474474

475475
//
@@ -506,10 +506,7 @@ if (window.Element && !Element.prototype.closest) {
506506
// Get the fixed header if not already set
507507
fixedHeader = document.querySelector(_settings.header);
508508
}
509-
if (!headerHeight) {
510-
// Get the height of a fixed header if one exists and not already set
511-
headerHeight = getHeaderHeight(fixedHeader);
512-
}
509+
var headerHeight = getHeaderHeight(fixedHeader);
513510
var endLocation = isNum ? anchor : getEndLocation(anchorElem, headerHeight, parseInt((typeof _settings.offset === 'function' ? _settings.offset(anchor, toggle) : _settings.offset), 10), _settings.clip); // Location to scroll to
514511
var distance = endLocation - startLocation; // distance to travel
515512
var documentHeight = getDocumentHeight();
@@ -650,18 +647,6 @@ if (window.Element && !Element.prototype.closest) {
650647

651648
};
652649

653-
/**
654-
* On window scroll and resize, only run events at a rate of 15fps for better performance
655-
*/
656-
var resizeThrottler = function (event) {
657-
if (!eventTimeout) {
658-
eventTimeout = setTimeout((function() {
659-
eventTimeout = null; // Reset timeout
660-
headerHeight = getHeaderHeight(fixedHeader); // Get the height of a fixed header if one exists
661-
}), 66);
662-
}
663-
};
664-
665650
/**
666651
* Destroy the current initialization.
667652
*/
@@ -672,7 +657,6 @@ if (window.Element && !Element.prototype.closest) {
672657

673658
// Remove event listeners
674659
document.removeEventListener('click', clickHandler, false);
675-
window.removeEventListener('resize', resizeThrottler, false);
676660
window.removeEventListener('popstate', popstateHandler, false);
677661

678662
// Cancel any scrolls-in-progress
@@ -683,7 +667,6 @@ if (window.Element && !Element.prototype.closest) {
683667
anchor = null;
684668
toggle = null;
685669
fixedHeader = null;
686-
headerHeight = null;
687670
eventTimeout = null;
688671
animationInterval = null;
689672

@@ -704,16 +687,10 @@ if (window.Element && !Element.prototype.closest) {
704687
// Selectors and variables
705688
settings = extend(defaults, options || {}); // Merge user options with defaults
706689
fixedHeader = settings.header ? document.querySelector(settings.header) : null; // Get the fixed header
707-
headerHeight = getHeaderHeight(fixedHeader);
708690

709691
// When a toggle is clicked, run the click handler
710692
document.addEventListener('click', clickHandler, false);
711693

712-
// If window is resized and there's a fixed header, recalculate its size
713-
if (fixedHeader) {
714-
window.addEventListener('resize', resizeThrottler, false);
715-
}
716-
717694
// If updateURL and popState are enabled, listen for pop events
718695
if (settings.updateURL && settings.popstate) {
719696
window.addEventListener('popstate', popstateHandler, false);

0 commit comments

Comments
 (0)