0

I want to make a button that zoom in/out the page in Google Chrome.(Like if i'm pressing ctrl and + or ctrl and -). Please help me. I don't want to zoom element(body).

MikeyJY
  • 41
  • 2
  • 2
  • 7
  • Please try this: https://stackoverflow.com/questions/10464038/imitate-browser-zoom-with-javascript#10464073 – enxaneta Oct 13 '18 at 19:43
  • 1
    Possible duplicate of [imitate browser zoom with JavaScript](https://stackoverflow.com/questions/10464038/imitate-browser-zoom-with-javascript) – Darren Oct 14 '18 at 01:44

3 Answers3

3

There are many ways to do it,you have to write both ZoomIn and ZoomOut functions for it.

below is the working code.

function zoomIn()
{
  var Page = document.getElementById('Body');
  var zoom = parseInt(Page.style.zoom) + 10 +'%'
  Page.style.zoom = zoom;
  return false;
}

function zoomOut()
{
  var Page = document.getElementById('Body');
  var zoom = parseInt(Page.style.zoom) - 10 +'%'
  Page.style.zoom = zoom;
  return false;
}

(though it is Copied code).

And make sure you add style=”zoom: 100%” in your <body> tag of web page.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
0

Here is how to zoom in and out.

                function zoomIn(){
                var body = document.querySelector("body");
                var currWidth = body.clientWidth;
                if(currWidth == 1000000){
                    alert("Maximum zoom-in level of 1 Million reached.");
                } else{
                    body.style.width = (currWidth + 50) + "px";
                } 
            }
            function zoomOut(){
                var body = document.querySelector("body");
                var currWidth = body.clientWidth;
                if(currWidth == 500000){
                    alert("Maximum zoom-out level reached.");
                } else{
                    body.style.width = (currWidth - 50) + "px";
                }
            }

Zoom in by running zoomIn() Zoom out by runnint zoomOut()

0

set the zoom property in style of top level html element. Its the closest thing to browser zooming.

document.firstElementChild.style.zoom = 0.85;

You can use a step value of, say 0.05 on - and + buttons.

If you want to do it according to window. you can do it by:

window.onresize = function(){
    let zoom = Number((window.innerWidth / window.screen.width).toFixed(3));
    document.firstElementChild.style.zoom = zoom;
  }
Ronn Wilder
  • 1,228
  • 9
  • 13