Smooth Scrolling with Just 1.1KB

Copy Link
Copied!

If you are already using a JavaScript library for your animations (e.g. Velocity.js) on your project, chances are it has a function that allows you to smooth scroll within the page and possibly even within scrollable containers. So you could just use it to add smooth scrolling.

But there are cases where you find yourself working on a project that doesn’t require much JavaScript to justify using Velocity.js (34KB) (or even worse jQuery (84KB)) just to add smooth scrolling. The thing is you don’t have to because there is a very tiny JavaScript module called Zenscroll.

Zenscroll

I came across Zenscroll recently and it quickly became my go-to scrolling module. What’s not to love:

I can already hear you going “holy crap that’s awesome”, but wait there’s more!

The most convenient feature Zenscroll provides is automatic smooth-scolling on links within the same page (can be disabled). So if you literally only want to add smooth scrolling to same page links, then you literally only need to include Zenscroll in your HTML and you’re done. There’s no need to write any JavaScript yourself unless you want to change the default parameters.

Other useful features

Visit Zenscroll’s Demo Page to learn how to use it

However, there’s one small bug I encountered whilst playing around with it.

The bug discussed below is from an old version of Zenscroll

Problem with Disabling Automatic Smoothing on Local Links

By default Zenscroll takes care of smooth scrolling on local links automatically and that’s reasonable. And you can easily change the default parameters for the automatic smoothing (and all other scrolling):

var defaultDuration = 777 // ms
var edgeOffset = 42 // px
zenscroll.setup(defaultDuration, edgeOffset)

You can disable automatic scrolling all together (why would you?) or on specific links (possible scenario: adding a callback function on scroll completion).

Unfortunately, I found in some browsers when I disable automatic scrolling and add my own Zenscroll behaviour to a click event on a local link, the page jumps to the expected target (no scrolling) then jumps back to the link that was clicked before executing the desired custom Zenscroll behaviour. The problem doesn’t happen every single time, but it was certainly repeatable. This is really the only drawback I encountered with Zenscroll.

Note that this problem doesn’t occur when you use Zenscroll programmatically on elements other than local links.

Callback Function on Local Links with Zenscroll

To add a callback function on scroll completion you normally do this:

//syntax
zenscroll.to(element, duration, onDone);

//example
zenscroll.to(element, 600, function () { console.log('scroll complete') });

But given the problem with local links outlined above, you can use a simple workaround: keep the automatic smoothing and use setTimeOut() to execute your code on scroll completion. Why? Because you know the duration of the scroll; by default it's 999ms or you can set the default value yourself with zenscroll.setup()

Example:

You have a comment/reply link. When clicked it takes you to the comment field on the same page. And on scroll completion you want to give focus to the comment field (textarea element)

Below is a simplified example and not a fully functioning solution

HTML

<!-- comment -->
<a href="#commentField" class="reply">Reply</a>
<!-- more comments -->

.
.

<form>
	<textarea id="commentField"></textarea>
</form>

.
.
<script src="js/zenscroll-min.js"></script>
</body>

Add eventListener to all 'Reply' links

//assuming you have more than one reply link
var replies = document.getElementsByClassName('reply');

for(var i=0; i<replies.length; i++)
{
	replies[i].addEventListener("click", prepareReply);
}

Add focus to comment field on scroll completion

function prepareReply(e)
{
	var commentField = document.getElementsById('commentField');
	setTimeout(function () { 
		commentField.focus();
	}, 999);	
}

Last Words

I’m so thankful that Gabor Lenard has released Zenscroll into the public domain. Despite the small bug with local links, it is immensely powerful for a 1.1KB module.

Now I’m just waiting for the day I enter a stackoverflow question on smooth scrolling and read “just use Zenscroll” among the answers. Because it is indeed the “One JavaScript to Smooth-Scroll Them All”.

Get in Touch