How to trigger the scroll event in pure javascript without using third-party libraries.

For example, I can trigger such an event as click() on elements, and because of this, a trigger will trigger on a click on this element. Also with .focus() and .blur() events.

How to do the same (similar) to scroll .


I tried to assign the scrollTop value to the scrollTop first by +1 , and immediately by -1 , it worked, but I think this is a dirty option.

  • Describe more precisely what exactly and where you want to scroll? - Grundy
  • @Grundy nothing and nowhere, just run the scroll trigger. So that all signatories fulfill their method - Vasily Barbashev
  • And if you assign the current value without +1 and -1, wouldn't it work? - sivik_xes
  • @sivik_xes alas no. I tried. Should obviously change the value. But for example, if you call a .change() event on a field, this does not mean that the value has changed in it. - Vasily Barbashev

2 answers 2

You can use constructors to create events. In particular for the UIEvent scroll event

 var scrollEvent = new UIEvent('scroll'); 

To call the created event, use the dispatchEvent function.

 element.dispatchEvent(scrollEvent); 

An example of creating and calling events on mdn

    To call events on any elements you need to use the Event object.

    Documentation link

    Work answer:

     var scroll = document.getElementById('scrollBlock'); scroll.dispatchEvent(new Event('scroll')); 

    For IE, you need to read the specification for supporting Event calls for DOM model elements.