On this tutorial I will show you how to create a smooth scrolling for your website. We’ll be using TweenMax and the ScrollToPlugin.

VIEW DEMO   DOWNLOAD FILES

 

The idea behind the script is very simple. First, we will catch the mouse scrolling event, then we will animate the window depending on the event data. We will be using this snippet to catch the mouse wheel event and TweenMax and ScrollToPlugin for the animation. The ScrollToPlugin makes it possible to animate the scroll position on every DOM element, or the window in vertical or/and horizontal direction.

First, we have to include jQuery, TweenMax and ScrollToPlugin. We’re doing this via CDN:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/ScrollToPlugin.min.js"></script>

Let’s set up some variables:

var $window = $(window);
var scrollTime = 1.2;
var scrollDistance = 170;

The first variable is the window object. We need it both to listen it for scroll event, and to animate it. The scrollTime variable will be used to define the time of the animation and the scrollDistance to calculate the amount of scroll.

Next, we have to listen for mouse scroll event:

$window.on("mousewheel DOMMouseScroll", function(event){
	event.preventDefault();	
	var delta = event.originalEvent.wheelDelta/120 || -event.originalEvent.detail/3;
});

We use event.preventDefault() to prevent default scrolling. What we need is the event delta – it will tell us how much the user scrolled. Once we know this, we’re able to calculate the new window position:

var scrollTop = $window.scrollTop();
var finalScroll = scrollTop - parseInt(delta*scrollDistance);

Let’s animate the window to this new position:

TweenMax.to($window, scrollTime, {
	scrollTo : { y: finalScroll, autoKill:true },
	ease: Power1.easeOut,
	overwrite: 5							
});

That’s all! Here is the final code:

$(function(){	

        var $window = $(window);
	var scrollTime = 1.2;
	var scrollDistance = 170;

	$window.on("mousewheel DOMMouseScroll", function(event){

		event.preventDefault();	

		var delta = event.originalEvent.wheelDelta/120 || -event.originalEvent.detail/3;
		var scrollTop = $window.scrollTop();
		var finalScroll = scrollTop - parseInt(delta*scrollDistance);

		TweenMax.to($window, scrollTime, {
			scrollTo : { y: finalScroll, autoKill:true },
				ease: Power1.easeOut,
				overwrite: 5							
			});

	});
});

If you want to make the scroll faster, decrease scrollTime variable. If you want to scroll to a longer distance, increase scrollDistance variable.
Advanced jQuery users may think of throttling the scroll event when implementing this technique. If you need advanced smooth scroll plugin, I would recommend Nicescroll and perfect-scrollbar.

 

Demo credits:

Purty Wood background pattern by  Richard Tabor.
*Note: subtlepatterns are now toptal

Mr Dafoe font by Sudtipos.

Hipsteripsum