How to trigger an on scroll event without scrolling

前端 未结 8 1216
梦毁少年i
梦毁少年i 2021-02-12 03:15

my Question is, how can i trigger (simulate or something esle) an on scroll event. In my special case I don\'t want to load all the Conservations in LinkedIn by scolling down al

8条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-12 04:03

    Alternatively, you can manually trigger a real scroll event as following:

    el.dispatchEvent(new CustomEvent('scroll'))
    

    Which feels a bit less of a hack (and more performant) than dual scrolling by +1 and -1 pixels...

    This should run any piece of code listening for a scroll event.

    Edit: To support IE11 or other legacy browser, consider using a CustomEvent polyfill such as mdn-polyfills

提交回复
热议问题