滚动事件监听器javascript

是否有一个js监听器,当用户滚动在某个文本框可以使用? Kinda像onclick除了滚动。我看到了HTML5 event listener for number input scroll – Chrome only,但似乎只是铬。我在寻找一些跨浏览器。
对于那些发现这个问题希望找到一个不涉及jQuery的答案,你钩住窗口“滚动”事件使用正常的事件侦听。说我们想添加滚动监听一些CSS选择器的元素:

// what should we do when scrolling occurs
var runOnScroll =  function(evt) {
  // not the most exciting thing, but a thing nonetheless
  console.log(evt.target);
};

// grab elements as array, rather than as NodeList
var elements = document.querySelectorAll("...");
elements = Array.prototype.slice.call(elements);

// and then make each element do something on scroll
elements.forEach(function(element) {
  window.addEventListener("scroll", runOnScroll);
});

对于奖励积分,你可以给滚动处理程序一个锁机制,以便它不会运行,如果我们已经滚动:

// global lock, so put this code in a closure of some sort so you're not polluting.
var locked = false;
var runOnScroll =  function(evt) {
  if(locked) return;
  locked = true;
  // ...your code goes here...
  locked = false;
};
http://stackoverflow.com/questions/12522807/scroll-event-listener-javascript

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:滚动事件监听器javascript