Isolating jQuery functions
Question
I have some jQuery code that highlights a link when clicked, and changes the font size and color of certain links on the page. My problem is that some of the functions in my jQuery are executing on ALL the links on the site, rather than just the ones in the div that I am trying to target.
Here's my code so far:
$(document).ready(function() {
slide("#sliding-navigation", 22, 17, 175, .8);
});
function slide(navigation_id, font_out, font_in, time, multiplier) {
// Creates the target paths
var list_elements = navigation_id + " li.sliding-element";
var link_elements = list_elements + " a";
// Initiates the timer used for the initial sliding animation
var timer = 0;
// Create the beginning slide animation
$(list_elements).each(function(i) {
// updates timer
timer = (timer*multiplier + time);
$(this).animate({ marginLeft: "0" }, timer);
$(this).animate({ marginLeft: "15px" }, timer);
$(this).animate({ marginLeft: "0" }, timer);
});
// Creates the hover effect
$(link_elements).each(function(i) {
$(this).mouseenter(function () {
$(this).animate({ fontSize: font_out }, 200);
}),
$(this).mouseleave(function () {
$(this).animate({ fontSize: font_in }, 400);
}),
// Highlights active link
$('a').addClass('inactive');
$('a').click(function() {
$('ul#sliding-navigation a').stop().animate({ fontSize : font_in }, 500);
$(this).stop().animate({ fontSize : font_out }, 0);
$('a.active').mouseenter(function () {
$(this).stop().animate({ fontSize : font_out }, 200);
}).mouseleave(function() {
$(this).stop().animate({ fontSize : font_in }, 400);
}),
$('a.active').addClass('inactive');
$('a.active').removeClass('active');
$(this).removeClass('inactive');
$(this).addClass('active');
$(this).unbind('mouseleave mouseenter');
});
});
}
I'm almost positive that the problem is in this line:
$(this).stop().animate({ fontSize : font_out }, 0);
Is there a way to put something in the beginning of this line so that "this
" will only be active if it is a link in the "ul#sliding-navigation
" list?
Thanks for helping me out! I really appreciate it...
Solution
Why not just use the appropriate selector when creating the event handler?
$('#sliding-navigation a').click(function() {});
Also, I find using both a class and an id in a selector (ul #sliding-navigation) confusing.
OTHER TIPS
This selector
$('a').click(function(){
...
});
Is selecting every link on the page. If you only want to select the links in your navigation ul use this as kgiannakakis suggested:
$('#sliding-navigation a').click(function() {});
If you want to both select all links, but only change the font on certain ones you can try something like this:
if( $(this).parent().attr('id') == 'sliding-navigation' ){
$(this).stop().animate({ fontSize : font_out }, 0);
}
You could also shorten your code by making use of chaining like writing
$('a.active').addClass('inactive').removeClass('active');
$(this).removeClass('inactive').addClass('inactive').unbind('mouseleave mouseenter');
instead of
$('a.active').addClass('inactive');
$('a.active').removeClass('active');
$(this).removeClass('inactive');
$(this).addClass('active');
$(this).unbind('mouseleave mouseenter');