2010-08-31 168 views
1

嘿,我有一個div通過jquery動畫移動,我試圖計算divs的位置。位置是通過點擊來計算的,但是之後是動畫計算之前的位置,而不是動畫之後。jquery計算動畫後div的位置

Sooo基本上我將如何去動畫發生,然後有位置計算,並使隱藏適當的按鈕?

這是我到目前爲止的一個例子。實際上,除了在動畫完成之前計算var galleryItemLeft之外。 http://www.ehbeat.com/test/

<script type="text/javascript"> 
$(document).ready(function(){ 

//Check width of Gallery div 
var galleryWidth = $("#Gallery").innerWidth(); 

//Check width of GalleryItem 
var galleryItemWidth = $(".GalleryItem").innerWidth(); 

    $('.MoveRight').hide(); 

    $(".MoveRight").click(function(){ 
    $(".GalleryItem").animate({"left": "+=100px"}, "slow"); 
    $(".GalleryItem2").animate({"left": "+=140px"}, "slow"); 
}); 

$(".MoveLeft").click(function(){ 
    $(".GalleryItem").animate({"left": "-=100px"}, "slow"); 
    $(".GalleryItem2").animate({"left": "-=140px"}, "slow"); 
}); 

$(".Controls").live("click", function() { 
    position = $(".GalleryItem").position(); 
    galleryItemLeft = position.left; 

    if(galleryItemLeft >= "0") { 
     $('.MoveRight').hide();} 
    else{ 
     $('.MoveRight').show(); 
    } 

    if(galleryItemLeft <= galleryWidth - galleryItemWidth) { 
     $('.MoveLeft').hide();} 
     else{ 
     $('.MoveLeft').show(); 
    } 
}); 

}); 
</script> 

回答

3

.animate()可以讓你有一個回調函數,一旦動畫完成。

例如。

$('#clickme').click(function() { 
    $('#book').animate({ 
    opacity: 0.25, 
    left: '+=50', 
    height: 'toggle' 
    }, 5000, function() { 
    // Animation complete. do stuff here 
    }); 
}); 

找到一個div的位置,只是做:

var leftPos = $('#mydiv').css('left'); 
+0

感謝我給它一個嘗試,但它的STIL hidding和顯示按鈕點擊它彪之後。你能否看看並告訴我我做錯了什麼? http://www.ehbeat.com/test/ – salmon 2010-08-31 08:38:56

+0

沒關係我有它排序= D非常感謝您的幫助! – salmon 2010-08-31 08:57:14