Simulate scroll event using Javascript

后端 未结 4 1526
长情又很酷
长情又很酷 2021-01-19 11:04

I am trying to Simulate a scroll event using Javascript for Mobile Safari. I am using the following code

var evt = document.createEvent("MouseEvents"         


        
4条回答
  •  广开言路
    2021-01-19 11:50

    I needed this to write a unit test , for which i need to simulate a scroll event

    function dispatchScroll(target,newScrollTop) {
          target.scrollTop = newScrollTop;
          var e = document.createEvent("UIEvents");
          // creates a scroll event that bubbles, can be cancelled,
          // and with its view and detail property initialized to window and 1,
          // respectively
          e.initUIEvent("scroll", true, true, window, 1);
          target.dispatchEvent(e);
        }
    

    For more details : https://developer.mozilla.org/en-US/docs/Web/API/event.initUIEvent

提交回复
热议问题