Easy Rollovers!

One day I was extremely lazy to create new graphic for rollovers for a navigation menu, and moreover, I was also tired of using simple text effects such as color change or changing the text-decoration property. After a quick tinkering, I came up with interesting idea of using jQuery to “generate” rollovers “on the fly”. The idea is based on changing element’s siblings instead of changing element itself, so the element pops among its siblings.

For example, I have this simple navigation menu:

<ul id="example">
	<li><a href="#">Link 1</a></li>
	<li><a href="#">Link 2</a></li>
	<li><a href="#">Link 3</a></li>
	<li><a href="#">Link 4</a></li>
</ul>

Using a small piece of jQuery code, I change boring color-changing or text-decoration:underline rollovers into absolutely new look and feel:

jQuery("#example").children().hover(function () {
     jQuery(this).siblings().stop().fadeTo(800, 0.4);
 },
 function () {
      jQuery(this).siblings().stop().fadeTo(700, 1);
});

Working example:

Source:

        <style>
                #example {border:1px solid #f4f4f4; margin-left:0 !important; padding-left:10px !important; width:150px; margin:0 auto !important}
                #example li a:hover {color:inherit}
        </style>
        <script type="text/javascript">
            jQuery(function(){
                jQuery("#example").children().hover(function () {
                    jQuery(this).siblings().stop().fadeTo(800, 0.4);
                },
                function () {
                    jQuery(this).siblings().stop().fadeTo(700, 1);
                });
            })
            </script>
        <ul id="example">
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li><a href="#">Link 3</a></li>
            <li><a href="#">Link 4</a></li>
        </ul>
        

It also looks badass for a horizontal menu:

Source:

            <style>
                #example2 {overflow:hidden; height: 100%; width:210px; margin:0 auto !important}
                #example2 li a:hover {color:inherit}
                #example2 li {float:left; margin-right:3px; display:block}
                #example2 li a {display:block; background:#f4f4f4; padding:3px 5px; color:#333; font:bold 13px Arial}
                #example2 li a:hover {color:#333}
            </style>
            <script type="text/javascript">
                jQuery(function(){
                    jQuery("#example2").children().hover(function () {
                        jQuery(this).siblings().stop().fadeTo(800, 0.4);
                    },
                    function () {
                        jQuery(this).siblings().stop().fadeTo(700, 1);
                    });
                })
            </script>
            <ul id="example2">
                <li><a href="#">Link 1</a></li>
                <li><a href="#">Link 2</a></li>
                <li><a href="#">Link 3</a></li>
                <li><a href="#">Link 4</a></li>
            </ul>
        

Summarizing this article, I wrap up this functionality into a standard jQuery plugin, which you can use on your sites

    jQuery.fn.fadingSiblings = function() {
        return this.each(function(i) {
            jQuery(this).children().hover(function () {
                jQuery(this).siblings(":visible").stop().fadeTo(800, 0.4);
            },
            function () {
                jQuery(this).siblings(":visible").stop().fadeTo(700, 1);
            });
        })
    };

Usage:

jQuery(function(){
   jQuery(selector).fadingSiblings();
})

 
 
 

5 Comments

  1. Adam2010-06-20 16:26:10

    I don't see anything happening in your demos.. is it broken?
    OSX Chrome / FF

     
  2. Andrei Zharau2010-06-20 18:37:20

    Adam, thanks for heads up. Looks like transition to WP3 wasn't smooth - it overwritten a few necessary js files . Should be working now, except example with images.

     
  3. Jon2010-06-22 21:44:57

    Hi Andrei-

    the Fading Siblings Slide in the right sidebar is linking to this page EasyRollovers.

    AWESOME work on this site btw!

    thanks
    Jon

     
  4. Jon2010-06-22 21:47:01

    whoops! I just realized this IS the Sibling Slide! sorry about that.

    Maybe it is the graphic that you are using on the sidebar image that threw me off - I guess I was expecting to see a sliding bar or something like that...

    thanks again
    Jon

     
  5. Andrei Zharau2010-06-22 21:57:43

    Jon, thank you for your comments :).
    I will add another Siblings example soon. For my slide/thumbnail I just used an example from one of the sites where I have used this script: http://pla.libertyconcepts.com/ (scroll to the bottom)

     

Leave a Reply





 

0 Trackbacks

Trackback URL for this post:
http://o2v.net/blog/easy-rollovers/trackback

1 Pings

  1. RollOver con JQuery :digilabs.com.ar2010-06-28 04:26:20

    [...] Más información y demos en: http://o2v.net/blog/easy-rollovers [...]

Latest Blog Posts (read all)

  • jQuery FormLabels 1.0 Released

    My previous article about jQuery FormLabels 1.0RC2 was very popular (>2000 visits daily – more than on any other publication I have ever had) and I’ve got tons of feedback. I would like to thank all people who visited my site, left a comment,...

  • jQuery FormLabels Plugin

    Demo Download Overview The idea of using captions on form input boxes is not new, but all previous methods of providing this functionality have a few disadvantages. The jQuery FormLabels Plugin is a result of working with hundreds of different clients,...

  • New version of Custom Facebook Fan Box Widget was released

    Good News! The new version of the Custom Facebook Fan Box Widget was just released. New in this version: + added a new option likeLink and functionality that allows to specify an element or elements that should perform action of a normal Facebook Like...