4

To clarify my example, I want to simulate a scroll event at the center of an open window. This should affect what is reasonably the main scrolling element on a given page.

Here are some relevant pages
https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/WheelEvent
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent#Values

and here's what I've tried.

scroll_evt = new WheelEvent('wheel', {
    deltaY:    10.0,
    deltaMode: WheelEvent.DOM_DELTA_LINE,
    clientX:   content.innerWidth/2,
    clientY:   content.innerHeight/2
});

-> wheel { target: null, buttons: 0, clientX: 520, clientY: 320, layerX: 0, layerY: 0 }

However, despite no errors, dispatching the event seems not to have any effect.

window.dispatchEvent(scroll_evt);
-> true

So I'm left to wonder: Is there some critical property I'm missing in my initializer? Is window an appropriate target for the event? I'm out of ideas.

Update

This works.
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMWindowUtils#sendWheelEvent%28%29.

var windowutils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                         .getInterface (Components.interfaces.nsIDOMWindowUtils);

windowutils.sendWheelEvent(window.innerWidth / 2, window.innerHeight / 2, 0, 10, 0,
                           WheelEvent.DOM_DELTA_LINE, 0, 0, 0, 0);

I'm still curious what's the matter with dispatchEvent.

lap
  • 125
  • 2
  • 8
  • 1
    what's wrong with `window.scrollBy(0, 10)`? – deleted user Aug 17 '15 at 03:54
  • I'm sorry but I don't quite understand what you mean by "content zoned of." Do you mean the content is inside some other element and that element needs to be scrolled rather than the window? In that case you can use `elem.scrollTop += 10`. – deleted user Aug 17 '15 at 06:19

1 Answers1

1

You can get an element by position like so:

var elem = document.elementFromPoint(content.innerWidth / 2, content.innerHeight / 2);

and then apply the scroll:

elem.scrollTop += 10;
deleted user
  • 824
  • 1
  • 7
  • 11
  • This works, but there is the additional step of working through the hierarchy until I reach an element with a scrollbar. Can I assume there is a scrollbar where `elem.scrollHeight > elem.clientHeight?` (Can I generate a path _to_ an element or am I stuck working upward through `elem.parentElement?`) – lap Aug 18 '15 at 02:44
  • @lap 1) Yes. That is _the_ way to tell if an element has a scrollbar. 2) What is a "path" and what do you need it for? – deleted user Aug 18 '15 at 02:51
  • I was wondering if I could work top to bottom, as in `body` first, `elem` last. It doesn't look like there's a trivial way to do this but it's not so important. – lap Aug 18 '15 at 03:04
  • Thanks for your help, by the way. : ) – lap Aug 18 '15 at 03:04
  • @lap: I believe what you are asking about is *tree traversal*. There are several ways to iterate through the DOM. – deleted user Aug 18 '15 at 03:10