# Has User Scrolled?

**URL:** <https://discuss.codemirror.net/t/has-user-scrolled/4786>\
**Category:** v6\
**Created:** [August 1, 2022, 9:15pm UTC](https://discuss.codemirror.net/t/has-user-scrolled/4786 "2022-08-01T21:15:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shshaw](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/shshaw/32/2184_2.png) [@shshaw](https://discuss.codemirror.net/u/shshaw)\
**Post date:** [August 1, 2022, 9:15pm UTC](https://discuss.codemirror.net/t/has-user-scrolled/4786/1 "2022-08-01T21:15:46Z")

</div>

I want to scroll to the end ( `EditorView.scrollIntoView(tr.state.doc.length, { x: 'start', y: 'end' })` ) when appending to the document, but only if the User hasn’t scrolled somewhere else.

Is there a way to tell in the state/view if the User has scrolled?

Based on my understanding, it seems like maybe I’ll need to do a `domEventHandler` extension watching scroll ( `EditorView.domEventHandlers({ scroll(event, view) { ... })` ) and update a `Facet`, but I’m hoping there’s something I missed. Some kind of property in the view.

> [@Codemirror 6 sync scrolling of two EditorView's](https://discuss.codemirror.net/t/codemirror-6-sync-scrolling-of-two-editorviews/3113/4):
>
> Hi, I was trying to implement sync scrolling too, and found this thread, but here’s what I discovered for future reference: Scroll events are not handled by updateListener, but rather [domEventHandlers](https://codemirror.net/6/docs/ref/#view.EditorView%5EdomEventHandlers), for example: extensions : [ ... EditorView.domEventHandlers({ scroll(event, view) { // handle infinite loop: // early exit if mouse is not hover over current div const self = document.getElementById( ... ) if (!self.matches(":hover")) { return; } // main l…

Additionally, is there a way to know if the user is scrolled to the bottom? Not sure how to use the available properties to detect what’s truly visible in the viewport, or if I should use native JS methods on `view.scrollDOM`.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [August 2, 2022, 9:32am UTC](https://discuss.codemirror.net/t/has-user-scrolled/4786/2 "2022-08-02T09:32:07Z")

</div>

The view itself doesn’t track this. Easiest way to do it would be a view plugin that keeps a flag for this and [registers](https://codemirror.net/docs/ref/#view.PluginSpec.eventHandlers) its own scroll handler.

---

<div class="post-metadata">

**Author:** ![shshaw](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/shshaw/32/2184_2.png) [@shshaw](https://discuss.codemirror.net/u/shshaw)\
**Post date:** [August 2, 2022, 5:14pm UTC](https://discuss.codemirror.net/t/has-user-scrolled/4786/3 "2022-08-02T17:14:09Z")

</div>

Thanks, marijn. That’s the route I started going down. Works decently well.

```auto
function isElementScrolledToBottom(el, wiggleRoom = 10) {
  return el.scrollHeight - el.scrollTop <= el.clientHeight + wiggleRoom;
}

export const scrollViewPlugin = ViewPlugin.fromClass(
  class {
    scrolledToBottom = true;
    programmaticScroll = false;

    update(update) {
      if (update.docChanged) {
        for (let tr of update.transactions) {
          for (let e of tr.effects) {
            if ( e.is( myCustomEffect ) && this.scrolledToBottom) {
                // Add flag that this was a programmatic scroll.
                this.programmaticScroll = true;
                // Delay by an animation frame to ensure DOM is ready
                requestAnimationFrame(() => {
                  // Scroll to the bottom of the console.
                  const el = update.view.scrollDOM;
                  el.scrollTop = el.scrollHeight;
                });
              }
            }
          }
        }
      }
    }
  },
  {
    eventHandlers: {
      scroll: function (e, view) {
        // Ignore programmatic scroll events.
        if (this.programmaticScroll) {
          this.programmaticScroll = false;
          return;
        }
        this.scrolledToBottom = isElementScrolledToBottom(view.scrollDOM);
      }
    }
  }
);

```
