2011-05-24 76 views
-1

我正試圖讓我的javascript/jquery滑塊按鈕停用,當它到達滾動圖像的末尾時(當圖像一直移動到右側時,MoveRight按鈕必須是停用,只剩下MoveLeft按鈕有效,移動LeftButton也是一樣),任何人都可以幫忙嗎?林不知道如果即時使用 .attr()和removeAttr()正確。下面粘貼了我的代碼。停用畫廊滑塊上的按鈕

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

//Check width of Gallery div 
var galleryWidth = $("#Gallery").innerWidth(); 
//Check width of GalleryItem 
var NoListed = $("ul.GalleryItem li").length; 
var galleryItemWidth = 1881;  

$('.MoveRight') 
$('.GalleryItem').css('width', galleryItemWidth); 

//Check width of Gallery div on resize 
$(window).resize(function(){ 
    var galleryWidth = $("#Gallery").innerWidth(); 
    }); 

$('.MoveLeft').click(function() { 
    $(".GalleryItem2").animate({"left": "-=350px"}, "slow"); 
    $(".GalleryItem3").animate({"left": "-=280px"}, "slow"); 
    $(".GalleryItem").animate({ 
    left: '-=230', 
    }, "slow", function() { 
    position = $(".GalleryItem").position(); 
    galleryItemLeft = position.left; 
    if(galleryItemLeft <= galleryWidth - galleryItemWidth) { 
     $('.MoveLeft').removeAttr('disabled');} 
     else{ 
     $('.MoveLeft').attr('disabled','disabled'); 
     $('.MoveRight').attr('disabled','disabled'); 
    } 
    }); 
}); 

$('.MoveRight').click(function() { 
    $(".GalleryItem2").animate({"left": "+=350px"}, "slow"); 
    $(".GalleryItem3").animate({"left": "+=280px"}, "slow"); 
    $(".GalleryItem").animate({ 
    left: '+=230', 
    }, "slow", function() { 
    position = $(".GalleryItem").position(); 
    galleryItemLeft = position.left; 
    if(galleryItemLeft >= "0") { 
     $('.MoveLeft').removeAttr('disabled');} 
     else{ 
     $('.MoveLeft').attr('disabled','disabled'); 
     $('.MoveRight').attr('disabled','disabled'); 
    } 
    }); 
}); 

}); 


</script> 
+0

[停用按鈕在圖庫滑塊上]的可能重複(http://stackoverflow.com/questions/6110654/deactivate-button-on-gallery-slider) – 2011-05-24 13:22:21

回答

0

首先,我看你做以下內容:

//Check width of Gallery div on resize 
$(window).resize(function(){ 
    var galleryWidth = $("#Gallery").innerWidth(); 
}); 

我喜歡你保持你的局部變量,但var關鍵字應該在這種情況下可以省略這一事實。你已經在包含這個調整大小回調的函數中聲明瞭galleryWidth,所以通過在這裏省略var關鍵字,該值將被分配給你在腳本中使用的變量,使用這個關鍵字va創建一個新的galleryWidth變量在調整大小回調中可見,因此從未使用過。

現在

的啓用和按鈕的禁用:

你可以保持一個CURRENTITEM計數器,每並將MoveRight或moveleft按鈕clikced時間更新。當此計數器達到0時,您將禁用移動左鍵,當它達到項目數時,您將禁用移動按鈕,否則啓用它們。