繁体   English   中英

使用srcdoc =时不会触发iframe滚动事件

[英]iframe scroll event not fired when using srcdoc=

我正在使用iframe显示html文档。 我需要在iframe中检测滚动事件。 使用src =可以正常工作,但是srcdoc =不会触发滚动事件。

HTML:

<iframe id="TermsConditionsText" srcdoc="<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.1//EN&quot; &quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd&quot;><html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;><head><meta http-equiv=&quot;Content-Type&quot; content=&quot;application/xhtml+xml; charset=utf-8&quot; /><title></title></head><body><div class=&quot;Section0&quot;> ... </div></body></html>" class="form-control" style="overflow-y: scroll; height: 600px;"></iframe>

jQuery的:

jQuery(document).ready(function () {
    var termsWereScrolled = false;

    $("#TermsConditionsText").scroll(function () {
        if (termsWereScrolled) return true;

        if ($(this).scrollTop() + $(this).innerHeight() + 2 >= $(this)[0].scrollHeight) {
            termsWereScrolled = true;
        }
    });
});

使用srcdoc =时,iframe是否触发滚动事件?

使用srcdoc =时,iframe是否触发滚动事件?

是的,使用srcdoc时会触发滚动事件。

iframe是它是一个新窗口,并且具有自己的文档。 因此,您必须将事件附加到其文档而不是元素本身。 尽管如此,您要做的就是查询iframe的内容,例如$("#TermsConditionsText").contents()来附加事件。 看一下这个:

var iframeContents = $("#TermsConditionsText").contents();

$(iframeContents).scroll(function () {
    console.log('scroll');

    var html = $(this)[0].scrollingElement;
    console.log('scrollHeight', html.scrollHeight);
});

暂无
暂无

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

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