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?
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.
To trigger the Vue watch method, you should assign a new city name to the value property of the ref object.
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.
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.
You could use a MutationObserver
to observe class changes, and react according to the new class value:
Add a ref
to the element to observe:
<button ref="myButton">foo</button>
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');
}
}
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With