最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I get this if-statement with jQuery .css to work? - Stack Overflow

programmeradmin3浏览0评论

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');
        }       
    });
});
发布评论

评论列表(0)

  1. 暂无评论