Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually trigger a scroll event of DOM element?

I have two independent web elements, if I scroll the content of one element, I also want the 2nd element scroll at same time. Here is an example: https://stackedit.io

I did the following code, but it is not working:

element.find('.fullscreen-mk-content-textarea').on('scroll', function(e){
    // first element will trigger this event, then manully trigger the 
    // the scroll of the 2nd element. It's my plan. 
    console.log(e); // works
    element.find('.right-side').trigger('scroll'); // doesn't work...
});

What shall I do?

like image 348
Nicolas S.Xu Avatar asked Nov 28 '22 14:11

Nicolas S.Xu


1 Answers

Vanilla Javascript

var el = document.querySelector(".right-side");

el.dispatchEvent(new Event('scroll'));
like image 89
Firanolfind Avatar answered Dec 05 '22 14:12

Firanolfind