我正在开发一个走马灯样式的轮播器,并希望在
- 左边距等于-3480px时隐藏其中一个控制器。这是我的代码:
$(function(){
if($("ul#listName").css("margin-left") == "-3480px"){
$(".nextButton").hide();
}
});
但它并没有执行任何操作,我不确定接下来该怎么办。有人能提供指导或建议吗?
我正在开发一个走马灯样式的轮播器,并希望在
$(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();
}
position
返回一个数字对象。 - pimvdb根据你正在执行动画的样式属性来检查。另外,在比较之前,确保将值转换为 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()
,以查看它是否实际输出你认为的内容。我还建议使用<=,以防你没有达到精确值。
我只是根据假设在工作,但希望这会有所帮助。