tags:

views:

102

answers:

2

Hey,

Any ideas why this isnt working?

I want the images in the sidebar to go opaque when hovered.

//Opaque image hover
        $('#sidebar ul li img').hover(function() {
            $(this).animate({opacity: 0.8}, 500);
        }, function() {
            $(this).animate({opacity: 1}, 500);
        });

<div id="sidebar"><!--Sidebar start-->
        <ul>
            <li><img src="images/darkroom.png" alt="Darkroom software" class="png"/></li>
            <li><a href="#"><img src="images/download.png" alt="Download" /></a></li>
            <li><a href="#"><img src="images/features.png" alt="Features"/></a></li>
            <li><a href="#"><img src="images/prices.png" alt="Prices"/></a></li>
            <li><a href="#"><img src="images/support.png" alt="support"/></a></li>
        </ul>
</div>

Thank you

+3  A: 

Hover usually takes two parameters, first is mouseenter, second is mouseleave, try:

$('#sidebar ul li img').hover(function() {
    $(this).animate({opacity: 0.8}, 500);
}, function() {
    $(this).animate({opacity: 0.2}, 500);
});

this assumes that original opacity was .2, you set it to what you want.

John Boker
Thanks John, but its not working, even when i take out the extra commas. I even try it with just 'img' but not working?
+3  A: 

As John Boker noted, hover() takes two functions.

Also, you have an extra comma in your animate call. I have a feeling that will affect IE.

This:

$(this).animate({opacity: 0.8,}, 500);

Should be:

$(this).animate({opacity: 0.8}, 500);

EDIT: added full ready() implementation.

$(document).ready(function() {
    $('#sidebar ul li img').hover(function() {
        $(this).animate({opacity: 0.8}, 500);
    }, function() {
        $(this).animate({opacity: 1}, 500);
    });
});
patrick dw
i didnt even see that comma, good catch.
John Boker
On most browsers that comma shouldn't have any effect, and the rest give you an error.
voyager
@voyager - Just tested it. It breaks in IE7. A few people still use that one. ;o)
patrick dw
Commas still wont solve the problem :S
Please modify your question and post a snippet of relevant CSS and HTML. There must be an error elsewhere.
patrick dw
Is your hover being called in jQuery's ready() function? Please see updated answer (in a second).
patrick dw
Works a charm, thankyou!
You're welcome! Glad it works. Remember to 'accept' my answer by clicking the checkbox. :o)
patrick dw