[英]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.