Skip to content
This repository has been archived by the owner on Jan 24, 2018. It is now read-only.

Adds setActiveLink method. #51

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
22 changes: 22 additions & 0 deletions src/skrollr.menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
var DEFAULT_DURATION = 500;
var DEFAULT_EASING = 'sqrt';
var DEFAULT_SCALE = 1;
var DEFAULT_ACTIVE_CLASS = 'active';
var DEFAULT_MENU_SELECTOR = '.skrollr-menu';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a menu selector is needed. There can be links all over the website, not just inside a menu.


var MENU_TOP_ATTR = 'data-menu-top';
var MENU_OFFSET_ATTR = 'data-menu-offset';
Expand Down Expand Up @@ -121,6 +123,9 @@
duration: _duration(_skrollrInstance.getScrollTop(), targetTop),
easing: _easing
});

setActiveLink();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this only called in the if-block?


} else {
defer(function() {
_skrollrInstance.setScrollTop(targetTop);
Expand All @@ -130,6 +135,19 @@
return true;
};

var setActiveLink = function() {
if(window.location.hash && document.querySelector) {

// remove the active class from all links
var previousActiveLink = document.querySelector(_menuSelector + ' a.active');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a.active hardcoded? Also you need to use querySelectorAll because multiple links can point to the same hash.

previousActiveLink.className = previousActiveLink.className.replace(_activeClass,'');

// add the active class to the new active link
var newActiveLink = document.querySelector('a[href="' + window.location.hash + '"]');
newActiveLink.className = newActiveLink.className + ' ' + _activeClass;
}
};

var jumpStraightToHash = function() {
if(window.location.hash && document.querySelector) {
var link = document.querySelector('a[href="' + window.location.hash + '"]');
Expand Down Expand Up @@ -158,6 +176,8 @@
_duration = options.duration || DEFAULT_DURATION;
_handleLink = options.handleLink;
_scale = options.scale || DEFAULT_SCALE;
_activeClass = options.activeClass || DEFAULT_ACTIVE_CLASS;
_menuSelector = options.menuSelector || DEFAULT_MENU_SELECTOR;

if(typeof _duration === 'number') {
_duration = (function(duration) {
Expand Down Expand Up @@ -198,6 +218,8 @@
var _animate;
var _handleLink;
var _scale;
var _activeClass;
var _menuSelector;

//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