JavaScript - Dynamically Adjust Image Size

In this tutorial we will create a Dynamically Adjust Image Size using JavaScript. This code will change the image size when user click a button. The code use onclick() to initiate a function that will dynamically change the image size by binding the image id and set its element style with your desire size. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.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-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="containet-fluid">
  9. <a class="navbar-brand" href="https://www.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 - Dynamically Adjust Image Size</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-4">
  17. <center><button class="btn btn-primary" onclick="resizeImage('large')">Large</button></center>
  18. <br />
  19. <center><button class="btn btn-success" onclick="resizeImage('medium')">Medium</button></center>
  20. <br />
  21. <center><button class="btn btn-danger" onclick="resizeImage('small')">Small</button></center>
  22. </div>
  23. <div class="col-md-8">
  24. <center><img src="image.jpeg" id="image" style="width:300px; height:200px;"/></center>
  25. </div>
  26. </div>
  27. <script src="js/script.js"></script>
  28. </body>
  29. </html>
  30. </html>

Creating the Script

This code contains the script of the application. This code will dynamically change the image size when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. function resizeImage(str){
  2. if(str=="small"){
  3. document.getElementById('image').style.height="140px";
  4. document.getElementById('image').style.width="200px";
  5. }else if(str=="medium"){
  6. document.getElementById('image').style.height="200px";
  7. document.getElementById('image').style.width="300px";
  8. }else if(str=="large"){
  9. document.getElementById('image').style.height="350px";
  10. document.getElementById('image').style.width="100%";
  11. }
  12. }
There you have it we successfully created a Dynamically Adjust Image Size using JavaScript. 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