简体   繁体   English

如何根据屏幕大小更改网页缩放?

[英]How to change webpage zoom based on screen size?

Hi so I created a web page that looks fine on my desktop monitors.嗨,我创建了一个 web 页面,在我的桌面显示器上看起来不错。 When I viewed the page using a laptop, the page looks abnormal.当我使用笔记本电脑查看页面时,页面看起来异常。 However, when I zoom the page to 67% it appears fine.但是,当我将页面缩放到 67% 时,它看起来很好。 Both my monitors and my laptop has the same resolution, 1920 x 1080, but my monitor is bigger than my laptop's one.我的显示器和笔记本电脑的分辨率相同,1920 x 1080,但我的显示器比笔记本电脑的大。 I know that i can change zoom in CSS but it would look bad on my pc.我知道我可以更改 CSS 的缩放,但在我的电脑上看起来很糟糕。 I was wondering is there a way to change zoom based on screen size in javascript or CSS.我想知道有没有办法根据 javascript 或 CSS 中的屏幕尺寸更改缩放。

One option could be using media queries in your CSS to change the styles that get applied depending on the size of your screen.一种选择是在 CSS 中使用媒体查询来更改根据屏幕大小应用的 styles。 You could have one set of styles that works for larger screens like your PC, and another that applies for smaller screens like your laptop.您可以拥有一套适用于 PC 等大屏幕的 styles,另一套适用于笔记本电脑等小屏幕。

This can be done with JavaScript if you include the viewport meta tag in your HTML.如果您在 HTML 中包含视口元标记,则可以使用 JavaScript 完成此操作。

if (document.body.clientWidth < /*screen width pixels*/) {
  viewport = document.querySelector("meta[name=viewport]");
  viewport.setAttribute('content', 'width=device-width, initial-scale=0.67, user-scalable=0');
}

You can substitute the properties below with different values to test out what works for you.您可以用不同的值替换下面的属性,以测试适合您的属性。

initial-scale property controls the initial zoom level (possible range between 10%-1000% or 0.1-10.0). initial-scale属性控制初始缩放级别(可能范围在 10%-1000% 或 0.1-10.0 之间)。

user-scalable=0 prevents users from zooming user-scalable=0防止用户缩放

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

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