简体   繁体   English

如何根据对象或iframe的内容长度动态更改其长度

[英]How to dynamically change the length of an object or iframe based on the length of its content

I'm working on a project where Web pages are composed of different fragments which are dynamically loaded with different types of objects. 我正在一个项目中,Web页面由不同的片段组成,这些片段动态地加载了不同类型的对象。 So for example, at a certain point a page might have 2 fragments, one that shows a video and another that shows some text, and at another time that same page might show 2 blocks of text. 因此,例如,某个页面上可能有2个片段,一个片段显示一个视频,另一个片段显示一些文本,而另一时间同一页面上可能显示2个文本块。 Changes in content depend on the user viewing it and because of that the same fragment (defined by and object tag or an iframe) might contain videos (mp4, avi, etc), or text (pdf, docx, txt, etc), or images, etc. 内容的更改取决于用户查看内容,因此,相同的片段(由和对象标记或iframe定义)可能包含视频(mp4,avi等)或文本(pdf,docx,txt等),或者图片等

The problem I'm having is related to the content's size (specifically the length). 我遇到的问题与内容的大小(特别是长度)有关。 Both the object and iframe tags are delimited by a bounding box which I need to resize according to the content's length. 对象标记和iframe标记都由一个边界框定界,我需要根据内容的长度调整其大小。 If the content is a 30 page long PDF, I need all 30 pages to be rendered in the Web page. 如果内容是30页长的PDF,那么我需要在Web页中呈现所有30页。 I can't have the user scroll down to view the PDF's content, I need it to be a part of the Web page as seamlessly as possible. 我不能让用户向下滚动来查看PDF的内容,我需要它尽可能无缝地成为Web页面的一部分。

The answers found here seem to provide a possible solution. 此处找到的答案似乎提供了可能的解决方案。 I don't know JQuery but I can learn. 我不了解JQuery,但我可以学习。 However, I noticed the content used in those examples is always a Web page. 但是,我注意到这些示例中使用的内容始终是网页。 In my case, the iframe's content can be of many different types and I'd like to know if JQuery can be used to determine the size of something that's not a web page. 就我而言,iframe的内容可以是许多不同的类型,我想知道是否可以使用JQuery来确定不是网页的内容的大小。 If it can't, can anyone please suggest a solution to my problem? 如果不能,请问有人可以提出解决我问题的方法吗? Thanks. 谢谢。

you are going to have to use jquery or javascript to dynamically set the height of the iframe , 您将必须使用jquery或javascript动态设置iframe的高度,

these should work 这些应该工作

// For other good browsers.
$('iframe').load(function() {
  this.style.height =
  this.contentWindow.document.body.offsetHeight + 'px';
});

// 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;
}

there is a whole article Here That explains this code 有一整篇文章在这里这也解释了这段代码

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

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