admin管理员组文章数量:1289623
I'm looking to get an event to fire when one scrolls "up" from $(window).scrollTop == 0
.
If you have the following code:
$(window).scroll(function(){
console.log("scrolling")
});
On a page where the document < window height then that event never fires because $(window).scrollTop
isn't changing, but this doesn't mean that there's no mouse scroll input. I want an event to fire on mouse scroll regardless if the page is moving or not.
I'm looking to get an event to fire when one scrolls "up" from $(window).scrollTop == 0
.
If you have the following code:
$(window).scroll(function(){
console.log("scrolling")
});
On a page where the document < window height then that event never fires because $(window).scrollTop
isn't changing, but this doesn't mean that there's no mouse scroll input. I want an event to fire on mouse scroll regardless if the page is moving or not.
-
1
$(window).scrollTop == 0
??? jQuery's scrollTop function will never=== 0
, unless it's been shadowed or overwritten. Did you mean.scrollTop() === 0
? – user1106925 Commented May 3, 2013 at 16:10 -
...maybe when you get to
0
, have the JS set it back to1px
. Could be it'll be a small enough change that it won't be too much of a visual distraction. jsfiddle/TKpsR – user1106925 Commented May 3, 2013 at 16:20
2 Answers
Reset to default 11Seems like what you are looking for:
http://jsfiddle/n8eVQ/
$(document).on('mousewheel DOMMouseScroll MozMousePixelScroll', function(event, delta) {
console.log('mousewheel');
//you could trigger window scroll handler
$(window).triggerHandler('scroll');
});
Other way is to capture scroll
event on modern browsers which support event capturing phase (IE>8). This can be used for any dynamic element. As jQuery doesn't implement capturing phase, you have to use javascript addEventListener()
method. Here an example implementing logic to get scrolling direction for a textarea:
document.addEventListener('scroll', function (event) {
var $elm = $(event.target);
if ($elm.is('textarea')) { // or any other filtering condition
// do some stuff
var direction = $elm.scrollTop() > ($elm.data('scrollTop') || 0) ? "down" : "up";
$elm.data('scrollTop', $elm.scrollTop());
console.log('scrolling', direction);
}
}, true);
-DEMO-
document.addEventListener('DOMMouseScroll', callbackFunction, false);
Solution for firefox; for other browsers see @roasted solution
本文标签: javascriptScroll event doesn39t fire unless page movesStack Overflow
版权声明:本文标题:javascript - Scroll event doesn't fire unless page moves - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741456833a2379805.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论