简体   繁体   English

阻止iframe在手机上加载

[英]Prevent iframe from loading on mobile

I am working on my portfolio page and I want to have my projects in a demo mode where the user can preview the sites in different viewports. 我正在“作品集”页面上工作,我想让我的项目处于演示模式,在此模式下,用户可以在不同视口中预览网站。 I got the idea from here: http://my.studiopress.com/themes/genesis/#demo-full 我从这里得到了这个主意: http : //my.studiopress.com/themes/genesis/#demo-full

On mobile devices I would like to keep the iframes from loading, and instead have links to the projects open the sites in the new tab. 在移动设备上,我希望不加载iframe,而是使用指向项目的链接在新标签中打开网站。

If I have the divs containing the iframes hidden at the very top of my CSS file with display:none, I can see the iframes still load in the background and the page takes a long time to load. 如果我的displays:none隐藏了包含iframe的div,它们位于CSS文件的顶部,则可以看到iframe仍在后台加载,并且页面需要很长时间加载。

Is there any way to keep them from loading at all when on a certain device or viewport size? 在某种设备或视口大小上,有什么办法可以阻止它们完全加载?

You could achieve this by using JavaScript and the HTML Data-Attribut. 您可以使用JavaScript和HTML数据属性来实现。 By setting the src-Attribute to something like "#" it won't load anything. 通过将src-Attribute设置为“#”之类的内容,将不会加载任何内容。 You can use the data-Attribute to store the URL for use with JavaScript. 您可以使用data-Attribute存储用于JavaScript的URL。

<iframe src="#" data-src="https://placekitten.com/200/300" frameborder="0"></iframe>

Then you just check to screen size with window.matchMedia() and set the src-attribute at a specific screen size. 然后,您只需使用window.matchMedia()检查屏幕大小,然后将src属性设置为特定的屏幕大小。

var $iframe = $('iframe'),
    src = $iframe.data('src');

if (window.matchMedia("(min-width: 480px)").matches) {
    $iframe.attr('src', src);
}

Here is a working example: https://jsfiddle.net/5LnjL3uc/ 这是一个工作示例: https : //jsfiddle.net/5LnjL3uc/

If you want to show the iframe after a user resizes the window you need to put your code into a function and bind it to a resize event: 如果要在用户调整窗口大小后显示iframe,则需要将代码放入函数并将其绑定到调整大小事件:

var $iframe = $('iframe'),
    src = $iframe.data('src');

function showIframe() {
    if (window.matchMedia("(min-width: 480px)").matches) {
        $iframe.attr('src', src);
    }
}

$(window).on('resize', showIframe);

// Initialize it once on document ready
showIframe();

Working Example: https://jsfiddle.net/5LnjL3uc/1/ 工作示例: https : //jsfiddle.net/5LnjL3uc/1/

A better solution is to approach this in reverse. 更好的解决方案是相反地进行处理。

IE do not load the src to begin with by placing your URL in a attribute like 'data-src' for example. IE不会通过将URL放在“ data-src”之类的属性中来加载src。

See my code for this below. 请参阅下面的代码。 You simply copy the data-src to your src when your device, or device width is not mobile/mobile size. 当设备或设备宽度不是移动设备/移动设备大小时,您只需将data-src复制到src。

I believe this is the best solution because there are no uncertainties. 我相信这是最好的解决方案,因为没有不确定性。 With the previously mentioned solutions (which I tried) you are racing against the clock with the browser for when your code runs and it decides to load the iframe src. 使用前面提到的解决方案(我尝试过),您将与浏览器争分夺秒地运行代码,并决定何时加载iframe src。

if(device !== true) {
  // add '#' to the href
  let iframe = document.getElementById('3dExperience')
  iframe.src = iframe.getAttribute('data-src')
}

Note: 'device' is the is-mobile npm package for detecting mobile. 注意:“设备”是用于检测移动设备的is-mobile npm软件包。

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

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