How to Flip your Background Image using jQuery

How to Flip your Background Image using jQuery

Introduction

In this tutorial we will create a How to Flip your Background Image using jQuery. This tutorial purpose is to allow you to flip your background image dynamically. This will cover all the functionality that will flip your image background. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system that needed to manipulate images directly. I will give my best to provide you the easiest way of creating this program Flip Background Image. So let's do the coding.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the interface of the program with image. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, 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">How to Flip your Background Image using jQuery</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <button id="flip" class="btn btn-primary">Flip</button> <button id="revert" class="btn btn-success">Revert</button>
  17. <br />
  18. <br />
  19. <img id="myImage" src="image.png" width="630">
  20. </div>
  21. <script src="js/jquery-3.2.1.min.js"></script>
  22. <script src="script.js"></script>
  23. </body>
  24. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will allow you to flip your background image dynamically. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. $(document).ready(function(){
  2. $("#flip").on("click",function(){
  3. if($("#myImage").css("transform") == 'none') {
  4. $("#myImage").css("transform","rotatey(180deg)");
  5. }
  6. });
  7.  
  8. $("#revert").on("click",function(){
  9. $("#myImage").css("transform","");
  10. });
  11. });

In the given code we first call the basic jQuery ready event to signal that the DOM of the page is now ready to be use. Then we will create function that will call the id of our button using the jQuery id target function $(""), and we bind it with the on() function. After that we will call the image id and use the css function and we will just tweak the transform by rotatey with 180degof value, this will force our background image flip the y position.

Output:

The How to Flip your Background Image using jQuery source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Flip your Background 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment