繁体   English   中英

如何根据CSS中的margin-left值使用jQuery隐藏元素?

[英]How can I hide an element using jQuery based on the value of it's margin-left in the CSS?

我正在开发一个旋转木马风格的滑块,并希望在ul的左边距等于-3480px时隐藏其中一个控件。 这是我的代码:

$(function(){
    if($("ul#listName").css("margin-left") == "-3480px"){
      $(".nextButton").hide();
    }    
});

它没有做任何事情,我不知道接下来该做什么。 有没有人有任何指示或建议?

var mm = $("ul#listName").css("margin-left");
if(mm == -3480+"px"){
  $(".nextButton").hide();
}
var leftMargin = parseInt($('ul#listName').css('margin-left'), 10);
if (leftMargin == -3480){
  $('.nextButton').hide();
}

我使用parseInt来显示一个替代方案,并避免在px后缀时你可能有的任何挂起。

var p = $(".your_class");
var position = p.position();
  if(p.position().left == '-3480px'){
$(".nextButton").hide();
}

根据您动画的样式属性,您应该检查。 还要确保在比较之前将值转换为int,因为css()方法也会给单位(px / em ..)及其值。

    if(parseInt($("ul#listName").css("margin-left"), 10) == -3480){
        $(".nextButton").hide();
    }    

如果您在任何动画回调上执行此代码,那么我建议您检查<=而不是==因为在动画期间该值可能不是那么完美。 试试这个。

    if(parseInt($("ul#listName").css("margin-left"), 10) <= -3480){
        $(".nextButton").hide();
    }  

我没有你的完整脚本可以使用,但我建议在$("ul#listName").css("margin-left")上做一个console.log() $("ul#listName").css("margin-left") ,看看它是否实际输出了你的想法确实。 如果你没有达到那个确切的值,我也会使用<=

我只是在这里做假设,但希望这会有所帮助。

暂无
暂无

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

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