make-scroll.ts 477 B

12345678910111213141516171819202122
  1. import sleep from './sleep'
  2. const makeScroll = (
  3. dom: Element,
  4. name: 'scrollTop' | 'scrollLeft',
  5. offset: number
  6. ) => {
  7. const eventTarget = dom === document.documentElement ? window : dom
  8. dom[name] = offset
  9. const evt = new CustomEvent('scroll', {
  10. detail: {
  11. target: {
  12. [name]: offset,
  13. },
  14. },
  15. })
  16. eventTarget.dispatchEvent(evt)
  17. // must use setTimeout instead of nextTick to wait dom change
  18. return sleep()
  19. }
  20. export default makeScroll