How to fire a scroll event without scrolling

My question is how can I fire (simulate or something esle) on a scroll event. In my special case, I don’t want to upload all Canned Foods to LinkedIn, reducing all Canned Foods, because there are too many of them!

I do not need a PHP or Javascript solution. Just using dev-tools on chrome is enough to get my target.

+6
source share
5 answers

You absolutely need a Javascript solution. What else do you think will listen / trigger the event?

If you want to trigger a scroll event, just scroll down to where you already typed window.scrollTo(window.scrollX, window.scrollY); in the developer console. Alternatively, you can fake one using a combination of CustomEvent and dispatchEvent .

If you want to trigger something in the scroll event, listen to the scroll using window.addEventListener("scroll", function(evt) { ... }); and do the processing function as you need.

+7
source

This does not work in Chrome or Firefox.

 window.scrollTo(window.scrollX, window.scrollY); 

This works on both:

 window.scrollTo(window.scrollX, window.scrollY - 1); window.scrollTo(window.scrollX, window.scrollY + 1); 

I do not like it, but it works.

Please note that you only need the first line of code, the other only to return scrolling back to where it was. I had to call the scroll function on the button. If you press the button twice, the scroll effect is seen, and it is not very interesting. Therefore, I prefer to add a second line of code.

+5
source

window.scrollTo(window.scrollX, window.scrollY); doesn't work for me in Firefox. Nothing happens, and it seems because you don’t really need to scroll to go to the place where you already are.

BUT window.scrollTo(window.scrollX, window.scrollY-1); works window.scrollTo(window.scrollX, window.scrollY-1); . This command fires the window.scroll event function.

(Trying to trick, for example: window.scrollTo(window.scrollX, window.scrollY-0) does not work either.)

+1
source

Sometimes you need to find the scrollElement element. In my case, my scrollElement is a body and works on mobile devices, so the code below works fine:

document.body.scrollTo(window.scrollX, window.scrollY + 1);

Hope this will be helpful.

0
source

Litle bit off off:

You do not need to use dev tools, just bookmark using "url"

  javascript:(function(){ /* Put your code here, eg the scrollng event or add an event listener to the current page or some other code*/ })(); 

when you click this bookmark, cod will be executed inside the current page environment

-1
source

All Articles