简体   繁体   English

如何在特定屏幕尺寸下屏蔽网站?

[英]How to block a site for a certain screen size?

My site is not available for displays smaller than 10". How can I determine the size of the screen to block access to the site at resolutions below 10"? 我的网站无法用于小于10英寸的显示器。如何确定屏幕尺寸以阻止低于10英寸的分辨率访问该网站? Thanks. 谢谢。

A computer can and has to know the resolution (in pixel) that it is displaying. 计算机可以并且必须知道其显示的分辨率 (以像素为单位)。 The image in this resolution is then send across a cable to another device which physically displays it. 然后,将通过该分辨率的图像通过电缆发送到另一台物理显示该图像的设备。 This could be a tiny LCD screen or a giant projector. 这可能是一个很小的LCD屏幕或一个巨大的投影仪。 Either way, the physical screen/display size is entirely independent of the screen resolution. 无论哪种方式,物理屏幕/显示器的尺寸都完全与屏幕分辨率无关。 The final physical size of the output is unknown by the computer and especially Javascript/CSS. 输出的最终物理大小由计算机(尤其是Javascript / CSS)未知。

In other words: you cannot detect "10 inches". 换句话说:您无法检测到“ 10英寸”。

You can't detect the user's screen size. 您无法检测到用户的屏幕尺寸。 The only thing that might be available to you (and can be easily spoofed) is the display resolution in pixels. 可能唯一可用的(并且很容易被欺骗)的是以像素为单位的显示分辨率。

To apply a stylesheet only if your content has more than XYZ px room, I recommend use of width media queries . 要仅在您的内容空间超过XYZ px时才应用样式表,我建议使用width媒体查询

You could do it with CSS Media Queries, hide all the content of your site and display a message to blocked people. 您可以使用CSS Media Queries做到这一点,隐藏网站的所有内容并向被屏蔽的人显示一条消息。

@media only screen 
and (max-width : YOURWIDTHpx) {
/* Styles */
 YOUR MESSAGE CONTAINER
}

Though it would make sense to just show something useful to these people rather than block them all together. 尽管只向这些人展示有用的东西,而不是将他们封锁在一起是有意义的。

Create an element with its .style.width defined in inches, then measure it's .width (in pixels) to figure out the screen's DPI. 创建一个元素,其.style.width以英寸为.style.width定义,然后测量其.width (以像素为单位)以计算屏幕的DPI。 Then you just need to use the screen object to figure out the screen's diagonal size and block users however you want. 然后,您只需要使用screen对象来找出屏幕的对角线大小,并根据需要阻止用户。

var el = document.createElement("div");
el.style.width = "100in";
document.body.appendChild(el);

var dpi = el.clientWidth / 100;
var screenSizeIn = Math.sqrt(screen.width * screen.width +
                             screen.height * screen.height) / dpi;

console.log("Screen size is " + screenSizeIn + " inches.");

document.body.removeChild(el);

if (screenSizeIn < 10) {
  window.location = "/blocked.html";
}

If the device doesn't know how big an inch actually is (and many don't), you're out of luck. 如果设备不知道实际英寸有多大(很多英寸不知道),那么您就不走运了。

For checking on resolution. 用于检查分辨率。

In javascript: 在javascript中:

if(screen.height < 600 || screen.width < 800)
{
    // Do something to block. i.e. redirect to specific page
}

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

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