可以使用v-bind指令绑定class,然后使用事件绑定来改变class名。例如:

<template>
  <div class="my-class" v-bind:class="{ 'new-class': isHover }" @mouseover="isHover = true" @mouseout="isHover = false">
    Mouse over me to change my class!
  </div>
</template>

<script>
export default {
  data() {
    return {
      isHover: false
    }
  }
}
</script>

<style>
.my-class {
  background-color: yellow;
  padding: 10px;
}

.new-class {
  background-color: blue;
  color: white;
}
</style>

在上面的示例中,我们使用v-bind:class指令来绑定class,然后使用isHover变量来控制是否添加新的class。@mouseover和@mouseout事件绑定用于监测鼠标移入和移出事件。当鼠标移入时,isHover变量被设置为true,并且new-class class被添加到div元素中。当鼠标移出时,isHover变量被设置为false,new-class class被移除


原文地址: https://www.cveoy.top/t/topic/g9qA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录