2014-01-08 28 views
-1

我想知道你是否可以幫助我,我試圖創建如果基於互動在我的網站上,但可悲的是代碼不起作用,我想不出爲什麼。這個想法是點擊一個按鈕將其向左移動,然後當另一個按鈕被點擊到它的下一個設置位置時,它將再次返回。jQuery的 - 如果和其他如果爲marginLeft的

if($("#featImgOne").css("marginLeft")=='0px'){ 
    $('#featNext').click(function(){ 
     $('#featImgOne').animate({marginLeft:"-960px"}); 
    }); 
    } 
    else if($("#featImgOne").css("marginLeft")=='-960px'){ 
    $('#featPrev').click(function(){ 
     $('#featImgOne').animate({marginLeft:"0px"}); 
    }); 
    } 

只是要注意的第一次點擊的作品,但第二個習慣,所以我提前困惑

感謝,希望有人能幫助

回答

1

你必須移動如果單擊處理內部consitions

$('#featNext').click(function() { 
    if ($("#featImgOne").css("marginLeft") == 0) { 
     $('#featImgOne').animate({ 
      marginLeft: "-960px" 
     }); 
    } 
}); 
$('#featPrev').click(function() { 
    if ($("#featImgOne").css("marginLeft") != 0) { 
     $('#featImgOne').animate({ 
      marginLeft: "0px" 
     }); 
    } 
}); 
+0

非常感謝!我剛剛意識到我有多愚蠢,工作太多,睡得太少-_- 非常感謝:) –

+0

** + 1 **您已經得到答案*第一!* – Cilan

0

你期待什麼也不會發生,因爲第二個click事件永遠不會被添加,因爲在開始時margin-left不是那個值。通過移除條件語句解決這個問題:

$('#featNext').click(function() { 
    if ($("#featImgOne").css("marginLeft") == 0) { 
     $('#featImgOne').animate({ 
      marginLeft: "-960px" 
     }); 
    } 
}); 
$('#featPrev').click(function() { 
    if ($("#featImgOne").css("marginLeft") != 0) { 
     $('#featImgOne').animate({ 
      marginLeft: "0px" 
     }); 
    } 
}); 
+0

謝謝你還有男人,工作過的魅力,不敢相信我多麼dopey:') –

0

你將必須添加某種循環。嘗試一段時間循環。 **while**//add a condition