Pull to refresh animation with Vanilla JavaScript
Tags: html, css, ui, javascript
Published: 2019-05-12 11:35:57

---
title: Pull to refresh animation with Vanilla JavaScript
published: true
description: 
tags: html, css, ui, javascript
---


<img src="https://thepracticaldev.s3.amazonaws.com/i/naxkjcuajuvanrfym6cu.gif">


Pull-to-refresh is a very popular swipe gesture on smartphones that allow users to load new content on top of listed data. Its used in all popular apps for smartphones and as well as in web apps these days and has become a major part of UX.

<hr>

A few days ago I came across this kind of animation on <a href="https://dribbble.com/shots/4963449-Pull-to-Refresh-Animation">dribbble</a>, so I thought why not come up with my version of it.

Guess what, I managed to build this with no libraries or JQuery plugin, just plain Vanilla JavaScript. 😎

A very simple markup , a top loading container that will contain the loader and a bunch of cards.

<img src="https://cdn-images-1.medium.com/max/800/1*_Ih6pqytK38ZhOJ_anWDuw.png">

Let’s get to the fun stuff now 😁.

<img src="https://cdn-images-1.medium.com/max/800/1*BuVmdgooGuq8bNUs97wDOw.png">

The idea is to hide the loading container by default and slide it down when the user swipes down.
<b>transform-style : preserve-3d</b> will enable the cards to be positioned in 3D space and the <b>perspective</b> property on the card wrapper will determine how the user will see from their perspective; lower values will produce more intense 3D effects. Note that perspective is not set on the child elements.
<a href="https://3dtransforms.desandro.com/">More about CSS 3D animations and transforms</a>.

That was it for the CSS, now let’s begin adding the swipe gesture.

<img src="https://cdn-images-1.medium.com/max/800/1*zpDoJNqjDREy9U5uXE2Kxw.png">

Touch events in JS are handled by the touch event listeners. These are similar to mouse events except they allow multiple touch on the touch surface.

<img src="https://cdn-images-1.medium.com/max/800/1*qtypiV59daSkoDAdaZ3VBQ.png">

pStart and pCurrent objects will store the touch positions of the start and current touch. We’ll require these to find the change in the Y coordinate to calculate the rotation of the cards.

<img src="https://cdn-images-1.medium.com/max/800/1*5HSwcCE7va1TREQ-Rj4u7w.png">

In the swipeStart() function, we’ll capture the touch coordinates and assign it to the pStart object.

<img src="https://cdn-images-1.medium.com/max/800/1*_yDlEjxwPY82fVqm90JnmQ.png">

In the swipe() function, we are setting the pCurrent object with current touch coordinates. Next step is to find the difference between the start and current Y position to calculate the distance of swipe and make a rotation based on the 30th percent of the distance. The loading container will only appear if the distance is greater than 100.

<img src="https://cdn-images-1.medium.com/max/800/1*Vl8xswbts96XM06u5CtQlA.png">

The swipeEnd() function will undo the rotation if the touch has begun but has not enabled the loading.

<img src="https://cdn-images-1.medium.com/max/800/1*cdXAjW9Ge8RMSZl7afiOPg.png">

While loading, the loading container will slide down for 2 seconds for the purposes of demonstration but in a real world example it can take longer or less time based on the response from the server. The loading container and cards will get back to original state once the loading has finished.

I took reference for JavaScript from <a href="https://stackoverflow.com/questions/46190436/how-to-detect-pull-to-refresh">this</a> stackoverflow question.

Complete source code in my <a href="https://codepen.io/Vijit_Ail/pen/pmbypw">codepen</a>. Open the devtools to see the animation in action.

<hr>

I hope you got to learn something new today 😄. If you have any questions or have any suggestions for improving the code, feel free to write your response.

Cheers and happy coding 🍻 ✌

<i>This post was originally shared on medium. Check out my medium profile <a href="https://medium.com/@vijit2ail">@vijit2ail</a></i>