Jim Vercoelen
Jim Vercoelen

Reputation: 1078

Javascript smoothscrolling still lagging

I'm working on this page: http://i333180.iris.fhict.nl/p2_vc/

After some help from some of you here, I have successfully added a smooth scroll plugin.

However, I am noticed a lot of scroll lag when the page is first opened.

What I've Tried

While all of this has helped somewhat, there is still a very noticeable amount of scroll lag.

How can I get rid of the scroll lag?

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

Update Tried this script instead of the other scripts

        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;

But still lagging

Update 2 Added

var loading = $('html');
loading.addClass('loading');
$(window).load(function(){
     loading.removeClass('loading');
});

Code works, lag is still noticeable

Upvotes: 5

Views: 1405

Answers (1)

user4639281
user4639281

Reputation:

You need to hide the page while it is loading so the user doesn't start scrolling until everything has loaded.

  • Add a class to the the html tag which hides body tag and adds a loading image to the background of the html tag
  • Wait for everything to load
  • Remove the previously added class, restoring page to its natural state.

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">

Upvotes: 1

Related Questions