2012-03-09 85 views
0

我正在使用jQuery製作兩個圖像箭頭來滑動DIV。我注意到,點擊垃圾郵件時,DIV的利潤率已經完全搞砸了,他們無法回到定義的位置。隨着一些搜索我結束了下面的代碼:jQuery停止點擊垃圾郵件

var is_clicked = false; 
    $("#arrow_r").click(function(){ 
     if (is_clicked == true){ 
      return; 
     } 
     is_clicked = true; 
     var $img_block = $('.photoBlock:visible');//current 
     if ($img_block.next().length > 0){ 
      $img_block.animate({ 
        marginLeft:-$('#server_photo_listing').outerWidth() 
       },function(){ 
        is_clicked = true; 
        var $img_block = $('.photoBlock:visible');//current 
        var $img_block_next = $img_block.next();//next 
        $img_block.css("display","none"); 
        $img_block_next.css("display","inherit"); 
        $img_block_next.animate({ 
         marginLeft:0 
        } 
        ); 
        is_clicked = false; 
        }); 
     } 

    }); 

    $("#arrow_l").click(function(){ 
     if (is_clicked == true){ 
      return; 
     } 
     is_clicked = true; 
     var $img_block = $('.photoBlock:visible');//current 
     if ($img_block.prev().length > 0){ 
      $img_block.animate({ 
        marginLeft:$('#server_photo_listing').outerWidth() 
       }, function(){ 
        is_clicked = true; 
        var $img_block = $('.photoBlock:visible');//current 
        var $img_block_prev = $img_block.prev();//previous 
        $img_block.css("display","none"); 
        $img_block_prev.css("display","inherit"); 
        $img_block_prev.animate({ 
         marginLeft:0 
        } 
        ); 
        is_clicked = false; 
        }); 
     } 
    }); 

雖然工作確定最初爲一個箭頭,當我用它在第二此外,一些惡意點擊測試變量is_clicked後仍然true使滑動停止。

我不得不說,箭頭也用在display:none上的不同DIV中 - 以防萬一它具有任何重要性。此外,以上所有代碼都包含主要$(function(){});以內的更多功能 - 同樣,如果它有任何重要性。

任何想法?

+0

我會通過整合這些處理成一個單一的處理器開始 - 他們幾乎是一樣的。 – karim79 2012-03-09 14:37:51

回答

0

您可以在此元素上綁定和解除綁定事件。

首先創建2個函數,在事件中您取消綁定click事件以及動畫完成後您重新綁定事件。

var go_right = function(){ 
    $("#arrow_r").unbind('click'); 
    var $img_block = $('.photoBlock:visible');//current 
    if ($img_block.next().length > 0){ 
     $img_block.animate({ 
      marginLeft:-$('#server_photo_listing').outerWidth() 
     },function(){ 
      var $img_block = $('.photoBlock:visible');//current 
      var $img_block_next = $img_block.next();//next 
      $img_block.css("display","none"); 
      $img_block_next.css("display","inherit"); 
      $img_block_next.animate({ 
       marginLeft:0 
      }, function() { 
       $("#arrow_r").bind('click', go_right); 
      }); 
     }); 
    } 
}; 
var go_left = function(){ 
    $("#arrow_l").unbind('click'); 
    var $img_block = $('.photoBlock:visible');//current 
    if ($img_block.prev().length > 0){ 
     $img_block.animate({ 
     marginLeft:$('#server_photo_listing').outerWidth() 
     }, function(){ 
      var $img_block = $('.photoBlock:visible');//current 
      var $img_block_prev = $img_block.prev();//previous 
      $img_block.css("display","none"); 
      $img_block_prev.css("display","inherit"); 
      $img_block_prev.animate({ 
       marginLeft:0 
      }, function() { 
       $("#arrow_l").bind('click', go_left); 
      }); 
     }); 
    } 
}; 

$("#arrow_r").bind('click', go_right); 
$("#arrow_l").bind('click', go_left); 

希望能夠幫助您

+0

謝謝你的回覆。不幸的是我有同樣的結果。當我在一段時間後點擊其中一個箭頭時,都會停止工作。 – 2012-03-09 15:00:08