Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force page zoom at 100% with JS

I created a little game in Canvas, but I have a problem. Some users who have the default zoom set to something other than 100% can't see the entire game page.

I have tried using this CSS:

zoom: 100%; 

This HTML

<meta name="viewport" content="initial-scale=1.0 , minimum-scale=1.0 , maximum-scale=1.0" /> 

And this JS:

style="zoom: 75%" 

Any ideas how to programatically set the page zoom?

like image 443
Crocsx Avatar asked Jan 13 '14 14:01

Crocsx


People also ask

How do you zoom in JavaScript?

JavaScript can be used to manipulate an HTML element's style property to create a zoom-in and zoom-out effect. To do this, simply access the width attribute using the style property and increase it or decrease it according to the requirement.

How do I change my browser zoom to 100?

By default, Chrome sets the zoom level to 100%. To manually adjust the settings, use the Ctrl key and “+” or “-” combos to increase or decrease the page magnification. If you are using a mouse, you can hold down the keyboard Ctrl key and use the mouse wheel to zoom in or out.

Can JavaScript detect the browser zoom level?

Currently there is no way to detect the zoom level with Javascript reliably.


2 Answers

You can set zoom property on page load

document.body.style.zoom = 1.0 

But, zoom is not a standard property for all browsers, I recommend using transform instead.

var scale = 'scale(1)'; document.body.style.webkitTransform =  scale;    // Chrome, Opera, Safari  document.body.style.msTransform =   scale;       // IE 9  document.body.style.transform = scale;     // General 

http://jsfiddle.net/5RzJ8/

like image 160
Fırat Deniz Avatar answered Sep 21 '22 09:09

Fırat Deniz


You can reset the code with this:

$("input, textarea").focusout(function(){     $('meta[name=viewport]').remove();     $('head').append('<meta name="viewport" content="width=device-width, maximum-scale=1.0, user-scalable=0">');      $('meta[name=viewport]').remove();     $('head').append('<meta name="viewport" content="width=device-width, initial-scale=yes">' ); }); 
like image 44
Linden Avatar answered Sep 21 '22 09:09

Linden