How to Increase and Decrease Image Size Using JavaScript
To increase and decrease image size. You can use JavaScript’s width or height property to proportionally increase or decrease the size of an image, such as the zoom in and out functions.
HTML Code:
<!DOCTYPE html> <html> <head> <title>Increase and Decrease Image Size in JavaScript</title> <script type="text/javascript"> // Put the Javascript code here. </script> </head> <body> <img src="https://via.placeholder.com/300" id="myImg"> <p> <button type="button" onclick="increase()">+ Increase</button> <button type="button" onclick="decrease()">- Decrease</button> </p> </body> </html>
JavaScript Code:
function increase() { var myImg = document.getElementById("myImg"); var width = myImg.clientWidth; if (width == 600) { alert("You have reached the maximum zoom level."); } else { myImg.style.width = (width + 20) + "px"; } } function decrease() { var myImg = document.getElementById("myImg"); var width = myImg.clientWidth; if (width == 40) { alert("You have reached the minimum zoom level."); } else { myImg.style.width = (width - 20) + "px"; } }
Resul |
---|
|