2013-02-07 51 views
-1

我試圖讓兩個元素在單擊時滑動。我希望第一個元素(一個img)從左側滑入,另一個元素(一個p標籤)從點擊一個按鈕(li)的右側滑入。然後,當點擊另一個按鈕時,我希望前兩個元素消失,並且接下來的兩個元素(對應於單擊的按鈕)滑入。單擊按鈕時,從左側和右側同時滑動元素?

是否有方法循環這些以便它們總是滑動

$( '#NAV-按鈕-1')上單擊(函數(){

$('#people-2').hide(); 
    $('#people-3').hide(); 
    $('#people-1').show(); 

$('.slideleft').animate({ 
    marginLeft: '103px' 
    }, 1000, function() { 
    // Animation complete. 
}); 
$('#slideright').animate({ 
    marginRight: '12px' 
    }, 1000, function() { 
    // Animation complete. 
}); 

})。。

+1

告訴我們,你有這麼遠 –

+0

$( '#導航鍵-1')的代碼。點擊(函數(){ $( '#人-2')。 hide(); $('#people-3')。hide(); $('#people-1')。show(); $('。slideleft')。animate({marginLeft:'103px '),1000,function(){ // Animation complete。 }); $('#slideright')。animated({marginRight:'12px'},1000,function(){ //動畫完成
});
});
我希望能夠循環這3個div,使他們總是
淡出和滑入。到目前爲止,但它的第一個
點擊後,它只是隱藏div,然後顯示新點擊已被點擊
Spade

+1

@Digby ...也許你應該用適當的格式將它編輯到你的文章中? – Doorknob

回答

0

這是一個入門示例。

我做了一個功能,你原有的JS

function doAnimation(id) { 
    $(id).children('.slideleft').fadeIn(function() { 
     $(this).animate({ 
      marginLeft: '103px' 
     }, 1000, function() { 
      // Animation complete. 
     }); 
    }); 
    $(id).children('.slideright').fadeIn(function() { 
     $(this).animate({ 
      marginRight: '12px' 
     }, 1000, function() { 
      // Animation complete. 
     }); 
    }); 
} 

的下面是一個被稱爲按鈕按下方法。它包含包含動畫項目的包裝的ID,並最終調用上述方法。

function animatePeople(id) { 
    // If there are visible people 
    if ($('.people:visible').length > 0) { 
     // Fade out the visible people and then process the following callback function 
     $('.people:visible').fadeOut(function() { 
      // Reset the margins 
      $('.people').css({ 
       'margin-left': '', 
       'margin-right': '' 
      }); 
      // Do the new animation 
      doAnimation(id); 
     }); 
    } else { 
     // Do the new animation 
     doAnimation(id); 
    } 
} 

最後,這些是按鈕的處理程序。

$('#nav-button-1').click(function() { 
    animatePeople('#people-holder-1'); 
}); 
$('#nav-button-2').click(function() { 
    animatePeople('#people-holder-2'); 
}); 

jsfiddle

+0

完美地工作,非常感謝您承擔我非常糟糕的溝通和腳本幫助。乾杯!!!! – Spade

+0

@Digby沒問題。如果我的答案是可行的解決方案,請將其標記爲已接受。 –