简体   繁体   English

滚动动画在 IE 和 Mozilla 上不起作用

[英]Scroll animation doesn't work on IE and Mozilla

I have an element that fills the screen, under that is another element but this one is hidden so you can't scroll to it manually.我有一个填满屏幕的元素,在它下面是另一个元素,但这个元素是隐藏的,所以你不能手动滚动到它。

My CSS stylesheet looks like this for that:为此,我的 CSS 样式表如下所示:

body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
}
#page1, #content {
    height: 100%;
    width: 100%;
    position: absolute;
}
#content {
    top: 100%;
    display:none;
}

#page1 stands for the element that fills the screen and #content stands for the element which is underneath that. #page1代表填充屏幕和元件#content表示其是下面的元件。

When I click on a button on the first element (which fills the screen), it shows the element under that and smoothly scrolls down to that.当我单击第一个元素(填充屏幕)上的按钮时,它会显示该元素下的元素并平滑向下滚动到该元素。

I was using this piece of code in the first place:我首先使用了这段代码:

$(document).ready(function() {
    $('#exploreBtn').on('click', function() {
        $('#content').fadeIn(500);
        console.log($("#content").offset().top)
        $('html, body').animate({
            scrollTop: $("#content").offset().top
        }, 1000, function(){
            $("#page1").css('display','none');
            $('#content').css('top',0);
            $(document).scrollTop(0);
        });
    });
});

Which works in IE and Mozilla, but I've tried to improve it...它在 IE 和 Mozilla 中有效,但我试图改进它......

Now I'm using this code:现在我正在使用此代码:

$(function() {
    var headerLoaded = true,
            contentLoaded = false,
            header = "#fitScreen",
            exploreBtn = "#exploreBtn",
            scrollBackBtn = "#scrollBackBtn",
            content = "#content";



    $(exploreBtn).on('click', function() {
        if (!contentLoaded && headerLoaded) {
            showScrollHide(content, header, function() {zit
                var sum = ($('nav').height()) + parseInt($('nav').css('margin-bottom'));
                $('#panelContent').css('margin-top', sum);
                
               
                $('#content').css('top', 0);
                $('html, body').css('overflow-y', 'auto');
                
                $(window).scrollTop(0);

                headerLoaded = false;
                contentLoaded = true;
            });
        }
    });

    var showScrollHide = function(element, hide, func) {
        $(element).fadeIn(500, function() {
            $('body').animate({
                scrollTop: $(element).offset().top
            }, 1000, function() {
                $(hide).fadeOut(100, func);
            });
        });
    };

});

And for some reason, it doesn't work on IE and Mozilla.出于某种原因,它不适用于 IE 和 Mozilla。

It just gives me a slight delay, and then it fades in the screen I'm scrolling to.它只是给了我一点延迟,然后它会在我滚动到的屏幕中消失。

Can anyone help me with this?谁能帮我这个?

In your new code, you have this part :在你的新代码中,你有这部分:

$(element).fadeIn(500, function() {
    $('body').animate({
        scrollTop: $(element).offset().top
    }, 1000, function() {
        $(hide).fadeOut(100, func);
    });
});

The difference between your working code and your not working code is which element you animate the scroll.您的工作代码和不工作代码之间的区别在于您为滚动设置动画的元素。

In you working code, you are animating 'body, html' .在您的工作代码中,您正在为'body, html'设置动画。 Depending on browser, the scrollbar is not on the same element.根据浏览器的不同,滚动条不在同一个元素上。 Hence, that's why you should target both html and body element :因此,这就是为什么你应该同时定位 html 和 body 元素:

$(element).fadeIn(500, function() {
    $('html, body').animate({ //HERE!
        scrollTop: $(element).offset().top
    }, 1000, function() {
        $(hide).fadeOut(100, func);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM