File size: 2,857 Bytes
c02f046 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | /*!
* Plugin for skrollr.
* This plugin makes hashlinks scroll nicely to their target position.
*
* Alexander Prinzhorn - https://github.com/Prinzhorn/skrollr
*
* Free to use under terms of MIT license
*/
(function(document) {
var DEFAULT_DURATION = 500;
var DEFAULT_EASING = 'sqrt';
var TOP_OFFSET_ATTRIBUTE = 'data-menu-top';
/*
Since we are using event bubbling, the element that has been clicked
might not acutally be the link but a child.
*/
var findParentLink = function(element) {
//Yay, it's a link!
if(element.tagName === 'A') {
return element;
}
//We reached the top, no link found.
if(element === document) {
return false;
}
//Maybe the parent is a link.
return findParentLink(element.parentNode);
};
/*
Handle the click event on the document.
*/
var handleClick = function(e) {
var link = findParentLink(e.target);
//The click did not happen inside a link.
if(!link) {
return;
}
//Don't use the href property because it contains the absolute url.
var href = link.getAttribute('href');
//Check if it's a hashlink.
if(href.indexOf('#') !== 0) {
return;
}
//Now get the offset to scroll to.
var offset;
//If there's a data-menu-top attribute, it overrides the actuall anchor offset.
if(link.hasAttribute(TOP_OFFSET_ATTRIBUTE)) {
offset = +link.getAttribute(TOP_OFFSET_ATTRIBUTE);
} else {
var scrollTarget = document.getElementById(href.substr(1));
//Ignore the click if no target is found.
if(!scrollTarget) {
return;
}
offset = _skrollrInstance.relativeToAbsolute(scrollTarget, 'top', 'top');
}
//Now finally scroll there.
if(_animate) {
_skrollrInstance.animateTo(offset, {
duration: _duration,
easing: _easing
});
} else {
_skrollrInstance.setScrollTop(offset);
}
e.preventDefault();
};
/*
Global menu function accessible through window.skrollr.menu.init.
*/
var menu = function(skrollrInstance, options) {
_skrollrInstance = skrollrInstance;
skrollr = _skrollrInstance.skrollr;
options = options || {};
_duration = options.duration || DEFAULT_DURATION;
_easing = options.easing || DEFAULT_EASING;
_animate = options.animate !== false;
//Use event bubbling and attach a single listener to the document.
skrollr.addEvent(document, 'click', handleClick);
};
//Private reference to the initialized skrollr.
var _skrollrInstance;
var skrollr;
var _easing;
var _duration;
var _animate;
//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
window.setTimeout(function() {
if(location.hash) {
window.scrollTo(0, 0);
}
}, 1);
if(! "plugins" in window.JuSkrollr){
window.JuSkrollr[plugins] = {};
}
window.JuSkrollr.plugins.menu = menu;
}(document)); |