我要做的就是在滚动 DIV 时调用一个函数.为简单起见,我没有指定其他任何内容.此外,我只关注 DOM 兼容的浏览器,例如 Chrome、Safari(不是 IE).
All I'm trying to do is to call a function when a DIV is scrolled.For simplicity sake Im not specifying anything else. Also I am only looking at DOM compliant browsers like Chrome, Safari (not IE).
我的问题是滚动处理程序永远不会被调用.如果我将 scroll 替换为 click ,它会在我单击时起作用.不知何故,滚动不起作用.
MY problem is that the scroll handler never gets called. If I replace the scroll to click , it works when I click. Somehow the scroll is not working.
请注意:我不能使用 jQuery :(
Please note: I cannot use jQuery :(
这是我的代码:
HTML:
<div id="test">--long content--</div>
JS:
function myFunc() {
console.log('in myFunc');
}
var objTable = document.getElementById("test");
objTable.addEventListener("scroll", function () {
myFunc();
}, false);
小提琴:
http://jsfiddle.net/yymg5/7/
这是因为窗口滚动而不是div.尝试将您的元素侦听器更改为 div 的父级(在本例中为窗口),例如 this.
This is because the window is scrolling not the div. Try changing your element listener to the parent of the div (in this case the window) like this.
window.addEventListener("scroll", function () {
myFunc();
}, false);
这篇关于Javascript 滚动处理程序未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)