简体   繁体   English

我的网站在显示正确内容之前会在桌面上显示移动视图一秒钟。 如何防止这样做?

[英]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-desktopdisplaynone 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.

相关问题 如何防止我的导航菜单在移动视图中覆盖我的网站内容? - How to prevent my nav menu from covering my website content in mobile view? 如何在我的网站的移动视图中添加汉堡包 - How can I add hamburger to my website's mobile view TinyMCE:保存内容时我的HTML会发生变化。 我怎样才能保留我的HTML? - TinyMCE: My HTML changes when saving the content. How can i keep my HTML? 如何防止桌面版网站上的图片加载到移动设备上? - How can I prevent images that are on my desktop site from loading on mobile? 为什么我的 DIV 没有显示在移动浏览器中,而是显示在桌面浏览器中? - Why my DIV is not showing up in mobile browser but shows on desktop browsers? 如何更改数字时钟的位置,使其位于网站的右上角? - How can I change the digital clock position so it will be at the top right corner of my website? 如何通过我的 HTML 或 JavaScript 代码在移动设备上强制桌面视图 - How can I Force desktop view on a mobile device from my HTML or JavaScript code 我该如何配置我的 <li> 角布线的项目,以便在新视图中显示相关详细信息? - How can I configure my <li> items with Angular Routing so that it shows the relevant details in a new view? 如何将我的“桌面视图”html文件与我的“移动视图”jQuery移动文件相结合? - How do I combine my “desktop view” html file with my “mobile view” jQuery mobile file? 为什么我的LastPass浏览器扩展程序至少应该为此页面添加autocomplete = off,并且如何防止这样做? - Why is autocomplete=off being supposedly added by my LastPass browser extension for this one page at least, and how can I prevent it from doing so?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM