Scrolling animation effects in GenerateBlocks Pro
The new GenerateBlocks Pro plugin is now available for purchase and it’s awesome! One of the new features added is the ability to add your own custom attributes to a GenerateBlocks container. Amongst other things, this gives us the ability to integrate various JavaScript libraries that make use of those custom attributes. One of those libraries is a very lightweight scrolling library called Sal.js.
Sal.js (Scroll Animation Library) is a performance-focused, lightweight (less than 2.8 kb) scroll animation library, written in vanilla JavaScript with no dependencies! It was created to provide a performant and lightweight solution for animating elements on scroll. It’s based on the Intersection Observer, which gives an amazing performance in terms of checking the element’s presence in the viewport.
To download the library, head over to the GitHub repository, select the green Code
button and from the dropdown that appears select Download ZIP. Once the package is downloaded, create a child theme and add an extra folder inside it called sal (or any name you want). Now, extract the zip file you downloaded into a location on your computer, copy sal.css and sal.js files located inside sal-master->dist folder and paste those files inside the sal folder you created in your child theme directory. Finally, create another js file named sal-init.js (or any name you want). We are going to use that file as our initialization script.
In my case, I am going to use the library for the Create Advanced homepage of my agency website. The following code added to functions.php will enqueue the files only for the homepage.
function gm_enqueue_sal_scripts() { if( is_front_page() ) { wp_enqueue_style( 'sal', get_stylesheet_directory_uri() . '/sal/sal.css' ); wp_enqueue_script( 'sal', get_stylesheet_directory_uri() . '/sal/sal.js', array(), false, true ); wp_enqueue_script( 'sal-init', get_stylesheet_directory_uri() . '/sal/sal-init.js', null, null, true ); } } add_action( 'wp_enqueue_scripts', 'gm_enqueue_sal_scripts' );
To include the scripts for the whole website just remove the is_front_page()
conditional.
function gm_enqueue_sal_scripts() { wp_enqueue_style( 'sal', get_stylesheet_directory_uri() . '/sal/sal.css' ); wp_enqueue_script( 'sal', get_stylesheet_directory_uri() . '/sal/sal.js', array(), false, true ); wp_enqueue_script( 'sal-init', get_stylesheet_directory_uri() . '/sal/sal-init.js', null, null, true ); } add_action( 'wp_enqueue_scripts', 'gm_enqueue_sal_scripts' );
Finally, I entered the following into the sal-init.js to initialize the script and also disable the scrolling animation effect for mobile devices.
const animation = sal(); const switchAnimations = () => { if (window.innerWidth < 768) { animation.reset(); animation.disable(); } else { animation.reset(); animation.enable(); } }; switchAnimations(); window.addEventListener('resize', switchAnimations);
For more information on initialization options, check the Sal.js library documentation.
Now you are ready to start adding your custom attributes to the containers that you want to animate. Attributes are added by selecting a container and scrolling to the Advanced section on the right. Expand it and start adding your attribute-value
pairs. For the first image and text below the fold, I added the following attributes to the containers:
Left Container
Attribute | Value |
---|---|
data-sal | slide-right |
data-sal-easing | easeInOutSine |
data-sal-duration | 600 |
Right Container
Attribute | Value |
---|---|
data-sal | slide-left |
data-sal-easing | easeInOutSine |
data-sal-duration | 600 |
data-sal-delay | 100 |
Note the slight delay added at the data-sal-delay
property to make the right container scroll in with a slight delay. I used exactly the same settings for the following groups of images and text.
For the icon boxes at the bottom, I used the following attribute-value
pairs:
First Container
Attribute | Value |
---|---|
data-sal | slide-up |
data-sal-easing | easeInOutSine |
data-sal-duration | 600 |
Second Container
Attribute | Value |
---|---|
data-sal | slide-up |
data-sal-easing | easeInOutSine |
data-sal-duration | 600 |
data-sal-delay | 100 |
Third Container
Attribute | Value |
---|---|
data-sal | slide-up |
data-sal-easing | easeInOutSine |
data-sal-duration | 600 |
data-sal-delay | 200 |
Again, note the slight delay between the values that contribute to the elements not scrolling up exactly the same time.
Finally, for the bottom Call to Action section, I used the following attribute-value
pairs.
Attribute | Value |
---|---|
data-sal | fade |
data-sal-easing | easeInOutSine |
data-sal-duration | 1000 |
If you don’t want any changes to the animation behavior on mobile devices smaller than 768px
, you don’t need the switchAnimations
function at all. You can simply initialize the animation once without any condition or function calls. Here’s how:
const animation = sal();
Very good !!!
Great!
This was really useful.
Thanks!
No problem!
Since GenerateBlocks Pro now supports Custom Attributes, we wouldn’t need to apply the attributes via functions.php, correct?
Hey, I am not applying the attributes through functions.php, I am adding them to the containers I want to animate already using GenerateBlocks Pro, of course. Like you are suggesting…
Hi!
I love GP and this article is very useful for me.
One question:
Why don’t you use It in mobile devices?
Thanks in advance
Hi there, glad you found the article useful. I find it a bit distracting for the mobile user experience but that’s just me!
Thanks a lot for this tutorial!
I was able to add some animations to my page, but how do you fix the “DevTools failed to load SourceMap: Could not load content” error when adding a JavaScript library?
Hi there, glad the tutorial was useful. My dev console is clean from my end. Could it be that a browser add-on is causing this issue? What browser are you using? Can you check if the problem exists in an Incognito/private browser window with no addons loaded?
WOW!!
This is beautiful!!
This is fantastic – just what I was looking for. Thanks so much!
Hello! I followed this exactly but the animations are not working on my end. I added the Sal folder and files into my Child theme as detailed using FileZilla. I have Generate Blocks Pro set up, added the custom attributes to the containers, and still no luck. Perhaps I am supposed to be applying the custom attributes to the blocks themselves? Any help would be greatly appreciated. Thanks!
Not sure what could be wrong. Do you get any errors in the browser console?
great library, lightweight and enough functionality, not overdone.
Thanks!!
No problem, yeah, really good library!
hi
Do I have to have ‘generateblocks pro’ to use this, can’t I use it on a site with only ‘generatepress pro’ installed?
Hi Lucas, you need both GB free and GB Pro for this to work.
Nice, absolutely will try this.
Can I just use GP pro elements to add the .js & .css lib (copy the whole script & style), and use code snippets to initiate the .js?
If yes, which one (vs the child theme method) could be better in term of performance & manageability
GP elements can only be used to add JS but the JS that usually comes with those libraries is minified and you could get confused. Plus, it would be easier to just replace the JS file in a future library update. CSS would need to be copied to the Customizer but again, it would complicate things. I would keep things simple and use my example. It’s more manageable this way!
Fantastic! Love only using the js I need and keeping the file size low as possible. Thanks George
Awesome!
This is really NICE!
But if i would like to use it on mobile too, what lines i would need to change from your sal-init.js?
Thank you
If you don’t want any changes to the animation behavior on mobile devices smaller than 768px, you don’t need the switchAnimations function at all. You can simply initialize the animation once without any condition or function calls. Here’s how:
const animation = sal();
I have also updated the article accordingly.
Thank YOU
Glad you found it useful!
Hi George, great article…. Given that Sal.js hasn’t been updated in quite a while, would you stil l confidently use it on new projects or perhaps use a different library? Thanks!
You should be fine but there are also a couple of plugins you could try:
Animate on Scroll
Animations for Blocks
Thanks George…
Isn’t this something that can be achieved using the GBP Effects panel?
Currently, this kind of scrolling effect cannot be achieved with the GB Pro effects.
Hi,
I followed the instructions and it works fine, I just don’t understand on how NOT to apply the animation for the mobile? I want to apply it only on desktop only, how can I do that?
I mention this in the tutorial, the init code should be this:
const animation = sal();
const switchAnimations = () => {
if (window.innerWidth < 768) { animation.reset(); animation.disable(); } else { animation.reset(); animation.enable(); } }; switchAnimations(); window.addEventListener('resize', switchAnimations);
Was not able to make this work. I am using the new GB & GBPro 2.0 alpha release, and I believe this should still work fine here. I am using the GB Grid block with two columns. I followed your instructions for the left and right containers. I have all the attributes in place for each container. I have your enqueue script in functions.php. Was there a CSS class I’m supposed to use? Does the grid block need to be set to full width to give the containers room to slide?
Hi Steve. I am not sure how set up looks like. If you create a staging site so I could have a look, I might be able to help. You can contact me with details from the form in the “Contact” page.