[英]My website shows the mobile view in the Desktop for a fraction of a second before showing the right content. How can I prevent it form doing so?
My website - https://wilfredopersonal.herokuapp.com/# - shows some specific content for mobile view. 我的网站https://wilfredopersonal.herokuapp.com/#-显示了一些用于移动视图的特定内容。 The problem is that this content is also shown in Desktop while the Desktop content is loading.
问题在于,在加载桌面内容时,该内容也会显示在桌面中。 How can I prevent it from doing so?
如何防止它这样做?
<script>
function isMobile() {
if (navigator.userAgent.match(/Mobi/)) {
return true;
}
if ("screen" in window && window.screen.width < 1366) {
return true;
}
var connection =
navigator.connection ||
navigator.mozConnection ||
navigator.webkitConnection;
if (connection && connection.type === "cellular") {
return true;
}
return false;
}
</script>
<script>
if (!isMobile()) {
document.getElementById("not-desktop").style.display = "none";
document.getElementById("container").style.display = "unset";
} else {
document.getElementById("not-desktop").style.display = "unset";
document.getElementById("container").style.display = "none";
}
</script>
Because your script is executed after HTML loaded. 因为您的脚本是在HTML加载后执行的。 So before the browser read to your script, the mobile keep visible.
因此,在浏览器读取您的脚本之前,移动设备保持可见状态。
I recommend you to use CSS media query to solve this rather then using script. 我建议您使用CSS媒体查询来解决此问题,而不要使用脚本。 Here is a good answer demonstrate how to use
media query
to target desktop and mobile . 这是一个很好的答案,演示了如何使用
media query
来定位台式机和移动设备 。 This answer could solve your problem. 这个答案可以解决您的问题。
Another way is set #not-desktop
's display
to none
in your CSS. 另一种方法是在CSS中将
#not-desktop
的display
为none
。 Then when the script executed, if it is shows on mobile, your code will show it. 然后,执行脚本后,如果脚本显示在移动设备上,则您的代码将显示该脚本。 But this method is not flexible.
但是这种方法不灵活。
This is an issue where you are displaying both your mobile view and your desktop view at the same time, then disabling which ever "view" is incorrect. 这是一个问题,您同时显示移动视图和桌面视图,然后禁用哪个“视图”都不正确。 Since your javascript is loaded after the page is created, it will display both views until the javascript loads and disables one.
由于您的javascript是在创建页面后加载的,因此它将显示两个视图,直到javascript加载并禁用一个视图为止。
You can fix this by making both of them "disabled" from the start - add the style attribute like this: style="display:none"
to "not-desktop" and "container". 您可以通过从一开始就将它们
style="display:none"
设为“禁用”来解决此问题-将样式属性添加如下: style="display:none"
到“ not-desktop”和“ container”。 That way both of them will be disabled until the javascript can enable one. 这样,在javascript启用前,它们都会被禁用。
EDIT: after looking at Li Jinyao's answer, I see that there is a much faster way to do this - use a CSS media tag to check the width of the element, and only display it if it matches the requirements. 编辑:查看李金耀的答案后,我发现有一种更快的方法-使用CSS媒体标签检查元素的宽度,并仅在符合要求时才显示它。 Afterwards, use java script to check the userAgent and anything else, and change the displayed element accordingly.
然后,使用Java脚本检查userAgent和其他内容,并相应地更改显示的元素。 To see information about CSS media tags, look at Li Jinyao's answer.
要查看有关CSS媒体标签的信息,请查看Li Jinyao的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.