简体   繁体   English

在Jquery中使用带有百分比的if语句始终返回false

[英]Using an if statement with a percentage in Jquery always returns false

I've got a small amount of code that creates an endless carousel. 我有少量代码可以创建无休止的轮播。 When it's done in pixels, it works fine: 当以像素为单位完成时,它可以正常工作:

$('.left').click(function(){
        $('.box').animate({left: '+=100'}, 100, function(){
        var $last = $('.box').last();
        if ($last.css('left') == '100px') {
            $last.prependTo('.container').before('\n\r');
            $('.box').css('left','0px');
        }
    });
});

http://jsfiddle.net/tmyie/4CuLE/1/ http://jsfiddle.net/tmyie/4CuLE/1/

But if I change from pixels to percentages, it does not work and the if statement is false . 但是,如果我从像素更改为百分比,它工作和if statement假的

$('.left').click(function(){
        $('.box').animate({left: '+=5%'}, 200, function(){
        var $last = $('.box').last();
        if ($last.css('left') == '5%') {
            $last.prependTo('.container').before('\n\r').hide().fadeIn();
            $('.box').css('left','0%');
        }
    });
});

http://jsfiddle.net/tmyie/4CuLE/2/ http://jsfiddle.net/tmyie/4CuLE/2/

Would anyone know why this is? 谁知道这是为什么? I'm working with a responsive layout, so I need to keep the percentages. 我正在使用响应式布局,因此需要保持百分比。

According to jQuery's documentation , .css() return the computed value. 根据jQuery的文档.css()返回计算值。

Note that the computed style of an element may not be the same as the value specified for that element in a style sheet. 请注意,元素的计算样式可能与样式表中为该元素指定的值不同。 For example, computed styles of dimensions are almost always pixels, but they can be specified as em, ex, px or % in a style sheet. 例如,尺寸的计算样式几乎总是像素,但是可以在样式表中将它们指定为em,ex,px或%。 Different browsers may return CSS color values that are logically but not textually equal, eg, #FFF, #ffffff, and rgb(255,255,255). 不同的浏览器可能会返回逻辑上相等但文本上不同的CSS颜色值,例如#FFF,#ffffff和rgb(255,255,255)。

So you may not compare it with percent value. 因此,您可能无法将其与百分比值进行比较。

What's happening here is that, even if you originally set it with a percentage, jQuery's animate() method automatically converts it to pixels. 这里发生的是,即使您最初使用百分比设置了它,jQuery的animate()方法也会自动将其转换为像素。 Therefore, when you then call css() , you get a pixel value. 因此,当您随后调用css() ,将获得一个像素值。

As stated by @Nathan P. you may not compare percent values, but if you need to keep your CSS percentages, you can retrive the box width in pixels with $('.box').width() like this: 如@Nathan P.所述,您可能无法比较百分比值,但是如果需要保持CSS百分比,则可以使用$('.box').width()来检索以像素$('.box').width()单位的框宽,如下所示:

var boxWidth = $('.box').width()
$('.left').click(function(){
    $('.box').animate({left: '+='+boxWidth}, 100, function(){
        var $last = $('.box').last();
        if ($last.css('left') == boxWidth+'px') {
            $last.prependTo('.container').before('\n\r');
            $('.box').css('left','0px');
        }
    });
});

here is the fiddle 这是小提琴

Note all css dimensions are in percentage. 请注意,所有CSS尺寸均以百分比表示。

Hope it helps 希望能帮助到你

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

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