2010-08-30 33 views
0

我試圖讓各種各樣的旋轉木馬隱藏要素。一旦div移動到它的容器的最左邊和最右邊,我就會隱藏並顯示下一個寶貴的按鈕。jQuery的節目,並根據位置

我想我擁有了一切正確的關於計算的寬度,但由於某種原因,當你點擊按鈕,元素留隱患,不論何時他們應該被隱藏或顯示這應該規定條件的意見。

這裏是什麼我迄今的鏈接。點擊MoveLeft和MoveRight按鈕。 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(); 

     //Check distance from left 
     var position = $('.GalleryItem').position(); 
     var galleryItemLeft = position.left; 


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

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

     $(".Controls").live('click', function() { 
      if (galleryItemLeft >= "0") { 
       $('.MoveRight').hide(); 
      } 
      else { 
       $('.MoveRight').show(); 
      } 
     }); 

     if (galleryItemWidth == galleryWidth - galleryItemWidth) { 
      $('.MoveLeft').hide(); 
     } 


    }); 
</script> 

回答

0

看起來你設置所有變量的$內(文件)。就緒()調用

這意味着當他們被負載設置,他們沒有得到與每次點擊更新。

galleryItemLeftgalleryItemWidthgalleryItemWidth變量需要在每次點擊進行更新,所以我建議在每次點擊重新分配值(由轉讓移入直播功能)

編輯此外,作爲你最後的if語句是從任何點擊本功能排除它需要重新定位到實時點擊事件中。

克里斯

0

克里斯是正確的,代碼應該是這樣的:

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

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

    if(galleryItemWidth == galleryWidth - galleryItemWidth) { 
     $('.MoveLeft').hide(); 
    } 
}); 
+0

謝謝你們該訣竅!我的按鈕顯示後,他們應該點擊一下,有什麼辦法解決這個問題?按鈕不按我的條件究竟隱藏或顯示,它使得它看起來有種破=/ http://www.ehbeat.com/test – salmon 2010-08-31 07:17:38

+0

它看起來像發生動畫之前被計算galleryItemLeft。 – salmon 2010-08-31 07:34:42

+0

鮭魚,你是不是想隱藏MoveRight的和MoveLeft按鈕時,較大的幻燈片是不再可見,或更小的? – d2burke 2010-08-31 13:50:50