简体   繁体   English

如何使用 javascript/css 强制网站页面缩放级别为固定值?

[英]How to use javascript/css to force a website page zoom level to fixed value?

I need html/css/javascript to fix everyone's zoom level on a website page.我需要 html/css/javascript 来修复网站页面上每个人的缩放级别。 Does anyone have an idea on the javascript?有没有人对javascript有想法?

The only way I found that works natively is in designing my HTML/CSS with the units "vw" and "vh" (% relative to the viewport) instead of "px".我发现本机工作的唯一方法是使用单位“vw”和“vh”(相对于视口的百分比)而不是“px”来设计我的 HTML/CSS。 You can use it everywhere you used to put "px" (font-size, width, height, padding, margin, etc...).您可以在用于放置“px”的任何地方使用它(字体大小、宽度、高度、填充、边距等...)。 Very useful for a page designed to be display full screen only (no scroll) or "Kiosk-style".对于设计为仅显示全屏(无滚动)或“信息亭样式”的页面非常有用。 "vw" and "vh" are not affected by browser zoom. “vw”和“vh”不受浏览器缩放的影响。 See: https://www.w3schools.com/cssref/css_units.asp请参阅: https : //www.w3schools.com/cssref/css_units.asp

You can use the document.onLoad to change the scale in CSS through Javascript:您可以使用 document.onLoad 通过 Javascript 更改 CSS 中的比例:

var minimum_width = 840;        // Put you own value here
var desired_width = 1440;       // and here
var actual_width = document.all ? document.body.clientWidth : window.innerWidth;
var actual_height = document.all ? document.body.clientHeight : window.innerHeight;
if (desired_width > actual_width) {
    desired_width = actual_width;
}
if (desired_width < minimum_width) {
    desired_width = minimum_width;
}
var scale = Math.round(actual_width/desired_width*100)/100;
var desired_height = Math.round(actual_height/scale);
var body = document.body;
body.style.transform = "scale(" + scale + ")";
body.style.width = desired_width + "px";
body.style.minHeight = (desired_height) + "px";

Will work on most popular browsers.将适用于大多数流行的浏览器。

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

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