Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent scroll event triggered when content push at top

Tags:

jquery

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/

like image 599
Su Beng Keong Avatar asked Apr 17 '15 07:04

Su Beng Keong


People also ask

How do I stop parent scrolling element?

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.

How do you stop scroll event propagation?

The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.

How do I stop a Web page from scrolling to the top when a link is clicked?

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.


1 Answers

.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.

like image 106
Vikram Avatar answered Sep 20 '22 02:09

Vikram