简体   繁体   English

HTML全页面缩放取决于屏幕分辨率

[英]HTML full page zoom depending on screen resolution

I have a problem with displaying my html site on different monitors/resolutions. 我在不同的显示器/分辨率下显示html网站时遇到问题。 I was trying to to solve this problem with the following script, but it isn't working. 我正在尝试使用以下脚本解决此问题,但是它不起作用。 How could I improve this? 我该如何改善?

 if (width <= 1280 && height <= 720) { document.getElementById('html').style.zoom = '50%'; 
 html { zoom: 100%; } 

You could scale the content without javascript, just using a mediaquery and a CSS3 transformation applied to the html element 您可以在不使用javascript的情况下缩放内容,只需使用mediaquery和应用于html元素的CSS3转换

@media screen and (max-width: 1280px) and (max-height: 720px) {
   html {
      transform: scale(.5);   
      // or simply zoom: 50%
   }
}

as a side note your code can't work because you're looking for an element with id="html" , while you're trying to target the html element (that is document.documentElement or document.querySelector('html') ) 顺便说一句,您的代码无法正常工作,因为您在尝试定位html元素(即document.documentElementdocument.querySelector('html')时,正在使用id="html"的元素)

I believe this is more of a viewport and/or css media query issue. 我相信这更多是一个视口和/或CSS媒体查询问题。 You shouldn't be trying to fix your pages look with javascript because it can be disabled. 您不应该尝试使用javascript来修复页面外观,因为可以将其禁用。 I would suggestion reading up on viewports Viewport Overview . 我建议阅读视口概述 The most commonly used paired tags are: 最常用的配对标签是:

<meta name="viewport" content="width=device-width">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

Those will help with resizing most of the content on the page based on the device width, other changes you'll have to manage more manually via css media queries, here is an example: 这些将有助于根据设备宽度调整页面上大多数内容的大小,您还需要通过css媒体查询来手动管理其他更改,下面是一个示例:

@media screen and (max-width: 300px) {
    body {
        width: 80%;
        font-size: 15px;
    }
}

The above corresponds to, at 300px or smaller change the width and font size. 上面的对应于,以300px或更小的宽度和字体大小更改。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM