Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue Watch for class change

I would like to listen for a class change. If the button got "fully-in-viewport" then to trigger click. $( "button.in-viewport.fully-in-viewport" ).trigger( "click" ); Found for many other options but nothing with on class change. Any suggestions, please?

like image 468
xx5ko Avatar asked Oct 12 '18 12:10

xx5ko


People also ask

How do I watch data changes on Vue instance?

Using watchers in Vue # vue file we can watch for changes in data or props by using watch . For example, the below code will watch for a change in the data element pageData , and run a function according to the value it is changed to.

How do you trigger a watch in Vue?

To trigger the Vue watch method, you should assign a new city name to the value property of the ref object.

How do I toggle a class at Vue?

We can toggle a class dynamically in vue by passing an object to the v-bind:class attribute. In this example, we are toggling a class by clicking the Toggle button.

How do you use a Vue watcher?

A watcher in Vue is a special feature that allows us to observe some data and perform specific actions when it changes. It is a more generic way to observe and react to data changes in the Vue instance.


1 Answers

You could use a MutationObserver to observe class changes, and react according to the new class value:

  1. Add a ref to the element to observe:

    <button ref="myButton">foo</button>
    
  2. Create a method to handle observed changes:

    methods: {
      onClassChange(classAttrValue) {
        const classList = classAttrValue.split(' ');
        if (classList.includes('fully-in-viewport')) {
          console.log('has fully-in-viewport');
        }
      }
    }
    
  3. Create a MutationObserver that observes changes to the class attribute of the ref element, which will call the method defined above:

    mounted() {
      this.observer = new MutationObserver(mutations => {
        for (const m of mutations) {
          const newValue = m.target.getAttribute(m.attributeName);
          this.$nextTick(() => {
            this.onClassChange(newValue, m.oldValue);
          });
        }
      });
    
      this.observer.observe(this.$refs.myButton, {
        attributes: true,
        attributeOldValue : true,
        attributeFilter: ['class'],
      });
    },
    beforeDestroy() {
      this.observer.disconnect();
    }, 
    

Vue.component('foo', {
  template: `<button ref="myButton" class="foo" @click="onClick">foo</button>`,
  mounted() {
    this.observer = new MutationObserver(mutations => {
      for (const m of mutations) {
        const newValue = m.target.getAttribute(m.attributeName);
        this.$nextTick(() => {
          this.onClassChange(newValue, m.oldValue);
        });
      }
    });

    this.observer.observe(this.$refs.myButton, {
      attributes: true,
      attributeOldValue : true,
      attributeFilter: ['class'],
    });
  },
  beforeDestroy() {
    this.observer.disconnect();
  },
  methods: {
    onClassChange(classAttrValue) {
      const classList = classAttrValue.split(' ');
      if (classList.includes('fully-in-viewport')) {
        this.$refs.myButton.click();
      }
    },
    onClick() {
      requestIdleCallback(() => {
        alert('foo clicked');
      });
    }
  }
});

new Vue({
  el: '#app',
  data: () => ({
    active: false
  }),
})
.foo {
  margin: 20px;
}
<script src="https://unpkg.com/[email protected]"></script>

<div id="app">
  <div>
    <label>
      <input type="checkbox" @change="active = !active">
      <code>.fully-in-viewport</code> class
    </label>
  </div>
  <foo :class="{'fully-in-viewport': active}"></foo>
</div>
like image 149
tony19 Avatar answered Sep 30 '22 14:09

tony19