Search code examples
javascriptjqueryrollover

javascript image rollover


Hi I have a question about image rollover but it's not how you'd think.

I've seen an example http://www.asos.com/ and click the home button(top nav far left)

You can see that when you rollover an image that image stays highlighted and the others darken.

I know this has been done with jquery but the code is very messy. Has anyone seen this before or knows how to do it?

Thanks


Solution

  • Fast solution (it can be tuned shorter i guess):

    <div class="images">
        <img src="http://www.google.com/google.jpg" />
        <img src="http://www.google.com/google.jpg" />
        <img src="http://www.google.com/google.jpg" />
        <img src="http://www.google.com/google.jpg" />
    </div>
    
    <script type="text/javascript">
    $().ready(function(){
        $('.images img').hover(
            function(){
                $(this).parents('.images').find('img').not(this).animate({"opacity": "0.3"}, 200);
                $(this).animate({"opacity": "1"}, 200);
            }, 
            function(){
                $(this).animate({"opacity": "1"}, 200);
            }
        );
    $('.images').bind('mouseleave',function(){$(this).find('img').animate({"opacity": "1"}, 200);});
    });