Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple link containers #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
124 changes: 74 additions & 50 deletions jquery.singlePageNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ if (typeof Object.create !== 'function') {

(function($, window, document, undefined) {
"use strict";

var SinglePageNav = {

init: function(options, container) {

this.options = $.extend({}, $.fn.singlePageNav.defaults, options);
this.container = container;

this.container = container;
this.$container = $(container);
this.$links = this.$container.find('a');

if (this.options.filter !== '') {
this.$links = this.$links.filter(this.options.filter);
}
this.$links.addClass(this.options.linkClass);

this.$window = $(window);
this.$htmlbody = $('html, body');

this.$links.on('click.singlePageNav', $.proxy(this.handleClick, this));

this.didScroll = false;
Expand All @@ -47,7 +48,7 @@ if (typeof Object.create !== 'function') {
link = e.currentTarget,
$elem = $(link.hash);

e.preventDefault();
e.preventDefault();

if ($elem.length) { // Make sure the target elem exists

Expand All @@ -60,8 +61,8 @@ if (typeof Object.create !== 'function') {
}

self.setActiveLink(link.hash);
self.scrollTo($elem, function() {

self.scrollTo($elem, function() {

if (self.options.updateHash && history.pushState) {
history.pushState(null,null, link.hash);
Expand All @@ -73,20 +74,20 @@ if (typeof Object.create !== 'function') {
if (typeof self.options.onComplete === 'function') {
self.options.onComplete();
}
});
}
});
}
},

scrollTo: function($elem, callback) {
var self = this;
var target = self.getCoords($elem).top;
var called = false;

self.$htmlbody.stop().animate(
{scrollTop: target},
{
{scrollTop: target},
{
duration: self.options.speed,
easing: self.options.easing,
easing: self.options.easing,
complete: function() {
if (typeof callback === 'function' && !called) {
callback();
Expand All @@ -96,99 +97,122 @@ if (typeof Object.create !== 'function') {
}
);
},

setTimer: function() {
var self = this;

self.$window.on('scroll.singlePageNav', function() {
self.didScroll = true;
});

self.timer = setInterval(function() {
if (self.didScroll) {
self.didScroll = false;
self.checkPosition();
}
}, 250);
},
},

clearTimer: function() {
clearInterval(this.timer);
this.$window.off('scroll.singlePageNav');
this.didScroll = false;
},

// Check the scroll position and set the active section
checkPosition: function() {
var scrollPos = this.$window.scrollTop();
var currentSection = this.getCurrentSection(scrollPos);
if(currentSection!==null) {
this.setActiveLink(currentSection);
}
},
},

getCoords: function($elem) {
return {
top: Math.round($elem.offset().top) - this.options.offset
};
},

setActiveLink: function(href) {
var $activeLink = this.$container.find("a[href$='" + href + "']");

if (!$activeLink.hasClass(this.options.currentClass)) {
this.$links.removeClass(this.options.currentClass);
$activeLink.addClass(this.options.currentClass);
var $activeLinks = $("a." + this.options.linkClass + "[href$='" + href + "']");
var $links = $("a." + this.options.linkClass);

if (!$activeLinks.hasClass(this.options.currentClass)) {
$links.removeClass(this.options.currentClass);
$activeLinks.addClass(this.options.currentClass);
}
},

},

getHashes: function () {
var self = this,
$links = $("a." + this.options.linkClass),
hashes = [];

$links.each(function() {
if (hashes.indexOf(this.hash) === -1 && $(this.hash).length) {
hashes.push(this.hash);
}
});

// Sort hashes by page position, from top to bottom
hashes.sort(function(a, b) {
var aCoords = self.getCoords($(a)),
bCoords = self.getCoords($(b));

return aCoords.top - bCoords.top;
});

return hashes;
},

getCurrentSection: function(scrollPos) {
var i, hash, coords, section;

for (i = 0; i < this.$links.length; i++) {
hash = this.$links[i].hash;

if ($(hash).length) {
coords = this.getCoords($(hash));

if (scrollPos >= coords.top - this.options.threshold) {
section = hash;
}
var i, hash, coords, section,
hashes = this.getHashes();

for (i = 0; i < hashes.length; i++) {
hash = hashes[i];

coords = this.getCoords($(hash));

if (scrollPos >= coords.top - this.options.threshold) {
section = hash;
}
}

// get the last section if we reached the bottom of the page
// get the last section if we reached the bottom of the page
// before reaching the last section top
var pageBottom = $(document).height() - $(window).height();
if ( scrollPos == pageBottom ){
var numberOfLinks = this.$links.length;
var numberOfLinks = hashes.length;
if ( numberOfLinks > 0 ){
section = this.$links[ numberOfLinks - 1].hash;
section = hashes[ numberOfLinks - 1];
}
}

// The current section or the first link if it is found
return section || ((this.$links.length===0) ? (null) : (this.$links[0].hash));
return section || ((hashes.length===0) ? (null) : (hashes[0]));
}
};

$.fn.singlePageNav = function(options) {
return this.each(function() {
var singlePageNav = Object.create(SinglePageNav);
singlePageNav.init(options, this);
});
};

$.fn.singlePageNav.defaults = {
offset: 0,
threshold: 120,
speed: 400,
currentClass: 'current',
linkClass: 'singlePageNavLink',
easing: 'swing',
updateHash: false,
filter: '',
onComplete: false,
beforeStart: false
};

})(jQuery, window, document);