Javascript 动态更改iframe高度
我正在尝试用iframe的内容来缩放iframe,使其随浏览器窗口大小的变化而动态变化。我有以下缩放高度的代码,但我的问题是我在Javascript 动态更改iframe高度,javascript,jquery,html,css,iframe,Javascript,Jquery,Html,Css,Iframe,我正在尝试用iframe的内容来缩放iframe,使其随浏览器窗口大小的变化而动态变化。我有以下缩放高度的代码,但我的问题是我在窗口外部定义的变量。resize()函数在函数内部没有值。我错过什么了吗?它们不应该是全球性的吗 var windowHeight = $(window).height(); var iframeHeight = $('iframe').height(); var heightScale = windowHeight/iframeHeight; //d
窗口外部定义的变量。resize()
函数在函数内部没有值。我错过什么了吗?它们不应该是全球性的吗
var windowHeight = $(window).height();
var iframeHeight = $('iframe').height();
var heightScale = windowHeight/iframeHeight;
//define resize function
$(window).resize( function() {
/*test*/ console.log("I have triggered");
//define dimensions for window and iframe
var newWindowHeight = $(window).height();
var newIframeSize = null;
var hScale = 1.01;
var sScale = .09;
//window size getting bigger
/*test*/ console.log("windowHeight is " + windowHeight);
/*test*/ console.log("newWindowHeight is " + newWindowHeight);
if ( windowHeight < newWindowHeight )
{
console.log("i'm in the first if")
windowHeight = newWindowHeight;
newIframeSize = iframeHeight*heightScale;
/*test*/ console.log("iframe"+iframeHeight);
/*test*/ console.log("heightScale"+heightScale);
/*test*/ console.log("newiframe"+newIframeSize);
while ( newIframeSize < iframeHeight )
{
console.log("While loop in progress");
$('iframe').css('transform' , 'scale(' + hScale + ')');
newIframeSize = $('iframe').height();
}
}
//window size getting smaller
else if ( windowHeight > newWindowHeight )
{
console.log("i'm in the first if")
windowHeight = newWindowHeight;
newIframeSize = iframeHeight/heightScale;
/*test*/ console.log("hey"+iframeHeight);console.log("hi"+heightScale);console.log("ho"+newIframeSize);
while ( newIframeSize > iframeHeight ) //can use newIframe < iframeHeight
{
console.log("While loop in progress");
$('iframe').css('transform' , 'scale(' + sScale + ')');
newIframeSize = $('iframe').height();
}
}
var windowHeight=$(window.height();
变量iframehight=$('iframe').height();
var heightScale=窗高/iframeHeight;
//定义调整大小功能
$(窗口)。调整大小(函数(){
/*test*/console.log(“我已触发”);
//定义窗口和iframe的尺寸
var newWindowHeight=$(window.height();
var newIframeSize=null;
var hScale=1.01;
var-sScale=.09;
//窗口越来越大
/*test*/console.log(“windowHeight为”+windowHeight);
/*test*/console.log(“newWindowHeight为”+newWindowHeight);
if(窗高<新窗高)
{
log(“我在第一个if中”)
windowHeight=新的windowHeight;
newIframeSize=iframeHeight*高度刻度;
/*test*/console.log(“iframe”+iframehight);
/*测试*/控制台日志(“高度刻度”+高度刻度);
/*test*/console.log(“newiframe”+newIframeSize);
while(newIframeSize新建窗口高度)
{
log(“我在第一个if中”)
windowHeight=新的windowHeight;
newIframeSize=iframeHeight/heightScale;
/*test*/console.log(“hey”+iframehight);console.log(“hi”+heightScale);console.log(“ho”+newIframeSize);
while(newIframeSize>iframehight)//可以使用newIframe
感谢您的帮助。谢谢。没有理由您的变量不可用。您可能需要创建一个演示,以便我们可以看到发生了什么。您是否尝试过查看?您需要调整事件,调整大小功能将对其作出反应。@Patryk这对我不起作用。我需要iframe根据眉毛进行更改ser,然后缩放iframe中的内容。通常,您可以设置iframe的大小、宽度和高度。如果其中的内容较大,则滚动条必须足够。@nulldev是的,不幸的是,我需要iframe中的内容在不滚动的情况下可见