2012-09-16 46 views

回答

2

你要检查元素的scrollHeightclientHeight更大,如果该元素的scrollWidthclientWidth更大。这可以直接使用这些属性来完成,或者使用jQuery提供的帮助器方法来完成。

MDN: element.scrollHeight

If the element's content generated a vertical scrollbar, the scrollHeight value is equal to the minimum clientHeight the element would require in order to fit all the content in the viewpoint without using a vertical scrollbar. When an element's content does not generate a vertical scrollbar, then its scrollHeight property is equal to its clientHeight property. This can mean either the content is too short to require a scrollbar or that the the element has CSS style overflow value of visible (non-scrollable).

+0

请问这种方法需要的iframe的内容在同一台服务器上托管的,也可以是跨域? – Olokoo

+0

它可以是因为iframe元素在您的域中。内容不是(必然),但内容将最终定义iframe的scrollWidth和scrollHeight,否则无关紧要。 – jimp

+7

我试过这个,但它不起作用,我得到了相同的结果,它们都是元素的高度。 \t \t function checkScroll(){ \t \t \t var intElemScrollHeight = document.getElementById('websiteView')。scrollHeight; \t \t \t var intElemClientHeight = document.getElementById('websiteView')。clientHeight; \t \t \t alert(intElemScrollHeight); \t \t \t alert(intElemClientHeight); \t \t} – Olokoo

1

一般来说,你应该比较元素的scrollHeight属性/ scrollWidth和的offsetHeight/offsetWidth的三角洲。 如果积极,那么'我们得到了胜利者'。寻找在iframe滚动条时 但..,事情变得有些棘手:

var frm=document.getElementById("frm"); 
var iIsVrScrollBar = frm.contentWindow.document.documentElement.scrollHeight>frm.contentWindow.document.documentElement.offsetHeight ? 1 : 0; 
var iIsHrScrollBar = frm.contentWindow.document.documentElement.scrollWidth>frm.contentWindow.document.documentElement.offsetWidth ? 1 : 0; 
+1

您应该补充说明,只有当iframe src来自与托管它的网页相同的来源时,它才起作用。一个示例错误是:“Uncaught SecurityError:阻止了一个源于”http:// ...“的框架访问源于”https:// .....“的框架。请求访问的帧具有“http”的协议,被访问的帧具有“https”的协议。协议必须匹配。 – gae123

0

我不得不修改代码来得到它的工作。

1)IFRAME &内容都在同一网站/文件夹结构

2)的过程由“sizewindow”设置为TRUE

3策动)它片断是一个计时器功能集的一部分在上IE11测试ANOUT0.5秒

4),FF34 & 35,铬31 & 40,&歌剧12.6

5)活性ç颂大小的窗口,只比含量多,

看到http://www.users.waitrose.com/~cresby/map2.htm

if (sizewindow){ 
    if(iIsVrScrollBar==1) { 
    frm.style.height = (frm.contentWindow.document.body.offsetHeight+20); 
    iIsVrScrollBar = (frm.scrollWidth!=frm.contentWindow.document.documentElement.scrollWidth) ? 1 : 0; 
    if(iIsVrScrollBar==0) sizewindow=false; 
}else{ 
    frm.style.height = (frm.contentWindow.document.body.offsetHeight-10); 
    iIsVrScrollBar = (frm.scrollWidth!=frm.contentWindow.document.documentElement.scrollWidth) ? 1 : 0; 
    if(iIsVrScrollBar==1) {frm.style.height = (frm.contentWindow.document.body.offsetHeight+10);} 
} 
}