简体   繁体   English

如何在HTML iFrame中检测滚动条的存在(使用Javascript)?

[英]How can I detect a Scrollbar presence ( using Javascript ) in HTML iFrame?

How can I detect a Scrollbar presence ( using Javascript ) in HTML iFrame ? 如何在HTML iFrame中检测滚动条的存在(使用Javascript)?

I have already tried : 我已经尝试过了:

        var vHeight = 0;
        if (document.all) {
          if (document.documentElement) {
            vHeight = document.documentElement.clientHeight;
          } else {
            vHeight = document.body.clientHeight
          }
    } else {
      vHeight = window.innerHeight;
    }

    if (document.body.offsetHeight > vHeight) {
      //when theres a scrollbar
    }else{
      //when theres not a scrollbar
    }

And I also had tried : 我也尝试过:

           this.scrollLeft=1;
    if (this.scrollLeft>0) {
        //when theres a scrollbar
        this.scrollLeft=0;
        }else{
        //when theres not a scrollbar
        return false;
    }

With no success.. 没有成功..

I have searched the javascript objets on DOM Inspector , but didn't find anything. 我在DOM Inspector上搜索了javascript objets ,但没有找到任何内容。

Is is possible to detect a scrollbar presence in a iframe in javacscript ? 是否可以在javacscript中检测iframe中的滚动条存在?


The iframe content comes from the same domain. iframe内容来自同一个域。

No success until now.. 直到现在都没有成功..

alt text http://www.upvtp.com.br/file.php/1/help_key.jpg alt text http://www.upvtp.com.br/file.php/1/help_key.jpg

var root= document.compatMode=='BackCompat'? document.body : document.documentElement;
var isVerticalScrollbar= root.scrollHeight>root.clientHeight;
var isHorizontalScrollbar= root.scrollWidth>root.clientWidth;

This detects whether there is a need for a scrollbar. 这会检测是否需要滚动条。 For the default of iframes this is the same as whether there is a scrollbar, but if scrollbars are forced on or off (using the 'scrolling="yes"/"no"' attribute in the parent document, or CSS 'overflow: scroll/hidden' in the iframe document) then this may differ. 对于I帧的默认,这是一样的是否滚动条,但如果滚动条被强制开启或关闭(使用父文档中的“SCROLLING =‘是’/‘否’”属性或CSS“溢出:滚动/隐藏'在iframe文档中)然后这可能会有所不同。

Using jQuery you can compare the document height, the scrollTop position and the viewport height, which might get you the answer you require. 使用jQuery,您可以比较文档高度,scrollTop位置和视口高度,这可能会为您提供所需的答案。

Something along the lines of: 有点像:

$(window).scroll(function(){
  if(isMyStuffScrolling()){
    //There is a scroll bar here!
  }
}); 

function isMyStuffScrolling() {
  var docHeight = $(document).height();
  var scroll    = $(window).height() + $(window).scrollTop();
  return (docHeight == scroll);
} 
$(window).scroll(function(){
  if(isMyStuffScrolling()){
//scrolling
  }else{
//not scrolling
}
}); 

function isMyStuffScrolling() {
  var docHeight = $(document).height();
  var scroll    = $(window).height() ;//+ $(window).scrollTop();
  if(docHeight > scroll) return true;
  else return false;
}

improved-changed a bit from Jon`s Winstanley code 改进 - 改变了Jon的Winstanley代码

I do not think this can be done if the iframe content comes from another domain due to JavaScript security limitations. 如果由于JavaScript安全限制而导致iframe内容来自其他域,我认为不能这样做。

EDIT: In that case, something along the lines of giving the iframe a name='someframe' and id='someframe2' and then comparing frames['someframe'].document.body.offsetWidth with document.getElementById('someframe2').offsetWidth should give you the answer. 编辑:在这种情况下,给iframe一个名称='someframe'和id ='someframe2',然后比较帧['someframe']。document.body.offsetWidth与document.getElementById('someframe2') .offsetWidth应该给你答案。

I've found this works on any element, at least on Chrome: 我发现这适用于任何元素,至少在Chrome上:

hasVerticalScrollbar = (element.scrollHeight > element.offsetHeight)
        || 
(element.scrollHeight > element.clientHeight

Horizontal scrollbars can be detected the same, using Width instead of Height . 可以使用“ Width而不是“ Height来检测水平滚动条。

I think your second attempt is on the right track. 我认为你的第二次尝试是在正确的轨道上。 Except instead of this , you should try scrolling/checking document.body . 除了this ,你应该尝试滚动/检查document.body

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

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