Jquery fadeIn 导致滚动顶部,我该如何解决?

发布于 2024-10-16 01:51:07 字数 1583 浏览 7 评论 0原文

我对 jQuery fadeIn(或 fadeOut)方法有疑问。 我建立了一个文章旋转器,一切正常 但是当页面滚动到底部并且文章旋转时出现问题, fadeIn(或fadeOut)方法导致滚动到文章位置。 我认为这些方法改变了body的css top属性, 但我不知道如何避免这种情况! 有什么想法吗???

这里的代码

    function rotate(direction)
{
    if($('articles > article:visible:first') == 'undefined')
        $currentArticle = $('articles > article:first');
    else
        $currentArticle = $('articles > article:visible:first');

    if($currentArticle.attr('id') == $('articles > article:last').attr('id'))
        $next = $('articles > article:first');
    else
        $next = $currentArticle.next();

    if($currentArticle.attr('id') == $('articles > article:first').attr('id'))
        $prev = $('articles > article:last');
    else
        $prev = $currentArticle.prev();

    if($do_animation)
    {
        $currentArticle.fadeOut(1000,function(){
                switch(direction)
                {
                    case 1:
                        $next.fadeIn(1000);
                        break;
                    case -1:
                        $prev.fadeIn(1000);
                        break;
                }
                if($('.rotate_show'))
                    $('.rotate_show').removeClass('rotate_show');
                $('article_number > btn[id|="'+$next.attr('id')+'"]').addClass('rotate_show');
                });
    }
    else
        return false;
}

可以在这里网站 http://kario91.altervista.org/ultimate 文本来自 joomla这是完整的网站!变量工作正常,没有问题..尝试缩小浏览器窗口并滚动底部

i have a problem with jQuery fadeIn(or fadeOut) method.
i build an article rotator , all works fine
but there is a problem when the page is scrolled bottom and the article rotate,
The fadeIn (or fadeOut) method causes a scroll to the article position.
I think that these methods ,changes the css top property of body,
but I do not know how to avoid this!
Any idea???

here the code

    function rotate(direction)
{
    if($('articles > article:visible:first') == 'undefined')
        $currentArticle = $('articles > article:first');
    else
        $currentArticle = $('articles > article:visible:first');

    if($currentArticle.attr('id') == $('articles > article:last').attr('id'))
        $next = $('articles > article:first');
    else
        $next = $currentArticle.next();

    if($currentArticle.attr('id') == $('articles > article:first').attr('id'))
        $prev = $('articles > article:last');
    else
        $prev = $currentArticle.prev();

    if($do_animation)
    {
        $currentArticle.fadeOut(1000,function(){
                switch(direction)
                {
                    case 1:
                        $next.fadeIn(1000);
                        break;
                    case -1:
                        $prev.fadeIn(1000);
                        break;
                }
                if($('.rotate_show'))
                    $('.rotate_show').removeClass('rotate_show');
                $('article_number > btn[id|="'+$next.attr('id')+'"]').addClass('rotate_show');
                });
    }
    else
        return false;
}

ok here the site http://kario91.altervista.org/ultimate the text is from joomla this is the complete site! the variables work fine, there's no problem.. try to reduce the browser window and scroll bottom

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

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

发布评论

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

评论(3

七度光 2024-10-23 01:51:07

我想说这个问题是由于当您的文章完全淡出时,就在调用回调之前,页面的高度降低了(因为文章被隐藏),因此浏览器滚动直到页面底部(没有文章)位于浏览器窗口的底部。尝试在 fadeOut 完成后删除回调,您可能会注意到浏览器如何对齐底部。

我认为你可以通过在淡出之前给文章容器一个高度来解决这个问题,当淡出完成时,在淡入开始后立即删除这个高度......或者类似的东西。

我希望这有帮助!

I feel tempted to say that this issue is due to the fact that when your article fades out completely, just before the callback is called, the height of your page is reduced (because the article is hidden) and because of that, the browser scrolls up until the bottom of the page (without the article) is right at the bottom of the browser's window. Try removing the callback after the fadeOut is completed, and you might notice how the browser aligns the bottom.

I think you could fix this by giving the article container a height before the fadeOut beings, and when the fadeOut is completed, remove this height right after the fadeIn begins... Or something like that.

I hope this helps!

櫻之舞 2024-10-23 01:51:07

我用 fadeTo() 解决了这个问题,它是这样的

备份你的位置

$("#position").attr("name","scroll"+$("body").scrollTop());

FadeOut:

$("#content").fadeOut(300,function(){........});

要返回,执行“半”fadeIn 并回调位置:

$("#content").fadeTo(10,0.1,function(){

$("body").scrollTop($("#position").attr("name").replace("scroll",""));


});

然后它们完全淡入

$("#content").fadeTo(300,1);

I solved it with fadeTo(), it is something like this

Backup your position

$("#position").attr("name","scroll"+$("body").scrollTop());

FadeOut:

$("#content").fadeOut(300,function(){........});

To go back do a "semi" fadeIn and callback the position with:

$("#content").fadeTo(10,0.1,function(){

$("body").scrollTop($("#position").attr("name").replace("scroll",""));


});

and them fade completly in

$("#content").fadeTo(300,1);
你的往事 2024-10-23 01:51:07

设置父容器的高度解决了这个问题。

Setting height to parent container solved the problem.

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