2010-04-01 78 views
1

考慮下面的jQuery代碼時按下..jQuery的切換,防止切換

jQuery.fn.sliding = function() { 

    return this.each(function() { 

     $(this).bind('ON_CONTENT_CHANGING', function (e) { 
      $(this).block({ 
       overlayCSS: { opacity: 1, color: '#000' }, 
       timeout: 800 
      }); 
     }); 

     $(this).bind('ON_CONTENT_CHANGED', function (e) { 
      $(this).sliding(); 
      $(this).unblock(); 
     }); 

     var $panels = $(this).find('.scrollContainer > div'); 
     var $container = $(this).find('.scrollContainer'); 
     var $resized = $panels.css({ 'width': $(this).width() - 44 }); 



     // if false, we'll float all the panels left and fix the width 
     // of the container 
     var horizontal = true; 

     // float the panels left if we're going horizontal 
     if (horizontal) { 
      $panels.css({ 
       'float': 'left', 
       'position': 'relative' // IE fix to ensure overflow is hidden 
      }); 

      // calculate a new width for the container (so it holds all panels) 
      $container.css('width', $panels[0].offsetWidth * $panels.length); 
     } 

     // collect the scroll object, at the same time apply the hidden overflow 
     // to remove the default scrollbars that will appear 
     var $scroll = $(this).find('.scroll').css('overflow', 'hidden'); 

     // handle nav selection 
     function selectNav() { 
      $(this) 
      .parents('ul:first') 
       .find('a') 
        .removeClass('selected') 
       .end() 
      .end() 
      .addClass('selected'); 
     } 

     $(this).find('.navigation').find('a').click(selectNav); 


     // go find the navigation link that has this target and select the nav 
     function trigger(data) { 
      var el = $(this).find('.navigation').find('a[href$="' + data.id + '"]').get(0); 
      selectNav.call(el); 
     } 

     if (window.location.hash) { 
      trigger({ id: window.location.hash.substr(1) }); 
     } else { 
      $('ul.navigation a:first').click(); 
     } 

     // offset is used to move to *exactly* the right place, since I'm using 
     // padding on my example, I need to subtract the amount of padding to 
     // the offset. Try removing this to get a good idea of the effect 
     var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) || 0) * -1; 


     var scrollOptions = { 
      target: $scroll, // the element that has the overflow 

      // can be a selector which will be relative to the target 
      items: $panels, 

      navigation: '.navigation a', 

      // allow the scroll effect to run both directions 
      axis: 'xy', 

      onAfter: trigger, // our final callback 

      offset: offset, 

      // duration of the sliding effect 
      duration: 500, 

      // easing - can be used with the easing plugin: 
      // http://gsgd.co.uk/sandbox/jquery/easing/ 
      easing: 'swing' 
     }; 

     $(this).serialScroll(scrollOptions); 
     $.localScroll(scrollOptions); 

     scrollOptions.duration = 1; 
     $.localScroll.hash(scrollOptions); 

    }); 
}; 

那麼這就是實際的HTML文件...

$('#toggle').toggle(function (e) { 
     if ($("#sidebar:animated, #canvas:animated").length) { 
      e.preventDefault(); 
      return; 
     } 

     $(".ui-sliding").trigger('ON_CONTENT_CHANGING'); 


     $('#sidebar').hide('slide', { direction: 'right' }, 100, function() { 

      $('#canvas').switchClass('span-17', 'span-24', 100, function() { 
       $(".ui-sliding").trigger('ON_CONTENT_CHANGED'); 
       $('#toggle').switchClass('close', 'open'); 
      }); 
     }) 
    }, 
      function (e) { 
       if ($("#sidebar:animated, #canvas:animated").length) { 
        e.preventDefault(); 
        return; 
       } 


       $(".ui-sliding").trigger('ON_CONTENT_CHANGING'); 

       $('#canvas').switchClass('span-24', 'span-17', 100, function() { 
        $('#sidebar').show('slide', { direction: 'right' }, 100, function() { 
         $(".ui-sliding").trigger('ON_CONTENT_CHANGED'); 
         $('#toggle').switchClass('open', 'close'); 
        }); 
       }); 
      }); 

有什麼辦法來防止從切換如果有人在執行該功能時點擊,會再次被解僱?

我引用了Tell jQuery to ignore clicks during an animation sequence並且該技術沒有工作。

+0

你不是你的代碼示例中使用動畫。你的意思是?否則,切換瞬間發生在DOM中,無需擔心執行中斷。 – 2010-04-01 17:58:48

+0

編輯我的代碼。對於那個很抱歉。 – Ciel 2010-04-01 18:00:56

回答

2

你可以做這樣的事情,檢查你的動畫是否有動畫和取消,如果是這樣。

把這個在每個切換功能的頂部:

if($("#sidebar:animated, #canvas:animated").length) { 
    e.preventDefault(); 
    return; 
} 
+0

它仍然不能像我需要的那樣工作,但這仍然是一種改進。現在很容易讓它崩潰......我希望有更好的方法來做到這一點。 – Ciel 2010-04-01 18:12:18

+0

@Stacey - 如果您有我可以訪問的示例頁面,我會查看一下。 – 2010-04-01 18:13:17

+0

不幸的是我沒有這樣一個頁面。問題是,雖然它改變了其他元素的大小,但它仍然接受點擊。所以如果你敲擊這個元素,它就會疊加起來並使一切崩潰。我試着用jQuery block UI阻止頁面,但它仍然不起作用。它繼續採取點擊。 – Ciel 2010-04-01 18:15:05

0
var toggling = false 
$('#toggle').toggle(function() { 
if(!toggling) 
{ 
toggling = true; 
    // perform something 
toggling = false; //or put it in a callback function of animate,show,hide,slide and fade 
} 
}, function() { 
if(!toggling) 
{ 
toggling = true; 
    // perform something 
toggling = false; //or put it in a callback function of animate,show,hide,slide and fade 
} 
}); 
+0

沒有。那沒有做到。如果繼續點擊,仍然崩潰。 – Ciel 2010-04-01 18:03:44