如何检测滚动事件在iframe中? [英] javascript-How to detect scroll event in iframe?

查看:190
本文介绍了如何检测滚动事件在iframe中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用iframe tage添加事件滚动时出现问题。一般来说,我用div标签使用滚动事件它运行良好。但是当我在iframe标签中添加滚动事件来检测用户滚动pdf页面时,它不起作用。为什么我不能访问iframe中的html元素?,我有代码检查如下:





,我尝试使用iframe添加JavaScript滚动事件: b HTML代码



 < iframe id =myframesrc =doc.pdf >< / iframe>  


$ b

document.getElementById('myframe')。onscroll = function(){alert(滚动页面');};


解决方案

JavaScript为我们提供 onscroll 作为传递参数的属性(可以是函数)。但是我们在这里获得了iframe,请尝试下面的代码片段(jQuery)。

  $(#yourFrameId)。load (){
var iframe = $(#yourFrameId)。contents();
$ b $(iframe).scroll(function(){
// your code here
});
});


I have a problem when I try add event scroll with iframe tage. generally, I use scroll event with div tag It was working well. but when I add scroll event in iframe tag to detect user scroll pdf page, It was not working. why cannot I access html elements in iframe?, I have code inspect below:

and I try to add javascript scroll event with iframe :

HTML Code:

<iframe id="myframe" src="doc.pdf"></iframe>

JavaScript Code:

document.getElementById('myframe').onscroll = function(){
	 	alert('scrolling page');
};

解决方案

JavaScript provide us onscroll as an attribute which passes parameters (Can be a function). But we got iframe here, try the following code snippet (jQuery).

$("#yourFrameId").load(function () {
     var iframe = $("#yourFrameId").contents();

    $(iframe).scroll(function () { 
        //your code here
    });
});

这篇关于如何检测滚动事件在iframe中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