简体   繁体   English

如何使用jQuery .css获得此if语句?

[英]How do I get this if-statement with jQuery .css to work?

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? 在我的jQuery代码中(我知道if语句不是jQuery),类“ slider”的CSS属性“ right”不等于30,但是在鼠标按下时“ container”仍在逐渐消失。 ?

I want it to be: if the class of slider has a "right" CSS property equal to 30 pixels, then fadeout the container. 我希望它是:如果滑块的类具有等于30像素的“正确” CSS属性,则淡出容器。

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

$('.slider').css({'right':30}) returns an array object which always evaluates to true. $('.slider').css({'right':30})返回一个始终为true的数组对象。

You want if ($('.slider').css('right') == "30px") ... 您想要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. 值的末尾可能有一个单位,例如px Not sure. 不确定。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM