Instructions on creating a number increment animation resembling Twitter's post engagement counter

I've been attempting to replicate the animation seen on Twitter's post like counter (the flipping numbers effect that happens when you like a post). Despite my best efforts, I can't seem to make it work. Here is what I have tried:

$(function () {
  $('#test').click(function () {
     var $text = $(this).text();
     $(this).addClass('up');
     $(this).text($text + 1);
     $(this).toggleClass('up down');
     $(this).toggleClass('down static');
  });
});
  .up {
      display: inline-flex;
      opacity: 0;
      transform: translate3d(0, -20px, 0);
      transition: 0.1s ease-in-out;
  }
  .down {
      display: inline-flex;
      opacity: 0;
      transform: translate3d(0, 20px, 0);
  }
  .static {
      display: inline-flex;
      opacity: 1;
      transform: translate3d(0, 0px, 0);
      transition: 0.1s ease-in-out;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="test" class=""> 1 </button>

Unfortunately, my attempts were unsuccessful.

Is there anyone who could assist me with this?

Answer №1

If you're looking for the animation tutorial, check out this link:

To improve your current counter code, you can try implementing a function that increments a number by 1 each time it's clicked:

$(function () {
  var $number = 1
  $('#test').click(function () {
     var $text = $(this).text()
     $number = $number + 1
     $(this).text($number)
  });
});
.up {
      display: inline-flex;
      opacity: 0;
      transform: translate3d(0, -20px, 0);
      transition: 0.1s ease-in-out;
  }
  .down {
      display: inline-flex;
      opacity: 0;
      transform: translate3d(0, 20px, 0);
  }
  .static {
      display: inline-flex;
      opacity: 1;
      transform: translate3d(0, 0px, 0);
      transition: 0.1s ease-in-out;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="test" class=""> 1 </button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to creating nested collapsing rows in AngularJS

My attempt to implement expand and collapse functionality in AngularJS for a section is not yielding the desired result. To demonstrate, I created a simple demo of collapsible/expandable sections in AngularJS which works fine. You can view it here. The ex ...

Ways to modify the screen when a click event occurs

To make the display block when clicking this icon: <div class="index-navi-frame-box"> <p onclick="expandFunction()"> <i class="fa fa-bars"></i> </p> </div> Here is what it should change to: <div class=" ...

R: Extracting data from web pages: The content appears to be formatted in XML but is not recognized as such; employing HTML

Working on scraping data from multiple years which are represented by different webpages. I have successfully extracted the 2019 data as expected, but encountering an error while attempting to preprocess the 2016 data in a similar manner. url19 <- &apos ...

Using Javascript to read a JSON string displayed in a URL

I'm working on developing a straightforward web application that extracts latitude and longitude data stored in a JSON format and places markers on a Google map based on this information. Currently, there is a program running on a server that generate ...

At what point is ng-if triggered?

I am currently in the process of developing functionality for buttons that open directives in modal windows. To keep things simple, I decided to use a modal container flagged with the versus directive: <div ng-if="vm.Modal.Modal1"> <directive-for ...

Using CSS units such as vw, vh, or percentage to specify height is not possible

In my Ionic app, I am adjusting the width and height of a div based on the viewport dimensions. This setup functions correctly on the browser and Android 4.4 devices. However, it does not work as expected on Android 4.2 (the height is constrained to the te ...

`Must have content in file input in order to be saved to MongoDB`

When attempting to save a registry in MongoDB using Node.js, it seems that the operation fails if an image is not selected. I would like the inclusion of an image in this process to be optional, rather than mandatory. router.post("/", upload.single("image" ...

Is there a way to apply border-radius to the header of a table in Bootstrap 5?

My use of Bootstrap 5 for styling a table has encountered an issue. Even with the border-radius set on the table-dark class, the thead element's border does not change. I am looking for a way to address this problem. Any suggestions? .table-dark { ...

I'm struggling to retrieve $wpdb data after using a PHP function to load my posts with AJAX

Hey there! I'm trying to create a cool feature where users can view post archives grouped by year. When they click on a specific year, all the posts from that year should be displayed. I've set up an AJAX call to my functions.php file, which con ...

What could be causing these "areas" or "panels" to intersect with each other?

As I work on constructing a website, I'm facing an issue with the top two sections. They appear correctly when my screen is full size, but as soon as I adjust the screen size, they jump down and create white space. It seems like I may have approached ...

Is it possible to show a pop-up window containing aggregated data when the jQuery double-click event

How can I create a pop-up window to display aggregated data when the Double-click event is triggered in jQuery? In my code, each questionId has multiple related reasons. When a user clicks or selects a questionId button/event, the selected questionId will ...

Guide to utilizing SVG animations for line drawing rather than just outlines

I've been experimenting with animating an SVG file to resemble the gif below, and I'm getting pretty close, but I seem to be encountering an issue where the outlines are drawn before being filled. I want the entire lines to be animated as shown i ...

Styling tables within HTML emails for Gmail and then utilizing PHPMailer to send the emails

I've been racking my brain over this for hours with no luck! Objective: Implementing inline styles for table, td, th, p elements in an HTML email intended for Gmail using PHPMailer. Challenge: Inline styles not being rendered HTML Snippet: <sec ...

Steps to develop a log-in API using Node.js

In the process of developing my web application, I have utilized node js exclusively for all functionalities and the web user interface has been successfully implemented. An issue that has come to light is that users are able to access the services API wi ...

Download files using AJAX in Laravel 5.6

I am faced with a challenge in my Laravel 5.6 REST API. Users have the capability to upload their files to a non-public folder. Now, I need to ensure that users can only download files if their JWT token is valid and they have the necessary privilege. H ...

Exploring the significance of a super in Object-Oriented Programming within JavaScript

During my studies of OOP in JS, I encountered the super() method which serves to call the constructor of the parent class. It made me ponder - why is it necessary to invoke the parent class constructor? What significance does it hold for us? ...

Is it possible to incorporate a label within a dropdown menu (<select>)?

How can I add a label inside a select box using Bootstrap? Here is an example: Before selecting an option After selecting an option, the label should appear in a small size like this: After selecting an option : <div class="form-group"> & ...

The anchor tag fails to trigger the onClick function in React

I'm having trouble updating the component state in my React app when clicking on an anchor tag within the render method. I've attempted to bind the function in the constructor, but the console.log statement is still not being called. Here's ...

Encapsulate the HTTP request within a common function for reus

My rest-provider service handles all of my http calls, but I've noticed that each function is repeating a lot of the same http code. It seems like I should create a shared function to avoid this repetition and make it easier to update in the future. ...

Even with e.preventDefault, the anchor link still opens in a new tab on the browser

I am dealing with an ajax call that triggers a REST API to return a list of all names, as well as another REST API that matches those names. For instance: /list returns: list1, list2, list3 and /api/list1.json returns: JSON data for list1.. Currently, ...