【问题标题】:Delay after animating "scroll" to an element near the bottom of the page动画“滚动”到页面底部附近的元素后延迟
【发布时间】:2015-04-27 03:53:32
【问题描述】:

我有一个网页,我在折叠上方的圆圈中显示一个向下的箭头,表示下面有更多内容。单击此箭头时,我有 jQuery 来执行动画滚动到页面底部,然后 jQuery 向上滚动到所需的位置。当动画到达页面底部时,我想让 jQuery 立即开始向上滚动到所需的位置,而在页面底部没有短暂的暂停。我尝试在每个动画结束时使用和不使用函数选项使用 jQuery,当第一个动画到达页面底部时,我仍然注意到暂停。

示例页面位于: Click Here

我的示例代码是:

<script type="text/javascript">
$(document).ready(function (){
         $("#arrow-circle").click(function (){
                //$(this).animate(function(){

                $('html, body').animate({
                scrollTop: $("#footer-seamless-name").offset().top
                }, 2500);

                $('html, body').animate({
                scrollTop: $("#arrow-circle").offset().top
                }, 1000);

                $('html, body').animate({
                scrollTop: getFoldHeight()
                }, 1000);
                //});
            });
       });
</script>

<script type="text/javascript">
 $(document).ready(function (){
            $("#arrow-circle").click(function (){
                //$(this).animate(function(){

                $('html, body').animate({
                scrollTop: $("#footer-seamless-name").offset().top
                }, 2500, function() {

                    $('html, body').animate({
                    scrollTop: $("#arrow-circle").offset().top
                    }, 1000, function () {

                    $('html, body').animate({
                    scrollTop: getFoldHeight()
                    }, 1000);
               //});
            });
        });
            });
 });
</script>

【问题讨论】:

  • 可以在 Question 中包含html,getFoldHeight() 吗?

标签: jquery scroll jquery-animate


【解决方案1】:

我认为您需要考虑窗口的高度,因为它会滚动到元素的顶部。如果您的元素是粘在底部的页脚,请从滚动位置减去窗口的高度

试试这个:

$('html, body').animate({ scrollTop: $("body").height() - $(window).height() }, 2500,function() {
    $(this).animate({ scrollTop: $("#arrow-circle").offset().top }, 1000)
})

http://jsfiddle.net/mag59hw7/11/

滚动到文档末尾后,它会尝试滚动到您的页脚,该页脚的位置低于您在页面中可以滚动的最大量。这基本上是造成延迟

【讨论】:

  • 出于学习的目的,为什么您的方法在没有意外停顿的情况下发生?
  • 当您滚动到特定位置时,您的函数会滚动到元素的顶部(在您的情况下是页脚)。您不能滚动到页脚的顶部,因为它低于您可以在页面中执行的最大滚动。这就是您需要滚动到bottom of your document-window-height 的原因。如果您的页脚高度大约是您的窗口高度,它会起作用
  • 调整您的浏览器窗口大小并使其等于您的页脚高度,您的代码将起作用
  • 我的解释有帮助吗? @DaveD
  • 这是正确的理解吗?我最初的方法是尝试使用等于页面顶部和我引用的页脚元素之间的空间的量向下滚动。由于开始此滚动过程的要单击的元素位于初始折叠的正上方,因此没有足够的垂直高度来滚动此量,因此在要滚动的量耗尽之前滚动到达页面的末尾,所以我注意到暂停,因为屏幕无法再向下滚动,但代码想要再滚动一些。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 2013-05-06
  • 1970-01-01
相关资源
最近更新 更多