How to Create Twitter Heart Button Animation

In this article, we are going to create Twitter Heart Button Animation with CSS3. Twitter has released a new heart button, it's a replacement for the favorite button. This heart button it's a cool animation effect for click action. This article will help you how to create this using CSS3 and jQuery. Result

Creating Markup

Displaying example of a news feed. Copy and paste this HTML source code to your BODY tag of your page.
  1. <div id="container">
  2. <h1>Twitter Heart Button Animation with CSS3</h1>
  4. <h3>Click on heart button</h3>
  6. <div class="newsFeed" id="newsFeed1">
  7. <p>Free Source Code - PHP <a href="" target="_blank"></a></p>
  8. <div class="heart_animation_Class " id="like1" rel="like"></div> <div class="count_For_Like" id="count_For_Like1">195</div>
  9. </div>
  11. <div class="newsFeed" id="newsFeed2">
  12. <p>Free Source Code - C/C++ <a href="" target="_blank"></a></p>
  13. <div class="heart_animation_Class" id="like2" rel="like"></div> <div class="count_For_Like" id="count_For_Like2">149</div>
  14. </div>
  16. <div class="newsFeed" id="newsFeed2">
  17. <p>Free Source Code - Visual Basic .NET <a href="" target="_blank"></a></p>
  18. <div class="heart_animation_Class" id="like3" rel="like"></div> <div class="count_For_Like" id="count_For_Like3">159</div>
  19. </div>
  20. </div>

JavaScript Codes

It contains the class name of the DIV tag. Copy and paste this script and link to your HEAD tag of your page.
  1. <script src="js_code.js"></script>
  2. <script>
  3. $(document).ready(function()
  4. {
  6. $('body').on("click",'.heart_animation_Class',function()
  7. {
  8. var A=$(this).attr("id");
  9. var B=A.split("like");
  10. var messageID=B[1];
  11. var C=parseInt($("#count_For_Like"+messageID).html());
  12. $(this).css("background-position","")
  13. var D=$(this).attr("rel");
  15. if(D === 'like')
  16. {
  17. $("#count_For_Like"+messageID).html(C+1);
  18. $(this).addClass("heartAnimation").attr("rel","unlike");
  20. }
  21. else
  22. {
  23. $("#count_For_Like"+messageID).html(C-1);
  24. $(this).removeClass("heartAnimation").attr("rel","like");
  25. $(this).css("background-position","left");
  26. }
  27. });
  28. });
  29. </script>

Was this article helpful?

Kindly click the "Download Code" button below for full source code. Thank you very much. Hope that this tutorial will help you a lot. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment