Open In App

How to zoom-in and zoom-out image using JavaScript ?

Given an image, the task is to increase and decrease the image size using JavaScript. Use the following property to increase and decrease the image.

These are the following ways:



Using Width property

Syntax:

object.style.width = "auto|length|%|initial|inherit";

Example:






<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to zoom-in and zoom-out
        image using JavaScript ?
    </title>
 
    <script src=
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
 
    <h3>
        JavaScript | Increase and
        Decrease image size
    </h3>
    <hr>
 
    <div class="box">
 
        <img src=
             id="geeks" GFG="250"
             alt="Geeksforgeeks">
    </div>
    <hr>
 
    <button type="button" onclick="zoomin()">
        Zoom-In
    </button>
 
    <button type="button" onclick="zoomout()">
        Zoom-Out
    </button>
 
    <script type="text/javascript">
        function zoomin() {
            let GFG = document.getElementById("geeks");
            let currWidth = GFG.clientWidth;
            GFG.style.width = (currWidth + 100) + "px";
        }
 
        function zoomout() {
            let GFG = document.getElementById("geeks");
            let currWidth = GFG.clientWidth;
            GFG.style.width = (currWidth - 100) + "px";
        }
    </script>
</body>
 
</html>

Output:

Output

Using Height property

Syntax:

object.style.height = "auto|length|%|initial|inherit"

Example:




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to zoom-in and zoom-out
        image using JavaScript ?
    </title>
 
    <script src=
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
 
    <h3>
        JavaScript | Increase and
        Decrease image size
    </h3>
    <hr>
 
    <div class="box">
        <img src=
             id="geeks" GFG="250"
             alt="Geeksforgeeks">
    </div>
    <hr>
 
    <button type="button" onclick="zoomin()">
        Zoom-In
    </button>
 
    <button type="button" onclick="zoomout()">
        Zoom-Out
    </button>
 
    <script type="text/javascript">
        function zoomin() {
            let GFG = document.getElementById("geeks");
            let currHeight = GFG.clientHeight;
            GFG.style.height = (currHeight + 40) + "px";
        }
        function zoomout() {
            let GFG = document.getElementById("geeks");
            let currHeight = GFG.clientHeight;
            GFG.style.height = (currHeight - 40) + "px";
        }
    </script>
</body>
 
</html>

Output:

Output


Article Tags :