Skip to content

Instantly share code, notes, and snippets.

@roseg43
Created December 2, 2014 13:15
Show Gist options
  • Save roseg43/b239258bc6484a0b3739 to your computer and use it in GitHub Desktop.
Save roseg43/b239258bc6484a0b3739 to your computer and use it in GitHub Desktop.
Animated nav for single-page navigations
$(function() {
var $navItems = $('li');
var $offsets = new Array();
var $widths = new Array();
var $activeOffset,
$activeWidth,
newPos;
$navItems.each(function() {
$offsets.push($(this).position().left);
$widths.push($(this).width());
});
console.log($offsets);
console.log($widths);
$('.active-nav li').click(function() {
$navItems.each(function() {
$(this).removeClass('menu-item-active');
});
$(this).addClass('menu-item-active');
activeAnimation();
});
var activeAnimation = function() {
$activeOffset = $('.menu-item-active').position();
$activeWidth = $('.menu-item-active').width();
newPos = $activeOffset.left + ($activeWidth / 2) + 10;
console.log(newPos);
console.log($activeOffset);
$('#active-item').animate({
'left': newPos + 'px'
},500, 'swing')
}
});
@roseg43
Copy link
Author

roseg43 commented Dec 2, 2014

WIP. Live demo at http://jsfiddle.net/cjLzqLd9/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment