Skip to content
This repository was archived by the owner on Jan 24, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions src/skrollr.menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,17 @@
}
};

/*
Handles the click on a link. May be called without an actual click event.
When the fake flag is set, the link won't change the url and the position won't be animated.
*/
var handleLink = function(link, fake) {
//Don't use the href property (link.href) because it contains the absolute url.
var getLinkTargetTop = function(link)
{
var href = link.getAttribute('href');

//Check if it's a hashlink.
if(!/^#/.test(href)) {
return false;
}

//Now get the targetTop to scroll to.
var targetTop;

var menuTop = null;
var menuTop = 0;

if(!_handleLink) { //If there's a data-menu-top attribute and no handleLink function, it overrides the actuall anchor offset.
if(!_handleLink) //If there's a data-menu-top attribute and no handleLink function, it overrides the actuall anchor offset.
menuTop = link.getAttribute(MENU_TOP_ATTR);
} else { //If there's a handleLink function, it overrides the actuall anchor offset
else //If there's a handleLink function, it overrides the data-menu-top attribute
menuTop = _handleLink(link);
}

if(menuTop !== null) {
targetTop = +menuTop;
Expand All @@ -101,6 +89,23 @@
targetTop += +menuOffset;
}
}
return targetTop;
}
/*
Handles the click on a link. May be called without an actual click event.
When the fake flag is set, the link won't change the url and the position won't be animated.
*/
var handleLink = function(link, fake) {
//Don't use the href property (link.href) because it contains the absolute url.
var href = link.getAttribute('href');

//Check if it's a hashlink.
if(!/^#/.test(href)) {
return false;
}

//Now get the targetTop to scroll to.
var targetTop = getLinkTargetTop();

if(supportsHistory && !fake) {
history.pushState({top: targetTop}, '', href);
Expand Down Expand Up @@ -148,6 +153,7 @@
_animate = options.animate !== false;
_duration = options.duration || DEFAULT_DURATION;
_handleLink = options.handleLink;
_activeSubstract = options.activeSubstract || 0;

if(typeof _duration === 'number') {
_duration = (function(duration) {
Expand All @@ -174,13 +180,42 @@
jumpStraightToHash();
};

skrollr.menu.getActiveHref = function()
{
var links = [];
Array.prototype.forEach.call(document.getElementsByTagName('a'), function(link) {
if(link.getAttribute('data-skrollr-menu') !== null) {
var targetTop = getLinkTargetTop(link);

if(!links[targetTop]) links[targetTop] = {targetTop:targetTop, href:link.getAttribute('href')};
}
});

links.sort(function(a, b) {
return a.targetTop - b.targetTop;
});

var activeLinkHref = null;
var currentPosition = _skrollrInstance.getScrollTop();
var substractIsFunction = (typeof _activeSubstract === 'function');

Array.prototype.forEach.call(links, function(link) {
if(currentPosition >= (link.targetTop - ((substractIsFunction) ? _activeSubstract.call(link) : _activeSubstract))) {
activeLinkHref = link.href;
}
});

return activeLinkHref;
};

//Private reference to the initialized skrollr.
var _skrollrInstance;

var _easing;
var _duration;
var _animate;
var _handleLink;
var _activeSubstract;

//In case the page was opened with a hash, prevent jumping to it.
//http://stackoverflow.com/questions/3659072/jquery-disable-anchor-jump-when-loading-a-page
Expand Down