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

Determine image size (pre-upload)

One of the requirements for a project I am working on is to verify the dimensions (width and height) of images before uploading them. I have outlined 3 key checkpoints: 1. If the dimensions are less than 600 X 600 pixels, the upload should be rejected. ...

Cache application

Experimented with the following code snippet from w3schools: Coding in index.html <!DOCTYPE html> <html manifest="demo_html.appcache"> <body> <script src="demo_time.js"></script> ...

Refreshing the session cookie during an ajax request in CodeIgniter

Here is the ajax call I am using: remote: { url:"http://localhost/my_site/index_page/validate_name", type: "post" } Within the validate_name function, I am setting the session cookie in this way: $username = "t ...

`The challenge of properly handling quotation marks within quotation marks in JavaScript string literals

I am having trouble displaying text in a JavaScript tooltip Despite my efforts to escape the quotes and eliminate any line breaks, I keep encountering unterminated string literals. The specific text I want to show is: "No, we can't. This is going t ...

The combination of Backbone.js and underscore.js is causing disorganized HTML code

When it comes to creating HTML elements, I typically use a combination of backbone.js and underscore.js. Here is an example snippet from my usual workflow: _.template($('#html-container').html(), this.model.toJSON()); I then append this code wh ...

Node.js - Hitting maximum call stack size limit despite using process.nextTick()

I am currently developing a module for creating "chainable" validation in Express.js: const validatePost = (req, res, next) => { validator.validate(req.body) .expect('name.first') .present('This parameter is required') ...

Is it possible to use both interfaces and string union types in TypeScript?

My goal is to create a method that accepts a key argument which can be either a string or an instance of the indexable type interface IValidationContextIndex. Here is the implementation: /** * Retrieves all values in the ValidationContext container. ...

What is the reason behind JavaScript's restriction on using double comparison operators?

What is the reason behind javaScript not supporting double comparison? For example, in 64 < str.charCodeAt(i) && str.charCodeAt(i)<=77, why is it not possible to simplify it to 64 < str.charCodeAt(i)<=77? ...

What causes these images to stack on top of one another rather than align in rows? [HTML][CSS][Bootstrap]

I've implemented Bootstrap's grid system to showcase two rows, each containing two images. Here is the code: <section class="container"> <div class="row"> <figure class="column-sm-6"> <p>floor pl ...

Tips for generating an auto-incremented ID column in jQuery tables through the render method

My jQuery Datatable setup looks like this let purchasedProductTbl = $('#grdPurchasedProduct').DataTable({ filter: false, paging: false, lengthChange: false, searching: false, ordering ...

Processing the results from a recursive function call in Node.js

I have an objects array that needs to be processed with delayed calls to an external server, then collect all results into an array for postprocessing. Here is an example: (async () => { const serverOperation = () => new Promise(resolve => setT ...

Refresh Bootstrap modal with Ajax update

I have implemented a modal for user profiles on my website. When the "Edit Profile" button is clicked, I want to populate the fields with existing data using Ajax. However, as I am new to Ajax, I am facing difficulties in making it work. Here is the struc ...

"Need a hand with bookmarklets? Try using this code: document

Having an issue with my bookmarklet JavaScript where it always adds the www. portion to the domain. Any ideas on how to make it work by removing the www portion from the URL? Appreciate any help. javascript:void(window.open('http://siteanalytics.comp ...

Styling of checkboxes in jQuery Mobile is missing after an AJAX request

I am currently working on implementing an ajax call to retrieve a list of items from a json array and display them as checkboxes. While the items are loading correctly, they lack the jquery mobile styling. $(document).ready(function(){ ...

PHP ajax handling multiple requests

I've searched high and low on the internet, but couldn't find a solution to my problem. I am making multiple jQuery AJAX calls to a PHP script. Initially, I noticed that each call was being executed only after the previous one had finished. To a ...

Dealing with all errors across all pages in Angular using a comprehensive error handling approach

I am currently using AngularJS and attempting to capture all errors across all pages. However, I am encountering an issue where it does not catch errors in some instances. For example, when I trigger a ReferenceError in one of the controllers, it is not be ...

"When running next build, NextJS fetch() function throws an error indicating an invalid URL, but everything works fine when using

Currently, I am in the process of developing a NextJS React application and attempting to retrieve data from my server by using the following line of code: let data = await fetch('/api/getAllAlumniInfoList').then(res => res.json()) Interestin ...

Tips for dynamically modifying style mode in Vue.js

I am looking to implement a feature where the style can be changed upon clicking a button. In my scenario, I am using Sass/SCSS for styling. For example, I have three different styles: default.scss, dark.scss, and system.scss. The code for dark.scss look ...

Attempting to create a dynamic grid layout utilizing CSS to ensure optimal responsiveness

I am working on creating a webpage layout similar to the one in the link below: The maximum width for the layout is set to 1600px. However, I am facing challenges in making it fully responsive. This is because all the boxes are given widths in percentages ...

Struggling to locate a route for the React styled components image

I'm having trouble locating the correct path for the image in my React styled components. I believe the path is correct, but could the issue be related to styled-components? Check it out here import styled from "styled-components"; export defaul ...