반응형
Vue Watch의 클래스 변경
수업 변경을 듣고 싶습니다.풀인뷰포트 $( "button.in-viewport.fully-in-viewport" ).trigger( "click" );
다른 많은 옵션에서 찾을 수 있지만 클래스 변경에 대해서는 찾을 수 없습니다.안해주 주실? ???
를 사용하면 클래스 변경을 감시하고 새로운 클래스 값에 따라 대응할 수 있습니다.
ref
"이것들"은 다음과 같습니다.<button ref="myButton">foo</button>
관찰된 변경을 처리하는 메서드를 만듭니다.
methods: { onClassChange(classAttrValue) { const classList = classAttrValue.split(' '); if (classList.includes('fully-in-viewport')) { console.log('has fully-in-viewport'); } } }
작성하다
MutationObserver
할 수 있습니다.class
「」의ref
위에서 정의한 메서드를 호출합니다.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/vue@2.5.17"></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>
언급URL : https://stackoverflow.com/questions/52779932/vue-watch-for-class-change
반응형
'IT이야기' 카테고리의 다른 글
1개의 클래스 vue.js 2에 2개의 조건을 추가하려면 어떻게 해야 하나요? (0) | 2022.07.23 |
---|---|
Java에서 사용되지 않는 메서드 또는 클래스를 사용하는 것이 잘못되었습니까? (0) | 2022.07.23 |
하위 항목 vueJ 간에 "활성" 클래스를 전환합니다.2 (0) | 2022.07.23 |
java 디렉토리에 파일을 작성하려면 어떻게 해야 합니까? (0) | 2022.07.23 |
매크로를 정의할 때 do while(0)이 무슨 소용이 있습니까? (0) | 2022.07.23 |