Smooth Scrolling for Inline Links
With only 8 lines of code, you can make any page scroll smoothly to inline links.
midori.addEventListener(window, 'ready', function (e) {
midori.get('a').apply(function (o) {
if (o.href.indexOf('#') != -1)
midori.addEventListener(o, 'click', function (e) {
midoriFX.scrollTo(o.href.substr(o.href.indexOf('#') + 1));
} );
} );
} );
And here is how it works:
- Add a ready event listener. So, as soon as the page loads, we can start running our code without waiting for the images to load.
- Get all the links in the document by using midori.get().
- Check for the inline link character (#), and add a click event to links that contain it.
- When the event is triggered, simply scroll to the link location by using midori.scrollTo().
You can see a live demonstration of this effect on any docs page such as midori. Just click on the method names on the left.
