简体   繁体   English

如何在浏览器缩放上动态定位和缩放iframe

[英]How to dynamically position and scale iframe on browser zoom

The Problem 问题

A user can choose to create an iframe by pasting in their iframe code or url. 用户可以选择通过粘贴iframe代码或网址来创建iframe。 When the window re-sizes the iframe needs to stay positioned in the middle and bottom of the page. 当窗口重新调整大小时,iframe需要保持在页面的中间和底部。 I have gotten the iframe to stay in the middle and the bottom of the page for the most part. 我已经让iframe大部分都停留在页面的中间和底部。 But when the window re-sizes (a user zooms in(150% etc) or out(50% etc)) the iframe itself gets larger when a user zooms in and smaller when a user zooms out. 但是当窗口重新调整大小(用户放大(150%等)或放大(50%等))时,iframe本身在用户放大时变大,在用户缩小时变小。 Since I can't target the contents within the iframe, I have been trying to figure out a way of applying a scale to the iframe so that it would appear to stay the same size and at the same position. 由于我无法定位iframe中的内容,因此我一直在尝试找出一种将缩放应用于iframe的方法,以使其看起来保持相同的大小和相同的位置。 I am having trouble dynamically figuring out the scale of an iframe when the window re-sizes. 当窗口重新调整大小时,我无法动态计算出iframe的比例。

The Javascript Javascript

load event 加载事件

                jQuery('#iframe_wrapper iframe').attr('id','iframe');
                jQuery( "#iframe" ).load(function() {
                    jQuery('#adhesion_iframe').css('-webkit-transform', 'scale(1)');
                });

position 位置

ratio = $('#iframe').width() / ($('#iframe').height() + ($('#close_btn')close.length>0 ? 0 : 20));


 if (window.innerWidth > window.innerHeight) {
                        var width = window.innerWidth / 1.5 / 1.5;
                        $('#iframe_wrapper').css({
                            width: width + "px",
                            height: window.innerWidth / ratio / 1.5 + "px",
                            left: parseInt((window.pageXOffset + window.innerWidth / 2) - (width / 2)) + "px",
                            top: parseInt(window.innerHeight + window.pageYOffset - window.innerWidth / ratio / 1.5 / 1.5) + "px"
                        });

iframe IFRAME

if($('#iframe').length >0 ){

                    jQuery.fn.center = function(){
                        this.css("position","absolute");
                        this.css("top","60%");
                        this.css("left","50%");
                        this.css("margin-top","-"+(this.height()/2)+"px");
                        this.css("margin-left","-"+(this.width()/2)+"px");
                        return this;}

                    $('#iframe').center();

                $('iframe').css({
                    '-webkit-transform': 'scale('+(document.body.offsetWidth * scale / initial_width )
                });

                }

When the page loads the initial scale would be set to 1. I was thinking of doing something like this to dynamically change the scale on window re-size '-webkit-transform': 'scale('+(document.body.offsetWidth * scale / initial_width) . So if the zoom level goes to 150% then the scale would be something like 0.something and if the zoom level goes to 50% then the scale may be 2.something etc..... Sure I can hard code the scale but that would only work on certain iframe sizes (the iframe width and height is dynamically set by the user). So it may work on an iframe that is 300 by 50 but not on one that is 600 by 100 etc. 当页面加载时,初始比例将被设置为1.我正在考虑做这样的事情来动态地改变窗口上的比例尺寸'-webkit-transform':'scale('+(document.body.offsetWidth *) scale / initial_width) 。所以如果缩放级别达到150%那么比例就像是0.something,如果缩放级别达到50%那么比例可能是2.something等.....当然我可以对代码进行硬编码,但这只适用于某些iframe大小(iframe的宽度和高度由用户动态设置)。因此它可能适用于300乘50的iframe,但不适用于600乘100等的iframe。

I think you are overdoing it. 我觉得你做得太过分了。 Just set the size of the iframe to pixel values with css: 只需使用css将iframe的大小设置为像素值:

iframe {
   width: 400px;
   height: 300px; 
}

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

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