Animatorsmooth Animations On Scroll

Posted on  by 



  1. Animatorsmooth Animations On Scroll Shapes
  2. Animatorsmooth Animations On Scroll Cursor

There is a kind of animations that has not stopped increasing its presence in the most modern and original websites: the animations based on the scroll event of Javascript.This trend literally exploded when the parallax effects appeared, and since then its use has become more frequent.

Table of Contents

If you are using a custom animation css instead of animate.css then you have to tweak the above line and use an option flag to use a different animation trigger other than ‘animated’ (see the codepen source since it has that option) All the hard work is now done. Now just add the appropriate CSS too items you want to animate on scroll. Generated while processing qtwebkit/Source/WebCore/platform/ScrollAnimatorSmooth.cpp Generated on 2019-Aug-02 from project qtwebkit revision v5.212.0-alpha2-4. Creating Scroll Animations with Waypoints and Animate.css Last fall Atomic Object launched a redesigned website with lots of great new visuals and content. One of my favorite features of the redesign is the “slide-in reveal” homepage animation that slides images and text in from the sides of the screen as you scroll down the page.

But the truth is that you must be very careful when implementing an animation based on scroll, since it can seriously affect the performance of the website, especially on mobile devices.

That's why we invite you to continue reading the tutorial, where we will implement from scratch and with vanilla Javascript, this beautiful animation based on scroll, also keeping good performance even on mobile devices:

Let's start!

HTML Structure

We will use a simple HTML structure, where each image in the design will actually be a div element in the HTML code, and the images will be defined and positioned with CSS, which will facilitate this task:

Now let's look at the CSS styles needed to achieve the desired design.

Applying CSS styles

First let's start by creating the layout.This time we will use a CSS Grid, taking advantage of the fact that this technology is already supported in all modern browsers.

It is important to note that, in addition to the CSS grid, we are giving the .container element a fixed position. This will make this element not affected by the default scroll behavior, allowing to perform custom transforms with Javascript.

Animatorsmooth Animations On Scroll

Now let's see how to define the styles associated with the images. See the comments for a brief explanation of each part:

Now let's make some adjustments for small screens, since there we should have a column instead of two.

And this way we have our design almost ready, we just need to add the background to the body, which we will not explain so as not to extend the tutorial with trivial details.

Note also that for now you will not be able to scroll, since we have given a fixed position to the container element. Next we will solve this problem and bring our design to life :)

Implementing Animations with Javascript

Now let's see how to implement, from scratch and using vanilla Javascript, a custom scroll movement, smoother and suitable for the animations planned. All this we will achieve without trying to reimplement all the work associated with the scroll that the web browser does. Instead, we will keep the native scroll functionality, at the same time that we will have a custom scroll behavior.Sounds good, huh? Let's see how to do it!

Useful Functions and Variables

First let's look at some useful functions that we will be using. Lean on the comments for a better understanding:

And these are the variables that we will be using also, described briefly to understand much better the code that we will present below:

With all this ready, let's see how to implement our custom scroll behavior.

Implementing the Custom Scroll Behavior

To make our webpage scrollable, we will add a new div element to the body dynamically, to which we will set the same height of our container element, in such a way that the scrollable area will be the same.

Animatorsmooth

We also need a bit of CSS styles so that our .fake-scroll element makes the page scrollable, without interfering with the layout and the other elements:

Now let's see the function responsible for calculating all the necessary dimensions, and preparing the ground for the animations:

Once the setupAnimation function is called, the page will be scrollable, and everything will be ready to start listening to the scroll event and run the animation.

So let's see what we will do when the scroll event is triggered:

Easy! Each time the scroll event is triggered, you simply update the target variable with the new position, and call the startAnimation function, which does nothing but start the animation if it is not active yet. Here is the code:

Now let's see the internal behavior for the updateAnimation function, which is the one that actually performs all calculations and transformations in each frame, to achieve the desired animation.Please follow the comments for a better understanding of the code:

And our custom scroll behavior is ready!After calling the function setupAnimation, you could scroll as you normally would, and the .container element would be moved in correspondence, but with a very smooth and pleasant effect :)

Then we only have to animate the images in correspondence with the position in which they are with respect to the viewport. Let's see how to do it!

Animating Images While Scrolling

Scroll

