javascript 如何预加载 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4891029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:15:02  来源:igfitidea点击:

How to preload iframe

javascriptjqueryiframejquery-animatepreload

提问by Thew

function toggle(){
    $("#tempc").toggle(
        function () {
            $("#tempc").animate({width: 255, height: 220}, 1000);
            $("#tempc").html("");
            $("#tempc").css("background-color", "transparent");
            $("#tempc").html("<iframe src='/src/stream.php?stream=1' width='255' height='225' frameborder='0' scrolling='no'></iframe><br><a href='javascript:;' onclick='hidegadget();' class='yellowblock'>Sluit</a>");
        },
        function () {
            $("#tempc").animate({width: 50, height:50}, 1000);
            $("#tempc").html("");
            $("#tempc").css("background-color", "#FFFF00");

            $.get('src/stream.php?stream=2', function(data002) {
                $('#tempc').html(data002);
            });
        }
    );
}

$.get('src/stream.php?stream=2', function(data002) {
    $('#tempc').html(data002);
});

A while ago I was trying to animate a div, well, it works now only one thing. When the first function is activated (starting row 3), and iframe gets loaded. But now, how can I preload that iframe? Because when the animation is finished the iframe isn't loaded.

前段时间我试图为一个 div 设置动画,好吧,它现在只有一件事。当第一个函数被激活时(从第 3 行开始),并且 iframe 被加载。但是现在,我如何预加载该 iframe?因为当动画完成时 iframe 没有加载。

回答by js1568

With JQuery, you don't need to use iframes. Take a look at the .load() function. http://api.jquery.com/load

使用 JQuery,您不需要使用 iframe。看看 .load() 函数。http://api.jquery.com/load

i.e.

IE

$('#tempc').load('src/stream.php?stream=1');

To preload a page, just create a hidden div and show it when ready.

要预加载页面,只需创建一个隐藏的 div 并在准备好时显示它。

var stream2 = $('<div>').load('src/stream.php?stream=2').hide();
$('#tempc').html(stream2.html());

回答by Martin Jespersen

Start by loading the iframe, hook into onLoadon the iframe (this might give you a headache in itself, but a little googling should show you how to make this work well cross browser) and run your animation from this event.

首先加载 iframe,连接到onLoadiframe(这本身可能会让您头疼,但稍微谷歌搜索应该会向您展示如何使其跨浏览器工作良好)并从此事件运行您的动画。