css - 如何在 VueJS 中的 <ul> 上实现鼠标悬停?

标签 css vuejs2 vue-component vue-router

谁能帮助实现 UL 的鼠标悬停功能, 我的模板中有一组使用相同类的 UL 标签,但是当我尝试实现鼠标悬停时(在鼠标悬停时更改边框颜色),所有具有该类的 UL 标签都被突出显示。 我是 VUE 的新手。

模板

<ul v-bind:class="[sbitmcls]"  @mouseover="mouseOver" @mouseleave="mouseOut">
  <img src="../assets/notification.png" alt="" height="30" width="30">
  <span> Notification  </span>
</ul>

<ul v-bind:class="[sbitmcls]"  @mouseover="mouseOver" @mouseleave="mouseOut">
  <img src="../assets/message.png" alt="" height="30" width="30">
  <span> Message  </span>
</ul>

脚本

data() {
  return {
    sbitmcls: "image",
    active: true
    };
  },
  methods: {
    onClick() {
    this.$router.push("/homepage");
  },
  mouseOver: function(name) {
    this.sbitmcls = "imageSelected"
  },
  mouseOut: function() {
    event.target.style.background = "#4a4b45";
  }
}

风格:

.image {
  display: relative;
  background-color: #4a4b45;
  color: white;
  font-family: Rubik, "Bookman", Garamond, "Times New Roman", Times, serif;
  font-size: 1.2em;
  font-style: bold;
  line-height: 2em;
  height: 5%;
  border-left: 5px solid #4a4b45;
  margin-right: 50px;
  margin: 1px 0;
  padding-left: 1em;
}

.imageSelected {
  display: relative;
  background-color: #575a51;
  color: white;
  font-family: Rubik, "Bookman", Garamond, "Times New Roman", Times, serif;
  font-size: 1.2em;
  font-style: bold;
  line-height: 2em;
  height: 5%;
  border-left: 5px solid blue;
  margin-right: 50px;
  margin: 1px 0;
  padding-left: 1em;
}

有没有更好的方法来实现这个?

谢谢,

最佳答案

你几乎完全可以在 CSS 中用 :hover pseudo-class 做到这一点.

.image {
  /* your CSS for the image class */
}

.image.hovered, .image:hover {
  border-left-color: blue;
}

.image:hover {
  background-color: #575a51;
}

您的模板只需要

<ul class="image" @mouseover.once="$event.target.classList.add('hovered')">

这会将 hovered 类添加到您的元素,当它第一次被鼠标悬停在元素上时,它会保持蓝色边框颜色,同时背景颜色返回到其默认值。

new Vue({el: '#app'})
.image {
  display: relative;
  background-color: #4a4b45;
  color: white;
  font-family: Rubik, "Bookman", Garamond, "Times New Roman", Times, serif;
  font-size: 1.2em;
  font-style: bold;
  line-height: 2em;
  height: 5%;
  border-left: 5px solid #4a4b45;
  margin-right: 50px;
  margin: 1px 0;
  padding-left: 1em;
}

.image.hovered, .image:hover {
  border-left-color: blue;
}

.image:hover {
  background-color: #575a51;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script>
<div id="app">
  <ul class="image" @mouseover.once="$event.target.classList.add('hovered')">
    <img src="https://picsum.photos/30" alt="" height="30" width="30">
    <span> Notification  </span>
  </ul>

  <ul class="image" @mouseover.once="$event.target.classList.add('hovered')">
    <img src="https://picsum.photos/30" alt="" height="30" width="30">
    <span> Message  </span>
  </ul>
</div>

关于css - 如何在 VueJS 中的 <ul> 上实现鼠标悬停?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52619633/

相关文章:

javascript - 在 vue.js 组件中操作 props

html - 为什么 100% 高度不是浏览器高度的 100%?

html - 移动设备横向模式下的键盘尺寸太大

vue.js - 删除子组件会删除该组件以及在其之后创建的所有子组件

vue.js - 在 VueJS 的字符串中使用动态 ID

javascript - 如何解决【Vue警告】: props must be strings when using array syntax?

php - 在文本区域以外的内容中显示文本区域输入

css - Chrome 开发工具 : If I map my local resources to network resources my source map links in the style inspector disappears

javascript - 带有 vue-class-component : next function does not accept callback option 的 Vue 路由器

javascript - 如何在 Vue 中保存 props 数据?