Last active
January 8, 2017 07:35
-
-
Save nishinoshake/a998b14b4ef1142c987fd1a5ef232cdb to your computer and use it in GitHub Desktop.
アクティブなページにクラス付与
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -------------------------- | |
* activateNav | |
* アクティブなページにis-activeクラス付与 | |
* ----------------------- */ | |
var activateNav = function() { | |
var currentPage = extractPageName(location.pathname); | |
$('#GlobalNavi li').each(function() { | |
if ( !$(this).find('a').length ) return false; | |
var anchorPage = extractPageName($(this).find('a').attr('href')); | |
if ( anchorPage === currentPage ) { | |
$(this).addClass('is-active'); | |
} | |
}); | |
function extractPageName(path) { | |
var pageName = path.match(/\/20\d\d\/(.*)\//); | |
return pageName ? pageName[1] : null; | |
} | |
} | |
// こちらでも | |
var currentPath = location.pathname; | |
var currentPage = currentPath.split('/').length >= 2 ? currentPath.split('/')[1] : null; | |
$('.side-nav').find('li').each(function() { | |
var anchor = $(this).find('a').attr('href'); | |
var anchorPage = anchor.split('/').length >= 2 ? anchor.split('/')[1] : null; | |
if ( currentPage === anchorPage ) { | |
$(this).addClass('active'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment