I think it’s safe to say that micro-interactions are cool. We – web people – universally all enjoy them. And no matter if you’re noticing them or not, they provide a significant user experience improvement. By making your website or web tool come to life subtly and in a friendly way, they simply make the user enjoy the whole interaction a tiny bit more.
In that spirit, I want to give you a quick micro-interactions tutorial and show you how to create a cool “Like” button for your site. With minimal code. Here it is in action:
Pretty exciting for a thumbs-up button, isn’t it? Let’s get to it!
But first:
Why bother with micro-interactions in the first place?
I’m not going to try to convince you too heavily here, but let’s just look at a couple of things.
Micro-interactions have been around for a while – enough so that they even have full books written about them. Plus, there have been many experiments and studies looking into how useful and effective they actually are.
One of the most notable research houses, the Nielsen Norman Group, looked at micro-interactions more closely in 2018. As per their conclusions, micro-interactions have proven to, among other things, improve user experience by encouraging engagement, and communicating brand.
In short, micro-interactions can be really beneficial if used right and, also, not too heavily. Today, we’re going to show you one simple example of a micro-interaction in action:
Micro-interactions tutorial: The markup and basic CSS of our thumbs-up button
? Note; at the bottom, we have some options for you that don’t involve writing code by hand.
First of all, we need some good old HTML and CSS magic to make it work. The structure is pretty straightforward.
We are going to use an SVG
tag for the like icon itself – this is from Font Awesome; you can find most of the icons here. Just to adjust the width and height attributes of the SVG
tag, we’re going to use the HTML below:
Now for the CSS, we’re going to go real basic. What we want to happen here is to give a nice look to our like button and have everything nicely centered on the screen. We already know that we’re going to toggle a class upon click (the active
class), so we need to account for that. We’re going to fill the icon with a blue color when active and dark-gray when not:
body { display: flex; align-items: center; justify-content: center; height: 100vh; } .like-button { display: flex; align-items: center; justify-content: center; } .like-button.active { -webkit-animation: pop 0.9s both; animation: pop 0.9s both; } .like-button svg { opacity: 1; } .like-button svg path { fill: #333; transition: fill .4s ease-out; } .like-button.active svg path { fill: #2196f3; }
Another thing we want here is the pop
animation used when the button becomes active, so we’re going to add that to our CSS file:
@-webkit-keyframes pop { 0% { -webkit-transform: scale3d(1, 1, 1); transform: scale3d(1, 1, 1); } 30% { -webkit-transform: scale3d(1.25, 0.75, 1); transform: scale3d(1.25, 0.75, 1); } 40% { -webkit-transform: scale3d(0.75, 1.25, 1); transform: scale3d(0.75, 1.25, 1); } 50% { -webkit-transform: scale3d(1.15, 0.85, 1); transform: scale3d(1.15, 0.85, 1); } 65% { -webkit-transform: scale3d(0.95, 1.05, 1); transform: scale3d(0.95, 1.05, 1); } 75% { -webkit-transform: scale3d(1.05, 0.95, 1); transform: scale3d(1.05, 0.95, 1); } 100% { -webkit-transform: scale3d(1, 1, 1); transform: scale3d(1, 1, 1); } } @keyframes pop { 0% { -webkit-transform: scale3d(1, 1, 1); transform: scale3d(1, 1, 1); } 30% { -webkit-transform: scale3d(1.25, 0.75, 1); transform: scale3d(1.25, 0.75, 1); } 40% { -webkit-transform: scale3d(0.75, 1.25, 1); transform: scale3d(0.75, 1.25, 1); } 50% { -webkit-transform: scale3d(1.15, 0.85, 1); transform: scale3d(1.15, 0.85, 1); } 65% { -webkit-transform: scale3d(0.95, 1.05, 1); transform: scale3d(0.95, 1.05, 1); } 75% { -webkit-transform: scale3d(1.05, 0.95, 1); transform: scale3d(1.05, 0.95, 1); } 100% { -webkit-transform: scale3d(1, 1, 1); transform: scale3d(1, 1, 1); } }
The “interaction” part of this micro-interactions tutorial
Now that we’ve got the basic styling out of the way, it’s time to handle the actual interactions – that means JavaScript!
First of all, we’re going to need a JavaScript function that generates a random integer between a given interval (a minimum and a maximum):
function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }
Next up, we’re going to need another function that randomly returns +1
or -1
so we can have random negative or positive values:
function plusOrMinus() { return Math.random() < 0.5 ? -1 : 1; }
Now for the actual interaction handling; let’s look at the code to explain it properly:
- First up, we’re going to save our button in a variable and add a click event listener to it.
- When the button is clicked, we’re going to add a
preventDefault()
function so that we’re sure that nothing unwanted happens. - After that, we’re going to add our
active
class to the button, which we previously styled with a blue color via CSS. - Then we’re going to run the
generateClones()
function (that isn’t defined yet), passing our button as a parameter viathis
.
let button = document.querySelector(".like-button"); button.addEventListener("click", function(e) { e.preventDefault(); this.classList.toggle("active"); generateClones(this); });
Now let’s define the generateClones()
function and run through the following steps with it:
- First, let’s decide on a random number of clones. We’re going to instantiate between two and four clones for the icon. We’re going to use the previously defined
randomInt()
function. - Then, for each clone, we’re going to use the
SVG
tag inside our button as a “model” and use thecloneNode(true)
JavaScript function to clone it into a new variable. We’re also going to define a size between 5 and 16 for our clone using the random integer generator function. - Next up, we’re going to add our clones to the button, set their width and height with the previously generated number, and make their position absolute so that we can move them around independently from the button.
- Another thing we need is a transition for our clones, so they don’t just pop into their new place when clicking.
- Now, for adding the actual transformations that will move our icon around, we’re going to go a bit crazy. We want our clones to move from the center of our button outwards randomly. Using a combination of our
randomInt()
andplusOrMinus()
functions, we’re going to move our clones in the XY axis with thetranslate3d
CSS property. We’re also going to use a “hack”, as our transition wouldn’t apply otherwise. So adding a simplesetTimeout()
function is going to help us achieve that. At the same time, we’re going to move the opacity to 0, making the clones disappear. - Deleting our clones. We have to clear up the DOM after we’re done with all the jazz. So we need another
setTimeout()
function that’s going to remove our clones from the DOM after one second.
generateClones(button) { // 1. Generate clones and iterate over the set number let clones = randomInt(2, 4); for (let it = 1; it <= clones; it++) { // 2. Get the SVG tag clone in a variable and generate a random size let clone = button.querySelector("svg").cloneNode(true), size = randomInt(5, 16); // 3. Add our clone to the button and change its' width/height/position button.appendChild(clone); clone.setAttribute("width", size); clone.setAttribute("height", size); clone.style.position = "absolute"; // 4. Add some flashy transitions clone.style.transition = "transform 0.5s cubic-bezier(0.12, 0.74, 0.58, 0.99) 0.3s, opacity 1s ease-out .5s"; // 5. Animate our icon outwards with a setTimeout function so that CSS complies. let animTimeout = setTimeout(function() { clone.style.transform = "translate3d(" + plusOrMinus() * randomInt(10, 25) + "px," + plusOrMinus() * randomInt(10, 25) + "px,0)"; clone.style.opacity = 0; clearTimeout(animTimeout); }, 1); // 6. Removing the clones after 0.9 seconds let removeNodeTimeout = setTimeout(function() { clone.parentNode.removeChild(clone); clearTimeout(removeNodeTimeout); }, 900); } }
That’s it! We’ve made it through the end! Down below you’ll find the CodePen example:
See the Pen
Like Button by Andrei Băicuș (@abaicus)
on CodePen.
Thanks for going through this!
Other options to add micro-interactions to WordPress
If writing code manually isn’t entirely your thing, there are actually quite a handful of WordPress plugins that you can use to get micro-interactions onto your site without needing to worry about how it all works under the hood.
First, you can use the Animate It! plugin to animate pretty much any element on your pages using CSS3.
For example, you can add a button, or a piece of text, and then set it to animate in a certain way on click.
Here’s an example of what the plugin can do – video ?.
Another plugin, and one of our editor’s favorites, is Shortcodes Ultimate.
Among a whole plethora of different shortcodes, there’s one for simple animations. There are some presets as to how the animation should behave, and you can put pretty much anything inside the block that’s going to get animated.
Here’s an example of an animated button (note; the animation works only the first time it’s scrolled into the viewport; if you don’t see it, refresh the page):
These two plugins should give you more than enough options to animate certain elements on your pages and make them pop a little more when the user is interacting with them. Of course, you don’t want to go overboard with those, but giving the user an additional nudge when they’re interacting with your key call to actions might help a lot.
What do you think of micro-interactions, are you considering putting this micro-interactions tutorial to good use and adding something to your site? Let us know in the comments.
…
Don’t forget to join our crash course on speeding up your WordPress site. With some simple fixes, you can reduce your loading time by even 50-80%:
Content Copyrights Belong to The Author. All Rights Reserved.
We're A Dallas Digital Marketing Agency That is Experts At Social Media Marketing, Website Design and Emarketing and Promotion.