Skrollr is a scroller library that allows you to perform animations or changes to your web page’s content based on screen position. For example, you can make a div slowly appear, or slide in, as you scroll down the page. You can see it in action here.
This tutorial will cover the bare minimum you need to get Skrollr working. There are plenty of tutorials out there that show you how to do more advanced things, but this one will help you get your first Skrollr powered page.
Step 1
Download Skrollr from the project’s Github (click the “Download ZIP” link on the right).
Unzip the file, and inside the “dist” folder, you’ll find the “skrollr.min.js” file. Copy that file into your own project. In my case, I’ll put the file inside a “js” folder in my project.
Step 2
Add the script to the bottom of your HTML page, right before </body>
:
<script src="js/skrollr.min.js"></script>
<script>
var s = skrollr.init();
</script>
Just make sure that the file is loading (the path you indicated in src points to the file).
Step 3
We can now animate things as people scroll down or up the page. There are all sorts of things you can do, but as a simple example, I’ll make something appear in the page as people scroll down by animating its opacity.
NOTE: This example will only work when the div I’m animating scrolls into view, so if you try this out, make sure that you have enough content above it to push it below the fold when you first load the page.
Here’s the HTML:
<!-- Above there must be enough content to push this below the fold -->
<div class="row featurette" id="third"
data-center-top="opacity:0;"
data-top-top="opacity:1;"
>
<div class="col-md-7 text">
<h2 class="featurette-heading">And lastly, this one. <span class="text-muted">Checkmate.</span></h2>
<p class="lead">Donec ullamcorper nulla non metus auctor fringilla. Vestibulum id ligula porta felis euismod semper. Praesent commodo cursus magna, vel scelerisque nisl consectetur. Fusce dapibus, tellus ac cursus commodo.</p>
</div>
<div class="col-md-5 image">
<img class="featurette-image img-responsive" src="http://placekitten.com/g/600/600" alt="Generic placeholder image">
</div>
</div>
It’s all regular HTML (what’s inside “featurette” div doesn’t matter), except for lines 2 and 3, where I added the Skroller attributes:
data-center-top
: The animation will start when the top of the div reaches the center of the screendata-top-top
: The animation will end when the top of the div reaches the top of the screen
In this example I used the top as the part of the div that triggers the beginning and end of the animation at different points of the screen. You can also use bottom, or center. So, for example, you can set your animation to end (or begin) when the center of the div reaches the center of the screen: data-center-center
.
Since this is a tutorial for beginners, I won’t go into detail as to more precise ways of picking the beginning and end of the changes. If you are interested, read the documentation about absolute an relative modes.
I only animated the “opacity” CSS property, but you can animate most other CSS properties. For example, assuming that “featurette” has been set to position:relative
in your regular CSS file, you could then animate it so that it moves in from the side by animating its position (left or right).
<div class="row featurette" id="third"
data-center-top="opacity:0; left:-200%;"
data-top-top="opacity:1; left:0;"
>
As you can see, you can animate multiple properties at the same time.
This tutorial is very basic, and just so you can get your fee wet, but you should look at the documentation, and the example that comes with the download. As well, there are many intermediate and advanced tutorials on how to use Skrollr.
Happy scrolling!
This really hit the spot. Thanks!
Thanks! Really helped me!
I want to know how can i add this Plug-in in my wordpress theme,
can you help me for that
Thank you
The tutorial would be the same, the only difference would be that you would have to use
wp_enqueue_script
to load the script. Here’s a tutorial on how to add a script to WordPress.I see you use “col-md-7” and “col-md-5″, my question; did you do your file set up with bootstrap? Because i did, and it’s not responsive. I use the ” col-**-** ” classes and it makes more of a mess than anything.
Yes, I did. It shouldn’t affect your code. Is your code valid? Make sure you are not missing any closing divs.
Thank you for this tutorial! Really helped!
Let me know if you have more! Thanks!