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)

  • My another blog

    I have stopped writing my link round-ups shortly after I started them. There is a reason for – I’d like to separate the content that I create out of content that was created by someone else. About a month ago, I’ve started my another...

  • Links Roundup #2

    This week was very productive on different updates in the Web world. The most noticeable were: False Simplicity by Dmitry Fadeyev tells us about very common design mistake of using icon-only navigation approach. In Top 10 Handy Web Typography Tools...

  • Links roundup #1

    After a short delay caused by job change (I am with Peopleclick Authoria now), I am going to continue publishing new posts at least once a week. In this week links roundup I have a few interesting articles: Jon Raasch from Smashing Magazine wrote an...