JavaScript - Simple Image Fade Effect Using jQuery

In this tutorial we will create a Simple Image Fade Effect using jQuery. This code can animate a fade effect on the images when the user hover the mouse pointer on it. The code itself use animate() to enable a fading effect on image by increase and decrease the opacity. 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. <meta charset="UTF-8" name="viewpot" content="width=device-width, initial-scale=1" />
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">JavaScript - Simple Image Fade Effect Using jQuery</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15.  
  16. <a href="images/image1.gif" style="margin:5px; float:left;"><img src="images/image1.gif" width="200" height="200"></a>
  17. <a href="images/image2.jpg" style="margin:5px; float:left;"><img src="images/image2.jpg" width="200" height="200"></a>
  18. <a href="images/image3.jpg" style="margin:5px; float:left;"><img src="images/image3.jpg" width="200" height="200"></a>
  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 fade the images when the mouse pointer hover on it. 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. $(document).ready(function(){
  2. $('a img').hover(function(){
  3. $(this).stop().animate({opacity: 1}, 300);
  4. }, function(){
  5. $(this).stop().animate({opacity:.3}, 500);
  6. });
  7.  
  8. $('a img').animate({
  9. opacity:.3
  10. });
  11.  
  12. });
There you have it we successfully created a Simple Image Fade Effect 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