jQuery fadeOut/fadeIn 未按预期工作?

发布于 2024-12-07 19:18:22 字数 354 浏览 1 评论 0原文

我试图根据用户的选择一次显示一个fieldset。理论上,所有字段集应先隐藏,然后显示选定的字段集。我正在使用 jQuery 的 fadeOut 和 'fadeIn` 函数。

您可以在此处看到这个小提琴。

但效果并不好。怎么了?当您更改所有权类型时,首先显示两个字段集,然后它们变暗并淡出,然后出现预期的字段集。然而,期望的行为是,在更改所有权类型时,当前可见的字段集会淡出,然后预期的字段集会淡入。

I'm trying to show one fieldset at a time, based on the user's selection. The theory is that, all fieldsets should hide first, then the selected fieldset should be shown. I'm using jQuery's fadeOut and 'fadeIn` functions.

You can see a fiddle of this here.

But it doesn't work just fine. What's wrong? When you change the ownership type, first two fieldsets are shown, then they dim and fade out, and then the intended fieldset appears. However, the desired behavior is that, on change of the ownership type, the currently visible fieldset simply fade out, and then the intended fieldset fade in.

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

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

发布评论

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

评论(3

羁〃客ぐ 2024-12-14 19:18:22

您还可以使用“承诺”http://api.jquery.com/jQuery.when/ 确保淡入发生在 fieldset 淡出之后。

$(function() {
    var ownershipType = $('#ownershipType').first();
    var fieldsets = $('fieldset');
    ownershipType.change(function() {
        var promise = fieldsets.fadeOut(2000);
        $.when(promise).then( function () {$('fieldset[data-ownership-type=' + ownershipType.val() + ']').fadeIn('fast');
        });
    });
});

http://jsfiddle.net/DtaHQ/26/

You can also use a 'promise' http://api.jquery.com/jQuery.when/ to be sure that fadein happens after when fieldset has faded out.

$(function() {
    var ownershipType = $('#ownershipType').first();
    var fieldsets = $('fieldset');
    ownershipType.change(function() {
        var promise = fieldsets.fadeOut(2000);
        $.when(promise).then( function () {$('fieldset[data-ownership-type=' + ownershipType.val() + ']').fadeIn('fast');
        });
    });
});

http://jsfiddle.net/DtaHQ/26/

雨巷深深 2024-12-14 19:18:22

问题是您已经隐藏了 fieldset 并且对于这些元素,fadeOut 的动画会立即触发,因为它已经隐藏了。

尝试更改为:

$(function() {
    var ownershipType = $('#ownershipType').first();
    ownershipType.change(function() {
        $('fieldset:visible').fadeOut(2000, function() {
            $('fieldset[data-ownership-type=' + ownershipType.val() + ']').fadeIn('fast');
        });
    });
});

代码:http://jsfiddle.net/DtaHQ/20/

The problem is that you already have hidden fieldset and for these elements animation of the fadeOut fires immediately, because of it's already hidden.

Try to change to this:

$(function() {
    var ownershipType = $('#ownershipType').first();
    ownershipType.change(function() {
        $('fieldset:visible').fadeOut(2000, function() {
            $('fieldset[data-ownership-type=' + ownershipType.val() + ']').fadeIn('fast');
        });
    });
});

Code: http://jsfiddle.net/DtaHQ/20/

旧人哭 2024-12-14 19:18:22

将代码更改为

$(function() {
    var ownershipType = $('#ownershipType').first();
    var fieldsets = $('fieldset');
    ownershipType.change(function() {
        $('fieldset:visible').fadeOut(2000, function() {
            $('fieldset[data-ownership-type=' + ownershipType.val() + ']').fadeIn('fast');
        });
    });
});

您只想淡出当前可见的字段集。

http://jsfiddle.net/DtaHQ/24/

Change your code to

$(function() {
    var ownershipType = $('#ownershipType').first();
    var fieldsets = $('fieldset');
    ownershipType.change(function() {
        $('fieldset:visible').fadeOut(2000, function() {
            $('fieldset[data-ownership-type=' + ownershipType.val() + ']').fadeIn('fast');
        });
    });
});

You only want to fade out the fieldset that is currently visible.

http://jsfiddle.net/DtaHQ/24/

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