Adjust width and height for iframe depending on screen size

只谈情不闲聊 提交于 2019-12-13 04:20:43

问题


I have:

$("#fancybox-manual-b").click(function() {
    $.fancybox.open({
    href : 'iframe.html',
    type : 'iframe',
    padding : 5
    });
});

<li><a class="fancybox fancybox.iframe" href="iframe.html">Iframe</a></li>

however I would like to Adjust size of iframe depending on user screen's size.

I was trying something like:

width = $(window).width() -80;
heigth= $(window).height() -80;

Is this correct?, how to use it in code?


回答1:


Alternatively, you can also try this..

<script type="text/javascript">
    $(document).ready(function () {
         $("a.iframe").fancybox({
            'width': 800,
            'height': 500,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'type': 'iframe'
        });
    });
</script>

ND




回答2:


Try this,

function ResizeIframe(iframe) {
    var iframeBody = (iframe.contentDocument) ? iframe.contentDocument.body : iframe.contentWindow.document.body; 
    var height = (iframeBody.scrollHeight < iframeBody.offsetHeight) ? iframeBody.scrollHeight : iframeBody.offsetHeight;
    height = height + 10;
    $(iframe).height(height);
}

<iframe id="idFrm" src="URL" frameborder="0" clientidmode="Static" scrolling="no"                            height="35px" width="98%" onload="javascript:ResizeIframe(this);"></iframe>

I have used it and works like charm in all browsers. Call it onload of your

ND



来源:https://stackoverflow.com/questions/15357047/adjust-width-and-height-for-iframe-depending-on-screen-size

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