I can successfully add a class on mouseover with Vue. But I want to remove the class when the mouse leaves the element. What is the idiomatic way of handling this in Vue?
<
A more scalable solution would be to use a directive:
// Directives
Vue.directive('add-class-hover', {
bind(el, binding, vnode) {
const { value="" } = binding;
el.addEventListener('mouseenter',()=> {
el.classList.add(value)
});
el.addEventListener('mouseleave',()=> {
el.classList.remove(value)
});
},
unbind(el, binding, vnode) {
el.removeEventListener('mouseenter');
el.removeEventListener('mouseleave')
}
})
new Vue({
el: "#app"
})
.hoverClass {
color: red;
font-weight: 700;
}
Text