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

Fix for first nav link not being made active again when returning to it #22

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
75 changes: 40 additions & 35 deletions jquery.singlePageNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ 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');

Expand All @@ -34,7 +34,7 @@ if (typeof Object.create !== 'function') {

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

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

this.didScroll = false;
Expand All @@ -47,7 +47,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 +60,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 +73,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,79 +96,84 @@ 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)) {
// Fix for first link not being set back to ACTIVE when scrolling back to top
// or clicking on first link in nav after going to another section. For some
// reason hasClass() returns TRUE matching a blank string on the first link
// link only. Still not entirely sure why, but this fixes it.
var $classList = $activeLink.attr('class');

if (!$activeLink.hasClass(this.options.currentClass) || (typeof $classList === 'undefined' || $classList === '')) {
this.$links.removeClass(this.options.currentClass);
$activeLink.addClass(this.options.currentClass);
}
},
},

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;
}
}
}

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

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

$.fn.singlePageNav.defaults = {
offset: 0,
threshold: 120,
Expand All @@ -180,5 +185,5 @@ if (typeof Object.create !== 'function') {
onComplete: false,
beforeStart: false
};

})(jQuery, window, document);