fadeOut() 一张表,fadeIn() 另一张表

发布于 2024-11-19 05:29:46 字数 1668 浏览 8 评论 0原文

可能是一个非常基本的问题 -

我有 2 个表 #favorites#leaders,每个表的底行都有一个按钮。

当我单击按钮时,我只想显示其中一个表。

所以我正在尝试以下方法,它有点有效:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
        $('#favorites').hide();

        $('#show_favorites').click(function() {
                $('#leaders').fadeOut();
                $('#favorites').fadeIn();
        });

        $('#show_leaders').click(function() {
                $('#favorites').fadeOut();
                $('#leaders').fadeIn();
        });
});
</script>

但它同时发生,这看起来很尴尬。

在开始 fadeIn() 之前,如何等待 fadeOut() 完成?

更新:

我已将代码更改为

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
        $('#favorites').hide();

        $('#show_favorites').click(function() {
                $('#leaders').fadeOut("slow", function() {
                        $('#favorites').fadeIn();
                });
        });

        $('#show_leaders').click(function() {
                $('#favorites').fadeOut("slow", function() {
                        $('#leaders').fadeIn();
                });
        });
});
</script>

现在它工作得更好,但是有一个新问题,当单击按钮时:

当一个表格(下面的屏幕截图中的灰色)消失时,滚动条跳起来。然后出现另一个表格,但它不再可见 - 您必须手动向下滚动。

在此处输入图像描述

有什么想法可以解决这个问题吗?

Probably a very basic question -

I have 2 tables #favorites and #leaders, each with a button in the bottom row.

And I want to display only one of tables, when I click a button.

So I'm trying the following and it kind of works:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
        $('#favorites').hide();

        $('#show_favorites').click(function() {
                $('#leaders').fadeOut();
                $('#favorites').fadeIn();
        });

        $('#show_leaders').click(function() {
                $('#favorites').fadeOut();
                $('#leaders').fadeIn();
        });
});
</script>

but it happens at the same time, which looks awkward.

How do you wait for the fadeOut() to finish, before starting fadeIn()?

UPDATE:

I've change the code to

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
        $('#favorites').hide();

        $('#show_favorites').click(function() {
                $('#leaders').fadeOut("slow", function() {
                        $('#favorites').fadeIn();
                });
        });

        $('#show_leaders').click(function() {
                $('#favorites').fadeOut("slow", function() {
                        $('#leaders').fadeIn();
                });
        });
});
</script>

And now it works better, but there is a new problem, when a button is clicked:

when the one table (grey in the screenshot below) disappears, the scrollbar jumps up. And then another table appears, but it is not visible anymore - you have to scroll down manually.

enter image description here

Any ideas please how to fight this?

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

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

发布评论

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

评论(3

谈场末日恋爱 2024-11-26 05:29:46

您可以向fadeOut提供回调函数,并在回调中调用fadeIn。当fadeOut完成时执行回调函数:

$('#leaders').fadeOut(function() {
    $('#favorites').fadeIn();
});

请参阅jQuery API了解更多信息。

更新(基于更新的问题)

滚动问题的潜在解决方案:

$('#leaders').fadeOut(function() {
    $('#favorites').fadeIn(function() {
        window.scrollTo(0, $(this).offset().top);
    });
});

这将导致文档自动滚动到刚刚淡入的元素的顶部。

You can supply a callback function to fadeOut, and call fadeIn in the callback. The callback function is executed when the fadeOut is complete:

$('#leaders').fadeOut(function() {
    $('#favorites').fadeIn();
});

See the jQuery API for more info.

Update (based on updated question)

A potential solution to your scrolling problem:

$('#leaders').fadeOut(function() {
    $('#favorites').fadeIn(function() {
        window.scrollTo(0, $(this).offset().top);
    });
});

This will cause the document to scroll automatically to the top of the element that's just faded in.

一场春暖 2024-11-26 05:29:46

您必须像这样进行回调:

$('#leaders').fadeOut(function()
{
    $('#favourites').fadeIn(); // execute after fadeOut has finished
});

其他:

$('#favourites').fadeOut(function()
{
    $('#leaders').fadeIn(); // execute after fadeOut has finished
});

You must make a callback like this:

$('#leaders').fadeOut(function()
{
    $('#favourites').fadeIn(); // execute after fadeOut has finished
});

Other:

$('#favourites').fadeOut(function()
{
    $('#leaders').fadeIn(); // execute after fadeOut has finished
});
第几種人 2024-11-26 05:29:46
$('#leaders').fadeOut("slow", function() { $('#favorites').fadeIn(); });

fadeOut 接受一个回调,当淡出完成时调用该回调。

$('#leaders').fadeOut("slow", function() { $('#favorites').fadeIn(); });

fadeOut takes a callback which is called when fadeout is done.

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