views:

51

answers:

2

I've created DIV.cb-toggle, when the user hovers over this div, it animates to Orange, when they hover off of this div, it animates back to gray, when the user clicks this div, it animates to blue, telling the user that it's been selected. So when it's NOT selected, it has mouseenter mouseleave animations, but when it's selected i want to unbind these events, I DO NOT want the hover event to work when it's been selected, only when it's not selected. What's the best way to do what i'm trying to accomplish? I came up with the code below but i'm pretty sure this is a horrible way to do it and i don't know what to do. thank you so much for any help.

my code:

$('.cb-toggle').toggle(function() { 
      $(this).animate({"background":"blue", "color":"#fff;"});      
      $(".cb-toggle").unbind("click.myfadee");
   }, function() {
      $(this).animate({"background":"gray", "color":"#fff;"});
      $('.cb-toggle').trigger('mouseenter');
   });
});

and I'm calling this bind:

$(".cb-toggle").bind("click.myfadee", function(){
      $(".cb-toggle").mouseenter(function() {
      $(this).animate({"background":"orange", "color":"#fff;"});
   }).mouseleave(function() {
      $(this).animate({"background":"gray", "color":"#fff;"});
   });
});

I need to keep the background color animation, it needs to fade.

+2  A: 

I would use CSS for the styling to simplify your whole setup without un/re-binding, like this:

.cb-toggle { background: blue; color: #fff; }
.cb-toggle.active { background: gray; }
.cb-toggle.active:hover { background: orange; }

Then you can do just this:

$('.cb-toggle').click(function() {
  $(this).toggleClass("active");
});

This approach also lets you offload all styling, colors, etc to the CSS, meaning no JavaScript changes are needed when you decide to tweak the colors or any other styling :)


Or, if you need to support IE6, add a .live() handler for the hover that triggers on only the ones with the .active class, like this:

$(".cb-toggle.active").live('mouseenter', function() {
  $(this).addClass('hover');
}).live('mouseleave', function() {
  $(this).removeClass('hover');
});

With matching CSS:

.cb-toggle.active.hover { background: orange; }
Nick Craver
+1 for using live(), although you should also look at the documentation for delegate()
RMorrisey
crap i posted the wrong code.. I need the background to animate between colors though.. so that wont work. code updated.
android.nick
@android.nick - Are you using the color plugin or jQuery UI for the color animation? I'm out for the day shortly, but jQuery UI lets you animate classes as well.
Nick Craver
A: 

You should probably just use a selected class. Also I'd recommend against using any of the .css() calls you have here. Just use classes.

$(".cb-toggle").bind("click.myfadee", function(){
  $(this).toggleClass('selected');
});

$('.cb-toggle').toggle(function() {
  var $this = $(this);
  if ( $this.is('.selected') ) {
    $this.css({"background":"blue", "color":"#fff;"});      
  }
}, function() {
  var $this = $(this);
  if ( $this.is('.selected') ) {
    $this.css({"background":"gray", "color":"#fff;"});
  }
});
BBonifield
i posted the wrong code that had .css() in it, it's supposed to be .animate(), how would i rework your code to fix it? grrr i'm sorry i'm so tired.
android.nick