简体   繁体   English

如何仅增加桌面设备的字体大小

[英]How to increase font size only for desktop device

Working on ASP.Net MVC project, I don't want to just say... looking for a way to detect mobile / desktop device.在 ASP.Net MVC 项目上工作,我不想只是说......寻找一种检测移动/桌面设备的方法。 as on mozilla web site, they say ...your first step is to try to avoid it if possible.就像在mozilla web 网站上一样,他们说......你的第一步是尽可能避免它。 Start by trying to identify why you want to do it.首先尝试确定您为什么要这样做。

I searched and found a lot of answer but none of them are efficient or an official way to detect mobile device that's why I start with explaining why I need it.我搜索并找到了很多答案,但它们都不是有效的或检测移动设备的官方方法,这就是为什么我开始解释我为什么需要它的原因。

First task given to me was to increase font size for the entire web site and I did it and it is working well using below code给我的第一个任务是增加整个 web 站点的字体大小,我做到了,它使用下面的代码运行良好

    $(function increaseFont() {
    document.body.style.zoom = "150%"
});

now I got new requirement to decrease size to normal for mobile device only ... so I'm thinking of having a if conditional before the above JS function to detect if device is mobile or desktop.现在我有了新的要求,仅将移动设备的尺寸减小到正常值......所以我正在考虑在上述 JS function 之前有一个if条件来检测设备是移动设备还是桌面设备。

Any idea please?请问有什么想法吗?

I would use standard media queries to change CSS properties.我会使用标准媒体查询来更改 CSS 属性。 The code you're looking for would probably be something like the following:您正在寻找的代码可能类似于以下内容:

@media only screen and (min-width: 768px) { /* Anything larger than a tablet */
  body {
   font-size: large; /* Increases every font size */
  }
}

For more information, you could check out the W3 Schools Website .有关更多信息,您可以查看W3 学校网站

Hope that helps.希望有帮助。

You can read out the window inner width with JavaScript.您可以使用 JavaScript 读出 window 内部宽度。

This is probably the if condition you are asking for:这可能是您要求的 if 条件:

if (window.innerWidth > 768) {
   // do stuff for desktop devices
}

The value in the if condition determines the minimum required width (in px) of the viewport, to execute the code in its body. if 条件中的值确定视口所需的最小宽度(以 px 为单位),以执行其主体中的代码。

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

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