4

iframe http://sonspring.com/journal/jquery-iframe-sizingの自動サイズ変更にこのコードを使用します。問題なく動作しますが、検索エンジンをiframeとして使用したときに昨日1つの問題が発生しました。結果が表示されるまでに0.5〜1秒かかり、自動サイズ変更がウィンドウのサイズを正しく変更できないため、スクリプトを0.5秒遅らせる必要があると思います. どうやってやるの ?

これは iframe.js のコードです

$(document).ready(function()
{
    // Set specific variable to represent all iframe tags.
    var iFrames = document.getElementsByTagName('iframe');

    // Resize heights.
    function iResize()
    {
        // Iterate through all iframes in the page.
        for (var i = 0, j = iFrames.length; i < j; i++)
        {
            // Set inline style to equal the body height of the iframed content.
            iFrames[i].style.height = iFrames   [i].contentWindow.document.body.offsetHeight + 'px';
        }
    }

    // Check if browser is Safari or Opera.
    if ($.browser.safari || $.browser.opera)
    {
        // Start timer when loaded.
        $('iframe').load(function()
            {
                setTimeout(iResize, 0);
            }
        );

        // Safari and Opera need a kick-start.
        for (var i = 0, j = iFrames.length; i < j; i++)
        {
            var iSource = iFrames[i].src;
            iFrames[i].src = '';
            iFrames[i].src = iSource;
        }
    }
    else
    {
        // For other good browsers.
        $('iframe').load(function()
            {
                // Set inline style to equal the body height of the iframed content.
                this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
            }
        );
    }
}
);
4

2 に答える 2

0

ページの変更を検出し、iFrame のサイズ変更をトリガーするには、mutationObserver を使用する必要があります。

これを行う GitHub のこのプロジェクトをチェックアウトしてください。

https://github.com/davidjbradshaw/iframe-resizer

于 2014-03-14T23:59:12.160 に答える
0

あなたのhtmlを見ずに正確に答えるのは難しいです。setTimout(iResize, 500);0.5 秒後にどちらをチェックするかを調整できます。

または、これは、単一の iframe のサイズを変更するときに使用するものです。

body 要素が読み込まれたかどうかを確認するために、50ms ごとにチェックを続けます。

function resizeIframe(iframe){
    var doc = iframe.contentDocument ||  iframe.contentWindow.document;
    if(doc.body){
        iframe.height =0;
        iframe.height = doc.body.scrollHeight + 30;
    }
    else{
         if(delay)clearTimeout(delay)
          delay = setTimeout(function(){resize(iframe)},50);
    }
};

var delay, iframe = document.getElementById('iFrame'); 


resizeIframe(iframe);
于 2013-03-19T07:49:43.970 に答える