jQuery:是否可以在触发函数后停止单击或表单提交并恢复?

发布于 2024-10-18 09:29:20 字数 2097 浏览 1 评论 0原文

我正在尝试编写一个简单的脚本,该脚本可以有效地淡出页面内容,然后在新图层中混合/淡出,最后在这两个淡入淡出动画完成后,我需要确保激活原始触发器。

例如,我有一个 input type=submit 按钮,用于提交表单并将一些内容保存到数据库,然后返回到流程的下一步,还有一个简单的 < a> 标签充当“返回”按钮。

这是我的代码:

$.fn.pageFade = function( direction , speed , callback ) {
    // make sure the callback is actually a function
    var callback = ( typeof callback == 'function' ? callback : null );
    // closure for the other params
    var direction = ( direction ? direction : 'out' ),
        speed     = ( speed ? speed : 400 );
    // new elements to be added to dom and manipulated
    var content =   $('#main'),
        shade   =   $("<div />", {
                        'id' : 'shade',
                        'css': {
                            'position':'absolute',
                            'top':0,
                            'left':0,
                            'width':'100%',
                            'height':'100%',
                            'background-color':'#fff',
                            'z-index':'-1'
                        }
                    });

    // do stuff
    if ( direction == 'out' ) { 

        // fade out of content then next page
        $(shade)
            .appendTo('body').hide();
        content.fadeOut( speed );
        $(shade).fadeIn( speed*4 , callback );

    } else {

        // on next page fade in the content (would be fired on load)
        content.hide();
        $(shade)
            .appendTo('body');
        $(shade).fadeOut( speed*4 );
        content.fadeIn( speed , callback );

    }

};

// attach to the triggers (go back & next buttons)
$("[name='submitNext'],.go-back").click(function(e) {
    // stop the default click so that the animation can fire instead of changing the page
    e.preventDefault();
    // start the fade out action, in the callback fire the original click
    $(this).pageFade('out', 400, function() {
        $(this).trigger('click');
    });
});

上面的代码(不用说真的)由于点击事件的递归而中断。

我需要知道的是如何有效地“恢复”原始内容,即使我停下来直到动画触发后。

任何想法都会很棒。

I'm trying to write a simple script that effectively fades out the page content, then blends/fades in a new layer and then finally after those two fade animation have completed I need to make sure that the original trigger is activated.

So for example I have a input type=submit button that submits a form and saves some stuff to the database then returns you to the next step in the process, there is also a simple <a> tag that acts as the 'go back' button.

Here is my code:

$.fn.pageFade = function( direction , speed , callback ) {
    // make sure the callback is actually a function
    var callback = ( typeof callback == 'function' ? callback : null );
    // closure for the other params
    var direction = ( direction ? direction : 'out' ),
        speed     = ( speed ? speed : 400 );
    // new elements to be added to dom and manipulated
    var content =   $('#main'),
        shade   =   $("<div />", {
                        'id' : 'shade',
                        'css': {
                            'position':'absolute',
                            'top':0,
                            'left':0,
                            'width':'100%',
                            'height':'100%',
                            'background-color':'#fff',
                            'z-index':'-1'
                        }
                    });

    // do stuff
    if ( direction == 'out' ) { 

        // fade out of content then next page
        $(shade)
            .appendTo('body').hide();
        content.fadeOut( speed );
        $(shade).fadeIn( speed*4 , callback );

    } else {

        // on next page fade in the content (would be fired on load)
        content.hide();
        $(shade)
            .appendTo('body');
        $(shade).fadeOut( speed*4 );
        content.fadeIn( speed , callback );

    }

};

// attach to the triggers (go back & next buttons)
$("[name='submitNext'],.go-back").click(function(e) {
    // stop the default click so that the animation can fire instead of changing the page
    e.preventDefault();
    // start the fade out action, in the callback fire the original click
    $(this).pageFade('out', 400, function() {
        $(this).trigger('click');
    });
});

The above (needless to say really) breaks because of recursion on the click event.

What I need to know is how to effectively "resume" the original even I stopped until after my animations have fired.

Any ideas would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵情客 2024-10-25 09:29:20

不要试图恢复,而是尝试定义你希望在淡出后发生什么。

所以你的底部代码可能更像是:

// attach to the triggers (go back & next buttons)
$("[name='submitNext'],.go-back").click(function(e) {
    // stop the default click so that the animation can fire instead of changing the page
    e.preventDefault();
    // start the fade out action, in the callback fire the original click
    $(this).pageFade('out', 400, function() {
        $(this).closest("form").submit();//assuming this is what you want done after the fade.
    });
});

希望有帮助!

Instead of trying to resume, try defining what you want to have happen after your fade.

So your bottom code would be more like maybe:

// attach to the triggers (go back & next buttons)
$("[name='submitNext'],.go-back").click(function(e) {
    // stop the default click so that the animation can fire instead of changing the page
    e.preventDefault();
    // start the fade out action, in the callback fire the original click
    $(this).pageFade('out', 400, function() {
        $(this).closest("form").submit();//assuming this is what you want done after the fade.
    });
});

Hope that helps!

栀梦 2024-10-25 09:29:20

问题是,您的“触发”点击再次调用相同的函数。

您可以在表单元素上调用“提交”。 $("#myform").submit();

The problem is, that your "trigger" click is calling the same function again.

you could call "submit" on the form element. $("#myform").submit();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文