I noticed that jquery scroll event automatically triggered when some content push at the top of the page by ajax. Any expert know how to avoid scroll event being triggered when the content has been added? But scroll event triggered when user run the mousewheel, page up down as well as keyboard up and down probably?
$(window).on('scroll', function () {
console.log("User manual scrolled.");
});
Based on this example. https://jsfiddle.net/q00jnv4n/
You can do this with a single CSS rule! To stop the scroll at the end of an element, set on the element's CSS: overscroll-behavior: contain; This way, if the user reaches the end of the scroll of an element, it will stop there and not “scroll-chain” to the body or parent element.
The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.
To stop a web page from scrolling to the top when a link is clicked that triggers JavaScript, we call preventDefault in the link's click handler.
.off
function used to Remove an event handler.
You should be using $(window).off("scroll")
just before loading content by ajax and then again use $(window).on('scroll', function () {
after loading the content
By using .off
your scroll handler will be remove while content is being loaded on site.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With