Dynamically add and remove classes on mouseover - Vue.js

前端 未结 2 2095
走了就别回头了
走了就别回头了 2021-02-20 15:20

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?

<
2条回答
  •  广开言路
    2021-02-20 15:34

    Listen for both mouseover and mouseout and set the class based on that.

    console.clear()
    
    new Vue({
      el: "#app",
      data:{
        isHovering: false
      }
    })
    .hovering{
      color: red
    }
    
    

    {{ isHovering ? "Woot! Hovered" : "Hover over me" }}

    Or just use CSS.

    console.clear()
    
    new Vue({
      el: "#app",
      data:{
        isHovering: false
      }
    })
    h1:hover{
      color: red
    }
    
    

    {{ isHovering ? "Woot! Hovered" : "Hover over me" }}

提交回复
热议问题