admin管理员组

文章数量:1390659

In my jQuery code (I know the if statement isn't jQuery), the CSS property "right" for the class "slider" does NOT equal 30, yet "container" is still fading out on mousedown.. What am I doing wrong?

I want it to be: if the class of slider has a "right" CSS property equal to 30 pixels, then fadeout the container.

$(document).ready(function() {
    $(".slider").mousedown(function() {
        if ($('.slider')
            .css({'right':30})
        ) {
        $('.container')
            .fadeOut('slow');
        }
    });
});

In my jQuery code (I know the if statement isn't jQuery), the CSS property "right" for the class "slider" does NOT equal 30, yet "container" is still fading out on mousedown.. What am I doing wrong?

I want it to be: if the class of slider has a "right" CSS property equal to 30 pixels, then fadeout the container.

$(document).ready(function() {
    $(".slider").mousedown(function() {
        if ($('.slider')
            .css({'right':30})
        ) {
        $('.container')
            .fadeOut('slow');
        }
    });
});
Share Improve this question edited Dec 20, 2010 at 1:58 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Dec 11, 2009 at 19:30 ExodusNicholasExodusNicholas 1,6445 gold badges16 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

$('.slider').css({'right':30}) returns an array object which always evaluates to true.

You want if ($('.slider').css('right') == "30px") ...

Maybe:

if($('.slider').css('right') == '30'){ ... }

There might be a unit, like px at the end of the value there. Not sure.

$(document).ready(function() {
    $(".slider").mousedown(function() {
        if ($('.slider').css('right') == 30) {
            $('.container').fadeOut('slow');
        }       
    });
});

本文标签: javascriptHow do I get this ifstatement with jQuery css to workStack Overflow