JavaScript - Simple Flip Image Using jQuery

In this tutorial we will create a Simple Flip Image using jQuery. This code can animate a flip effect on the images when the user click the button. The code itself use jQuery onclick function to manipulate the image stylesheet by adding a transform with a degree value, to be able to flip the image. This is a user-friendly program feel free to modify it. We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And this is the link for the jquery that i used in this tutorial https://jquery.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-widht, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple Flip Image Using jQuery</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <center><img id="imageTarget" src="image.jpeg"></center>
  17.                 <br />
  18.                 <center><button id="flip" class="btn btn-primary">Flip Image</button></center>
  19.         </div>
  20. <script src="js/jquery-3.2.1.min.js"></script> 
  21. <script src="js/script.js"></script>
  22. </body>
  23. </html>

Creating the Script

This code contains the script of the application. This code will flip the image when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. $("#flip").on("click",function(){
  2.         if($("#imageTarget").css("transform") == 'none') {
  3.                 $("#imageTarget").css("transform","rotatey(180deg)");
  4.         }
  5.         else {
  6.                 $("#imageTarget").css("transform","");
  7.         }
  8. })
There you have it we successfully created a Simple Flip Image using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment