2009
01.30

One jQuery feature that I’ve always wished was part of Prototype is the hover function. The hover function takes in two parameters: the first one is a function for when the mouse starts to hover over the desired element, and the second one is a function for when the mouse leaves the element:

$("#hover-element").hover(
  function() {
    $(this).addClass('my-hover');
  },
  function(){
    $(this).removeClass('my-hover');
  }
);

This is very simple and allows for hover effects on any html element (Important since IE is unable to apply the :hover style to elements other than links). You definitely can do the same using Prototype, but you have to use the mouseover and mouseout events.

$("hover-element").observe('mouseover', function() {
  this.addClassName('my-hover');
});
$("hover-element").observe('mouseout', function() {
  this.removeClassName('my-hover');
});

It’s not pretty syntax but it works. These two techniques, however, have one major difference between them.  jQuery’s hover method will only fire ONCE when you enter (mouseover) the desired item, even if it has multiple CHILDREN objects. Using Prototype’s observe method, the mouseover and mouseout events will fire MULTIPLE times if the object you are hovering over has multiple children; firing every time as you mouse over each one of them.  This is not a big deal if you are doing something simple like adding a class name to the object. In fact you might not even notice it since it happens so fast, but this is a big problem if you are trying to do something more complicated, like triggering an animation. I found this out while trying to create a simple animated drop-down menu for a website I was building (tutorial coming soon) and had a hard time figuring out what to do. I though about porting jQuery’s hover method to prototype and almost started doing it when I found this:

Protohover: a simple hover plugin for the prototype.js library

Luckily, it seems like someone read my mind and already did me the favor. It works exactly as you would expect. Just add the js file to your website and your syntax should be very similar to jQuery’s:

$("hover-element").hover(
  function(event) {
    $(this).addClass('my-hover');
  },
  function(event){
    $(this).removeClass('my-hover');
  }
);

I love me some Google.

6 comments so far

Add Your Comment
  1. Uhhh.. Thx for this ;) I couldn’t find anything.

  2. I too had the same requirement as you, I had to make a drop down contact form like one on
    http://www.fuelyourcreativity.com/

    I searched a lot and then ended up on the protohover. I implemented that, but thats not working pretty great.

    I personally am a prototype fan, but I just realized that prototype don’t have as much plugins as jQuery. If you manage to find some other prototype plugin, please also share with me.

    Have a nice day
    DC

  3. I’m glad to see someone was using Protohover. It is basically obsolete now that Prototype.js has finally added mouseenter/leave support.

  4. ok so does this mean the same thing as wanting to implement two elements as the hover trigger due to not knowing which of the two the viewer will hover over first that will cause one animation factor? example: $(this,or that).hover(this will happen).
    my problem is that when both items are hovered over before the animation finishes the animation happens again which is something i do not want to happen.

  5. No, I would say they are different problems unless the one of the elements you are talking about contains the other element.

    In your case it seems like you will have to set up some sort of flag to indicate that whether the animation has been initiated and then reset it once the animation is over.

  6. Thank you very much for bringing this to my attention. We’ve now included this in our script and it’s helped fix a lot of headaches.

    Cheers