tags:

views:

91

answers:

3

I've created a simple background effect for some tabs on a page but don't want that effect to fire if the tab has the class 'current'.

I presume there is a way to do this using .hasClass

Here's what I'm using for the effect:

$('ul.htabs a').mouseover(function(){

 $(this).stop().animate(
  {backgroundPosition:"(0 -810px)"}, 
  {duration:150},
  {easing: 'easeOutCubic'})
 }).mouseout(function(){
 $(this).stop().animate(
  {backgroundPosition:"(0 -806px)"}, 
  {duration:150},
  {easing: 'easeInCubic'})
 });
+2  A: 
.mouseover(function(e) {

if ( $(this).hasClass('lol') ) {
  return;
}

if the element has a class of lol kill the function by returning immediately.

meder
While I agree with this answer I don't think you have offered up enough explanation.
Jonathan Park
A: 

Use the :not() selector:

$('ul.htabs:not(.current) a').mouseover(function(){
    // ... your code here
});

Now, if the class current is added or removed dynamically (Thanks Matt!) to the <a> tag, this will produce confusing results, as when the mouseover event is bound that list of elements is static. If you are also applying the class current on the fly, then use live() to bind the event, so that you only ever get anchors that at that moment have the current class applied:

$('ul.htabs:not(.current) a').live('mouseover', function(){
    // ... your code here
});
artlung
+3  A: 

You could also put it in the selector:

$('ul.htabs:not(.current) a').mouseover(function(){ ... });
garann
your answer was quicker... and more correct than mine! +1
Agos
@garann - I'm not sure this would work if the `current` class is being added dynamically to the elements. It does work if the event handlers are assigned using `.live()`. But using `.bind()` the handler stays with the element even if the selector is changed.
patrick dw
@patrick - this is likely to be a good solution for a static page load but you are correct. If this is intended to be done such that current gets applied dynamically this will not work without a live query. Also I hadn't realized live had been added to core, that's totally sweet. I need to catch up here.
Jonathan Park
I should note though that this is a good solution assuming that that the OP doesn't expect the handler to follow along with adding/removing the `current` class. I was making perhaps too much of an assumption.
patrick dw
@Jonathan - Yeah, I perhaps was making the classic blunder one makes when they assume. *(If you know what I mean.)*
patrick dw