Skip to content

Commit

Permalink
Update Gumshoe to support use of headers instead of entire sections
Browse files Browse the repository at this point in the history
  • Loading branch information
mmistakes committed Mar 12, 2019
1 parent 28ee259 commit 1d49235
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion assets/js/main.min.js

Large diffs are not rendered by default.

54 changes: 46 additions & 8 deletions assets/js/plugins/gumshoe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* gumshoe v4.0.1
* gumshoejs v5.1.0
* A simple, framework-agnostic scrollspy script.
* (c) 2019 Chris Ferdinandi
* MIT License
Expand Down Expand Up @@ -134,15 +134,52 @@
};

/**
* Determine if an element is in the viewport
* Get the document element's height
* @private
* @returns {Number}
*/
var getDocumentHeight = function () {
return Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
};

/**
* Determine if an element is in view
* @param {Node} elem The element
* @param {Object} settings The settings for this instantiation
* @param {Boolean} bottom If true, check if element is above bottom of viewport instead
* @return {Boolean} Returns true if element is in the viewport
*/
var isInViewport = function (elem, settings) {
var isInView = function (elem, settings, bottom) {
var bounds = elem.getBoundingClientRect();
var offset = getOffset(settings);
return parseInt(bounds.top, 10) <= offset && parseInt(bounds.bottom, 10) > offset;
if (bottom) {
return parseInt(bounds.bottom, 10) < (window.innerHeight || document.documentElement.clientHeight);
}
return parseInt(bounds.top, 10) <= offset;
};

/**
* Check if at the bottom of the viewport
* @return {Boolean} If true, page is at the bottom of the viewport
*/
var isAtBottom = function () {
if (window.innerHeight + window.pageYOffset >= getDocumentHeight()) return true;
return false;
};

/**
* Check if the last item should be used (even if not at the top of the page)
* @param {Object} item The last item
* @param {Object} settings The settings for this instantiation
* @return {Boolean} If true, use the last item
*/
var useLastItem = function (item, settings) {
if (isAtBottom() && isInView(item.content, settings, true)) return true;
return false;
};

/**
Expand All @@ -152,8 +189,10 @@
* @return {Object} The content area and matching navigation link
*/
var getActive = function (contents, settings) {
var last = contents[contents.length-1];
if (useLastItem(last, settings)) return last;
for (var i = contents.length - 1; i >= 0; i--) {
if (isInViewport(contents[i].content, settings)) return contents[i];
if (isInView(contents[i].content, settings)) return contents[i];
}
};

Expand Down Expand Up @@ -403,9 +442,8 @@

/**
* Initialize the current instantiation
* @param {Object} options User options and settings
*/
publicAPIs.init = function (options) {
var init = function () {

// Merge user options into defaults
settings = extend(defaults, options || {});
Expand All @@ -429,7 +467,7 @@
// Initialize and return the public APIs
//

publicAPIs.init(options);
init();
return publicAPIs;

};
Expand Down

0 comments on commit 1d49235

Please sign in to comment.