0

Mac users knows that there is a very soft page scrolling with touchpad. But when you scroll page with a mouse wheel or even with browser scrollbar - page scrolls not soft, but with some breaks. It happens because of scrollTop difference because it renders not every pixel change.

Is there a way to make cross-browser soft page scrolling like touchpad? I was trying to use this code:

$window = $(window);

if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;

function wheel(event) {
  var delta = 0;
  if (event.wheelDelta) delta = event.wheelDelta / 90;
  else if (event.detail) delta = -event.detail / 3;

  handle(delta);
  if (event.preventDefault) event.preventDefault();
  event.returnValue = false;
}

var goUp = true;
var end = null;
var interval = null;

function handle(delta) {
  var animationInterval = 20; //lower is faster
  var scrollSpeed = 20; //lower is faster

  if (end == null) {
    end = $window.scrollTop();
  }
  end -= 20 * delta;
  goUp = delta > 0;

  if (interval == null) {
    interval = setInterval(function () {
      var scrollTop = $window.scrollTop();
      var step = Math.round((end - scrollTop) / scrollSpeed);
      if (scrollTop <= 0 || scrollTop >= $window.prop("scrollHeight") - $window.height() || goUp && step > -1 || !goUp && step < 1 ) {
        clearInterval(interval);
        interval = null;
        end = null;
      }
      $window.scrollTop(scrollTop + step );
    }, animationInterval);
  }
}
p:nth-child(10n) {background-color: #d00}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <body>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </body>
</html>

And this code works perfectly in Google Chrome and Opera. But Mozilla and Safari don't show similar soft scroll. They are scrolls content too slowly.

Is there another way to make soft page scroll?

Oleg Frolov
  • 89
  • 11
  • I have tried implementing smooth scrolling cross-browser and cross-platform before, and it's always far from perfect. My advice would be to leave it with native scrolling behaviour, however imperfect it is.You'll find that you'll make it work great in one browser or platform, then discover it's worse-than-native on others. Ever visited a site on a smooth-scrolling Mac that has a smooth scrolling script enabled? It's horrible, and there's no way to test in JavaScript whether it's a smooth Apple pointer device or a jolty Windows pointer device that's being used (well there is, but it's nasty) – rorymorris89 Jan 19 '17 at 11:34
  • Yep, I understand that. But on my page there is a lot of parallax elements and they are based on scrolls. So when I'm trying to use native scroll event, I cannot catch it to be smooth animated as I need. – Oleg Frolov Jan 19 '17 at 11:59
  • I want a very soft scrolldown animation like with a touchpad – Oleg Frolov Jan 19 '17 at 12:04
  • That was the exact reason why I tried to achieve smooth scrolling too. Best of luck! – rorymorris89 Jan 19 '17 at 12:05

2 Answers2

1

You can use https://github.com/jquery/jquery-mousewheel to help making your smooth scroll because Firefox needs a different event for mousewheel.

This plugin help you implementing scrolling effects in all browsers. Normally, I use TweenMax for my smooth scrolling.

Luis Serrano
  • 131
  • 1
  • 4
  • 10
  • You can use scrollTo prop from TweenMax where you set the distance you want to scroll. You can calc the distance of your scroll and add to TweenMax, setting an effect for this. Check their website https://greensock.com/gsap – Luis Serrano Jan 19 '17 at 15:19
0

I hope Labu doesn't mind me posting their answer here but you can you not use a light weight plugin instead?

Here is the answer from a similar post.

Simplr-SmoothScroll and Demo Here // Initial source here

Community
  • 1
  • 1