Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Jquery Scroll event is not firing

Writer Matthew Barrera

I am trying to trigger an scroll event using class element. I tried multiple scenarios but no good.

enter image description here

Below are types of events tried...

jQuery(function($){ $('[class="ag-body-container"]').bind('scroll',function() { alert("hai"); }); });
$(document).ready(function(){ $(".ag-body-viewport-wrapper").bind('scroll',function() { alert("hai"); });
});
$(document).on('scroll', '.ag-body-container', function(){ console.log('scroll happened');
});
$(function() { $(".ag-body-container").on('scroll', function () { console.log('scroll happened'); });
});
$(".ag-body-container").bind('scroll', function() { console.log('Event worked');
}); 

Non of the above gives expected result.

3 Answers

you havent tried

$(document).ready(function(){ $(document).on('scroll', '.ag-body-container', function(){ console.log('scroll happened'); });
});

OR

 $(document).ready(function(){ $(window).on('scroll', '.ag-body-container', function(){ console.log('scroll happened'); });
});
1

try .on instead of .bind or you can use .scroll.

Because, .bind may be removed from future versions at any time. There is no reason to keep using .bind and every reason to prefer .on instead.

See jquery .bind() vs. .on()

The scroll event occurs when the user scrolls in the specified element.

The scroll event works for all scrollable elements and the window object (browser window).

if window is scrollable than the below code will fired.

 $(function() { $(window).on('scroll', function () { console.log('scroll happened'); });
});

but if you are trying to something like this.

 $(".ag-body-container").on('scroll', function () { console.log('scroll happened'); });

if the element identified by '.ag-body-container' is scrollable , then only event will be fired.

check the plunker :

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy