Skip to content

using scrollMonitor.recalculateLocations() after a DOM change - how to? #76

@steappe

Description

@steappe

I'm using Vue.js in a project where I have a scroll container and its child elements.

In the scroll container:

  mounted: function () {
    this.monitor = scrollMonitor.createContainer(this.$el);
  },

  updated: function () {
    this.monitor.recalculateLocations();
  }

In the child elements:

  mounted () {
    this.watcher = this.monitor.create(this.$el);
    this.inViewport = this.watcher.isInViewport;

    this.watcher.visibilityChange(() => {
      this.inViewport = this.watcher.isInViewport;
    });
  },

  beforeDestroy () {
    this.watcher.destroy();
  }

The scroll container passes the scroll monitor to its children through a Vue property.

This works perfectly fine, until an element is moved from one position in the list to another. This is causing the associated DOM element to be moved in the scroll container, and the updated Hook function is called. This Hook function invokes monitor.recalculateLocations() to have the children watchers recalculate their locations and triggers events. But this is not happening (if the element is moved from a position where it is not visible to a position where it is visible in the scroll container, no event is fired), and I would like to understand why.

Example:

  • Div for element 1 (in viewport)
  • Div for element 2 (in viewport)
  • etc.
  • Div for element n (not in viewport)

The element n is moved to top, resulting in the following changes in the DOM:

  • Div for element n (in viewport)
  • Div for element 1 (in viewport)
  • etc.
  • Div for element n-1 (not in viewport)

The Div for element n is now visible in it's parent scroll container. However, when the scrollMonitor.recalculateLocations() is called, the watcher created for element n does not trigger the visibility change callback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions