Is it possible to not load an iframe in a hidden div, until the div is displayed?

两盒软妹~` 提交于 2019-11-30 01:57:43

Rather than loading the iframe with its src attribute, load it instead with a data-src attribute. For its value, provide the eventual location you'd use when the parent div becomes visible.

<div class="hidden_element">
    <iframe data-src="http://msdn.microsoft.com"></iframe>
</div>

When you show the parent div, issue a callback that takes the iframe attribute data-src, and sets its value to the src attribute of the iframe, causing it to load.

// Show our element, then call our callback
$(".hidden_element").show(function(){
    // Find the iframes within our newly-visible element
    $(this).find("iframe").prop("src", function(){
        // Set their src attribute to the value of data-src
        return $(this).data("src");
    });
});

Demo: http://jsfiddle.net/bLUkk/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!