How to Adjust Image Size Dynamically in JavaScript

How to Adjust Image Size Dynamically in JavaScript


In this tutorial we will create a How to Adjust Image Size Dynamically in JavaScript. This tutorial purpose is to teach you on how to adjust your image in the html page dynamically. This will cover all the important functionality that allow you to adjust the image size. 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 or application if you want to resize the background image. I will give my best to provide you the easiest way of creating this program Adjust Image Size. So let's do the coding.

Before we get started:

Here is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will only display image and the buttons. 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="containet-fluid">
  9. <a class="navbar-brand" href="">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 Adjust Image Size Dynamically</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-12">
  17. <center>
  18. <button class="btn btn-primary" onclick="adjustImage('large')">Large</button>
  19. <button class="btn btn-success" onclick="adjustImage('medium')">Medium</button>
  20. <button class="btn btn-danger" onclick="adjustImage('small')">Small</button>
  21. </center>
  22. </div>
  23. <br /><br style="clear:both;"/>
  24. <div class="col-md-12">
  25. <center><img src="image.jpg" id="image" style="width:50%; height:200px;"/></center>
  26. </div>
  27. </div>
  28. <script src="script.js"></script>
  29. </body>
  30. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically adjust the image size by clicking the button. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function adjustImage(str){
  2. if(str=="small"){
  3. document.getElementById('image').style.height="200px";
  4. document.getElementById('image').style.width="50%";
  5. }else if(str=="medium"){
  6. document.getElementById('image').style.height="300px";
  7. document.getElementById('image').style.width="70%";
  8. }else if(str=="large"){
  9. document.getElementById('image').style.height="350px";
  10. document.getElementById('image').style.width="100%";
  11. }
  12. }

In the code above we just simple create one method called adjustImage(), this function will adjust the image size base on the parameter you have been set. In my case we just set some parameter with small, medium, large. Each parameter has set of image sizes that will be apply to the image proportion after you clicked the event button.


The How to Adjust Image Size Dynamically in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Adjust Image Size Dynamically in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment