jquery切换,在切换期间防止单击

jquery切换,在切换期间防止单击,jquery,Jquery,给定以下jquery代码 jQuery.fn.sliding = function () { return this.each(function () { $(this).bind('ON_CONTENT_CHANGING', function (e) { $(this).block({ overlayCSS: { opacity: 1, color: '#000' }, time

给定以下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');
                    });
                });
            });
如果有人在执行切换功能时单击,是否有任何方法防止再次触发切换


我引用了一篇文章,但这项技术不起作用

您可以执行类似的操作,检查您制作的任何动画是否正在制作动画,如果是,请取消

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
}
});
将其置于每个切换功能的顶部:

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

您的代码示例中没有使用动画。你是故意的吗?否则,切换会在DOM中瞬间发生,无需担心执行中断。对不起,没有。那没用。如果你继续点击,仍然会崩溃。它仍然不能像我需要的那样工作,但这仍然是一个改进。它很容易崩溃,因为它现在的立场。。。我希望有更好的方法来做到这一点。@Stacey-如果你有一个我可以访问的示例页面,我会看一看。不幸的是,我没有这样的页面。问题是,虽然它更改了其他元素的大小,但它仍然接受单击。因此,如果你锤击元素,它就会堆积起来,使一切崩溃。我尝试过用jQuery block UI阻塞页面,但仍然不起作用。它不断地点击。@Stacey-我在两个函数中发布的返回码应该可以阻止点击发生任何事情,你是说动画还在排队?(例如,如果您在
if()
之后放置了一个警报,它将在动画制作时触发?@stacey-您是在
.toggle()
之前还是之后调用插件?事件按绑定顺序触发,因此请先尝试切换并替换my
return
返回false
并将
e.preventDefault()
更改为
e.preventDefault();e、 停止即时复制()取消它。