I can't get an event listener to fire when scrolling. It works when clicking but not scrolling.
I want it to work even when the div it's listening for is created after the listener itself because of ajax usages. Therefore I use .on
instead of .scroll
.
$('.container').on('scroll', '.appended', function(){
console.log('scrolling!');
});
I've created a JSFiddle here .
You need to attach the listener to the append element. Like this :
var nAppend = $('<div class="appended">this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br>this is appended<br></div>').scroll(function(){
console.log('scrolling!');
});
setTimeout(function(){$('.container').append(nAppend);},1000);
I updated your JSfiddle .
滚动事件不会冒泡 ,因此委托方法将行不通。
Use mousewheel
event listener instead:
$('.container').on('mousewheel', '.appended' ,function(e){
if(e.originalEvent.wheelDelta > 0) {
console.log('scrolling up !');
}
else{
console.log('scrolling down !');
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.