To animate the images we will use the current position of the fake scroll (current), and we will calculate the intersectionRatio (similar to the value from the IntersectionObserver API) between each image and the viewport. Then, we just have to apply the transformations that we want depending on that ratio, and we will obtain the desired animation.

The idea is to show the image without any transformation when it is in the center of the screen (intersectionRatio = 1), and to increase the transformations as the image moves towards the ends of the screen (intersectionRatio = 0).

Pay close attention to the code shown below, especially the part where the intersectionRatio for each image is calculated. This value is essential to then apply the appropriate CSS transformations. Please follow the comments for a better understanding:

Init Animation

And we are almost ready to enjoy our animation. We only need to make the initial call to the setupAnimation function, in addition to updating the dimensions in case the resize event is triggered:

Scroll! Scroll! Scroll!

Fixing the Jump Issue for Mobile Devices

So far, everything should work perfectly in Desktop, but the story is very different if we try the animation on mobile devices.

The problem occurs when the address bar (and the navigation bar in the footer of some browsers) hides when it is scrolled down, and is shown again when scrolling upwards. This is not a bug, but a feature. The problem appears when we use the CSS unit vh, since in this process that unit is recalculated, resulting in an unwanted jump in our animation.

The workaround that we have implemented is to use the small library vh-fix, which defines, for each element with the class .vh-fix, a static height based on their vh value and the viewport height.In this way, we should no longer have unwanted jumps :)

Conclusions

And we have finished implementing this beautiful scroll based animation:

You can check the live demo, play with the code in Codepen, or check the full code in the repository in Github.

Please keep in mind that the objective of this tutorial is essentially academic and to be used as inspiration. To use this demo in production other aspects must be taken into account, such as accessibility, browser support, the use of some debounce function for the scroll and resize events, etc.

Without further ado, we hope you enjoyed it and it has been useful!

Credits

  • The demo was inspired by this web page: Jorik.
  • All the images are from Unsplash.
  • The SVG background used was customized at SVGBackgrounds.com.

Like this article?Follow @lmgonzalves on Twitter

Read next...

Last fall Atomic Object launched a redesigned website with lots of great new visuals and content. One of my favorite features of the redesign is the “slide-in reveal” homepage animation that slides images and text in from the sides of the screen as you scroll down the page.

I was recently asked to do some updates and revisions on the website, and so took the opportunity to dig into how the animations work. It turns out that achieving the animations is relatively simple.

Here’s how you can implement your own “slide-in reveal” animations:

What You’ll Need

  • Waypoints – A javascript library that allows us to execute a function when we scroll to an element.
  • Animate.css – A CSS animation library that enables sophisticated animations through HTML classes.
  • jQuery – Although not a dependency of either Waypoints or Animate.css, jQuery allows us to easily trigger our animations.

1. Using Waypoints

The basic Waypoint function in jQuery looks like this:

The 'offset' value determines at what point the animation is triggered. By default, the animation is triggered when the element you are animating reaches the top of the viewport.

The offset can be set as a percentage of the viewport (eg, setting 'offset: 30%' triggers the animation when the element is 30% from the top of the window), or as a value that translates to pixels (setting 'offset: 100' triggers the animation when the element is 100px from the top of the window).

Generally, we’ve found that having the animation trigger lower on the screen works best. This would translate into a larger offset value.

Animatorsmooth Animations On Scroll Shapes

2. Combining with Animate.css

Animatorsmooth

To create the actual animation, we use Animate.css. Animate.css allows us to create sophisticated animations solely through CSS classes—we simply add the 'animated' class and the animation name (we use 'fadeInRight', 'fadeInLeft', and 'fadeInUp') to the target element.

When paired with Waypoints, we can wait until the user reaches a certain point on the screen before adding the actual animation name, thereby firing the animation at the exact desired moment:

3. Tying it All Together

Finally, we’ll need to hide the element we wish to animate in order to create for the “reveal” effect that we want to achieve. Fortunately, Animate.css gives us the handy 'fade' class to do this. Update: We can easily hide the element by setting its opacity to zero with CSS.

The 'fade' class sets the 'opacity' of the element to zero, thereby hiding it but not taking it out of the page structure. This is important, as we don’t want the page to reflow when our element is revealed.

Here it is all together:

Animatorsmooth Animations On Scroll Cursor

To see Waypoints and Animate.css in action, head over to the Atomic Object homepage and scroll down!





Coments are closed