我正在此页面上工作:http://i333180.iris.fhict.nl/p2_vc/

在您的帮助下,我已经成功添加了平滑滚动插件。

但是,第一次打开页面时,我注意到很多滚动滞后。

我尝试过的内容

  • 我将背景图像从10 mb压缩到2.2 mb(1280 x 1024)。
  • 我将背景图像移动到div元素,因此它仅在内容上而不在视频后面。
    <div id="div_section_img">
    <!-- all content -->
    </div>
    


    #div_section_img{
        background: url("nature.png") no-repeat center center fixed;
    }
    
  • 这样,视频和内容部分之间出现了一条奇怪的“空白”行。我认为这是因为视频高度设置为100vh

  • 我设法通过将margin-toppadding-top元素上的#logo设置更改为footer来解决此问题。
    #logo {
        width: 410px;
        **padding-top: 120px;**
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 0px;
    }
    
    footer {
        **padding-top: 100px;**
        width: 100%;
        height: 300px;
        background-color: rgba(25, 25, 25, 0.8);
    }
    

    尽管所有这些都有所帮助,但滚动延迟仍然非常明显。

    如何摆脱滚动延迟?

    smooth-scroll.js
    $(function() {
      $('a[href*=#]:not([href=#])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
          var target = $(this.hash);
          target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
          if (target.length) {
            $('html,body').animate({
              scrollTop: target.offset().top
            }, 1000);
            return false;
          }
        }
      });
    });
    

    From this css-tricks tutorial
  • 即使滚动顶部5000也落后于

  • 更新
    尝试过此脚本而不是其他脚本
            function start(){
            $(function() {
              $('a[href*=#]:not([href=#])').click(function() {
                if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
    
                  var target = $(this.hash);
                  target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
                  if (target.length) {
                    $('html,body').animate({
                      scrollTop: target.offset().top
                    }, 1000);
                    return false;
                  }
                }
              });
            });
            function videoEnded() {
                $('html,body').animate({
                    scrollTop: $("#section").offset().top
                }, 1000);
            }
        }
        window.onload = start;
    

    但仍然落后

    更新2
    添加
    var loading = $('html');
    loading.addClass('loading');
    $(window).load(function(){
         loading.removeClass('loading');
    });
    

    代码有效,延迟仍然很明显

    最佳答案

    您需要在页面加载时隐藏页面,以便用户在所有内容加载完毕后才开始滚动。

  • html标签添加一个类,该标签隐藏body标签,并将加载图像添加到html标签的背景
  • 等待所有内容加载
  • 删除先前添加的类,将页面恢复为其自然状态。


  • var loading = $('html');
    loading.addClass('loading');
    $(window).load(function(){
        loading.removeClass('loading');
    });
    html.loading {
        height: 100%;
        background-image: url('http://i.imgur.com/HEAhF9v.gif'); /* Animated loading spinner image */
        background-position: center center;
        background-repeat: no-repeat
    }
    html.loading body {
        visibility: hidden;
        height: 100%;
        overflow: hidden;
    }
    <!-- For demo purposes only -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <img src="//lorempixel.com/1024/768">

    07-28 02:24
    查看更多