/*!
* navscroll.js
* version: 1.2.0
* author: jeroen hammann
*
* copyright (c) 2014 jeroen hammann
* released under the mit license
*/
;(function ($, window, document, undefined) {
'use strict';
var pluginname = 'navscroll',
defaults = {
// the time it takes to scroll to the element (set this to 0 so it obviously won't show an animation).
scrolltime: 500,
// the class of the items which invokes the scroll animation. all anchor tags inside the element are clickable when the value is empty.
navitemclassname: '',
// set the height of the navigation (to use as offset). 'auto' let's the plugin determine the height automatically, a number determines the height in px.
navheight: 'auto',
// if your navigation hides and is used as a dropdown on small screens setting this to true hides the dropdown after a click.
mobiledropdown: false,
// additionaly you can insert the mobile nav's classname here, when left empty the plugin searches for a
in the same parent element.
mobiledropdownclassname: '',
// the window width, which functions as a breakpoint between desktop and mobile.
mobilebreakpoint: 1024,
// set to 'true' if you want to enable scrollspy.
scrollspy: false
};
function navscroll(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginname;
this.init();
}
navscroll.prototype = {
init: function() {
var self, options, element, navitem, navoffset, scrolltime;
self = this;
options = self.options;
element = self.element;
if (options.navitemclassname === '') {
navitem = $(element).find('a');
} else {
navitem = $(element).find('.' + options.navitemclassname);
}
if (options.navheight === 'auto') {
navoffset = $(element).height();
} else if (isnan(options.navheight)) {
throw new error ('\'navheight\' only accepts \'auto\' or a number as value.');
} else {
navoffset = options.navheight;
}
navitem.on('click', function(e){
var url, parts, target, targetoffset, targettop;
url = this.href;
parts = url.split('#');
target = parts[1];
if (target !== undefined) {
e.preventdefault();
targetoffset = $('#' + target).offset();
targettop = targetoffset.top;
}
if ($(this).data('scrolltime') !== undefined) {
scrolltime = $(this).data('scrolltime');
} else {
scrolltime = options.scrolltime;
}
if (options.mobiledropdown && $(window).width() >= 0 && $(window).width() <= options.mobilebreakpoint) {
if (options.mobiledropdownclassname === '') {
$(element).find('ul').slideup('fast');
} else {
$('.' + options.mobiledropdownclassname).slideup('fast');
}
}
$('html, body').stop().animate({
scrolltop: targettop - navoffset
}, scrolltime);
});
if (options.scrollspy) {
var scrollitems;
scrollitems = [];
navitem.each(function() {
var scrollitemid = $(this).attr('href');
scrollitems.push($(scrollitemid));
});
$(window).on('scroll', function () {
self.scrollspy(navitem, scrollitems);
});
self.scrollspy(navitem, scrollitems);
}
},
scrollspy: function(navitem, scrollitems) {
var scrollpos, changebounds, i, l;
scrollpos = (document.documentelement && document.documentelement.scrolltop) || document.body.scrolltop;
// changebounds = window.innerheight / 2 || document.documentelement.clientheight / 2;
l = navitem.length;
for (i = 0; l > i; i++) {
var item = scrollitems[i];
if (scrollpos > (item.offset().top-60)) {
navitem.removeclass('active');
$(navitem[i]).addclass('active');
}
}
}
};
$.fn[pluginname] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginname)) {
$.data(this, 'plugin_' + pluginname,
new navscroll(this, options));
}
});
};
})(jquery, window, document);