Force mobile browser zoom out with Javascript

前端 未结 5 1789
名媛妹妹
名媛妹妹 2020-12-03 17:26

In my web app, I have some thumbnails that open a lightbox when clicked. On mobile, the thumbnails are small and the user typically zooms in. The problem is that when they c

5条回答
  •  悲&欢浪女
    2020-12-03 18:17

    Dug through a lot of other questions trying to get something to zoom out to fit the entire page. This question was the most relevant to my needs, but had no answers. I found this similar question which had a solution, although implemented differently, and not what I needed.

    I came up with this, which seems to work in Android at least.

    1. initial-scale=0.1: Zooms out really far. Should reveal your whole website (and then some)
    2. width=1200: Overwrites initial-scale, sets the device width to 1200.

    You'll want to change 1200 to be the width of your site. If your site is responsive then you can probably just use initial-scale=1. But if your site is responsive, you probably don't need this in the first place.

    function zoomOutMobile() {
      var viewport = document.querySelector('meta[name="viewport"]');
    
      if ( viewport ) {
        viewport.content = "initial-scale=0.1";
        viewport.content = "width=1200";
      }
    }
    
    zoomOutMobile();
    

提交回复
热议问题