正确调整包含未知尺寸图像的 iframe 的大小
我有一个 C# 网络浏览器,它包含一个包含多个 iframe 的 html 页面。 每个 iframe 都包含一个任意 html 文件,通常包含图像。 img 标签通常没有任何宽度或高度属性(即,通常只是 )。
我已经阅读了如何根据 iframe 的内容调整其大小,并使用 jquery 让它工作。 iframe 的 html 中存在以下 jquery 片段:
$(document).ready(function() {
$('img').each(function(){
$(this)
.bind('load readystatechange', function(e) {
var iframes = window.top.document.getElementsByName(window.name);
if (iframes.length == 1) {
window.top.DoResizeFrame(iframes[0]);
}
})
});
DoResizeFrame 在父 html 文件(Web 浏览器显示的文件)中定义:
function DoResizeFrame(fr) {
if (fr && fr.Document && fr.Document.body) {
fr.style.height = fr.Document.body.scrollHeight + 'px';
fr.style.width = fr.Document.body.scrollWidth + 'px';
}
}
我还从父文档的 $(document).ready 事件中调用 DoResizeFrame。 这非常有效 - 如果没有图像,文档的就绪事件会触发调整大小。 如果有图像,则每次图像加载完成时,iframe 都会正确调整大小。
但是,加载大图像会导致 iframe 大小不正确,直到图像完全加载为止。 由于我很少在图像标签中使用宽度和高度属性,因此我想我可以使用jquery来监听图像的readystatechange事件,并在“加载”时获取图像的大小并适当调整大小即使图像仍在加载,iframe 也会显示。 不幸的是,至少在 IE7 中,readystatechange=loading 仅在图像下载完成后发生。
有谁知道如何通过“”检测 html 中引用的图像的大小,而无需等待整个下载发生? 我试图通过正确调整所有内容的大小来提供最佳的用户体验,以便用户可以阅读 iframe 的其余部分,即使下载大图像需要一段时间。
谢谢! 吉恩
I have a C# webbrowser that holds an html page that has several iframes. Each iframe holds an arbitrary html file, often with images. The img tags often don't have any width or height attributes (ie, it's often just <img src="someimage.jpg">
).
I've read about how to size iframes based on their content, and have gotten it to work using jquery. The following jquery snippet is present in the iframe's html:
$(document).ready(function() {
$('img').each(function(){
$(this)
.bind('load readystatechange', function(e) {
var iframes = window.top.document.getElementsByName(window.name);
if (iframes.length == 1) {
window.top.DoResizeFrame(iframes[0]);
}
})
});
DoResizeFrame is defined in the parent html file (the one that the webbrowser is showing):
function DoResizeFrame(fr) {
if (fr && fr.Document && fr.Document.body) {
fr.style.height = fr.Document.body.scrollHeight + 'px';
fr.style.width = fr.Document.body.scrollWidth + 'px';
}
}
I also call DoResizeFrame from the parent document's $(document).ready event. This works great - if there are no images, the document's ready event triggers a resize. If there are images, each time an image is finished loading, the iframe is properly resized.
However, loading a large image causes the iframe to be improperly sized until the image is completely loaded. Since I rarely have the width and height attributes in the image tag to work with, I figured I could use jquery to listen to the readystatechange event of the image, and when it's "loading", pick up the size of the image and properly size the iframe even while the image is still loading. Unfortunately, at least in IE7, readystatechange=loading only happens after the image is done being downloaded.
Does anyone have any ideas how I can detect the size of an image referenced in my html simply by "<img src="someimage.jpg">
" without waiting for the entire download to happen? I'm trying to provide the best user experience by sizing everything correctly, so that the user can read the rest of the iframe even if a large image is taking a while to download.
thanks!
jean
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种可靠的确定方法是在下载图像之前询问服务器图像的大小。 我已经使用 .net 完成了此操作,这非常简单。
One dependable way to be sure is to ask the server the size of the image before downloading it. I've done this using .net, it's quite easy.