简体   繁体   English

如何通过单击移动设备上的简单链接来切换桌面视图?

[英]How to switch desktop view by clicking on simple link on mobile?

On my responsive website (WordPress) I have a link "View desktop version". 在响应式网站(WordPress)上,我具有“查看桌面版本”链接。

I want when I click on that link the desktop view enable and when I click on "View mobile version" it came back to mobile version. 我想要当我单击该链接时启用桌面视图,并且当我单击“查看移动版本”时,它又回到了移动版本。 Is it possible? 可能吗?

Move any common settings into a separate stylesheet. 将所有常用设置移到单独的样式表中。 You should now be left with a stylesheet that only has media queries. 现在,您应该剩下一个仅包含媒体查询的样式表。 In the link tag for the responsive stylesheet add id="dynSS" . 在自适应样式表的链接标记中,添加id="dynSS" Next create a separate stylesheet with only the desktop styles, don't add a link for this stylesheet. 接下来,创建一个仅包含桌面样式的单独样式表,不要为此样式表添加链接。

Add this link somewhere in your document. 将此链接添加到文档中的某处。

<a href="javascript:void(0)" id="toggleSS"></a>

Next, the script 接下来,脚本

Change ssSmURL to the relative url of your responsive stylesheet ssSmURL更改为响应式样式表的相对URL
Change ssLgURL to the relative url of your non-responsive stylesheet ssLgURL更改为非响应式样式表的相对URL
Change breakPoint to the maximum width of the mobile stylesheet breakPoint更改为移动样式表的最大宽度

Basically we dynamically create a new link element with the proper attributes for the style sheet and a unique id so we are not removing common stylesheets. 基本上,我们会动态创建一个新的link元素,该元素具有样式表的适当属性和唯一的id因此我们不会删除常见的样式表。

Then we remove the current stylesheet if it exists and append the new stylesheet. 然后,如果当前样式表存在,则将其删除,然后追加新样式表。

(function() {
  "use strict";
  window.onload = function() {
    var ssSmURL = 'small.css';
    var ssLgURL = 'large.css';
    var breakPoint = 1000;
    var ssURL;
    var toggle = document.getElementById('toggleSS');

    function loadSS(url) {
      var ss = document.createElement("link");
      ss.href = url;
      ss.rel = "stylesheet";
      ss.id = "dynSS";
      var current = document.getElementById('dynSS');
      if (current) current.remove()
      document.head.appendChild(ss);
      ssURL = url;
      if (ssURL === ssLgURL) {
        toggle.textContent = 'mobile';
      } else {
        toggle.textContent = 'desktop';
      }
    }

    if(window.innerWidth <= breakPoint) {
      toggle.textContent = 'desktop';
    }

    toggle.onclick = function(e) {
      if (ssURL === ssSmURL)
        loadSS(ssLgURL);
      else
        loadSS(ssSmURL);
    };
  }
})();

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

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