【问题标题】:How can I implement mouse over on a <ul> in VueJS?如何在 VueJS 中的 <ul> 上实现鼠标悬停?
【发布时间】:2019-03-08 05:55:36
【问题描述】:

任何人都可以帮助实现 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;
}

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

谢谢,

【问题讨论】:

  • 但是你的方式很完美@girish
  • 为什么要复制所有属性?您不能设置一个全局图像类,然后在其上“选择” .image 和 .image .selected 吗?

标签: css vuejs2 vue-component vue-router


【解决方案1】:

使用:hover pseudo-class,您几乎可以在 CSS 中完全做到这一点。

.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>

【讨论】:

  • 为什么只有一次@mouseover.once?
  • @karthick 似乎OP希望在元素悬停后保留蓝色边框,即使在鼠标移动之后也是如此。每次元素悬停时都要求classList 继续添加相同的类是没有意义的,所以我使用了once 修饰符
【解决方案2】:

尝试将类绑定到数据值,例如:

 <ul :class="{'imageSelected': selected === true, 'image': selected ===
 false}" @mouseover="mouseOver" @mouseleave="mouseOut"> ... data() {
 return {   selected: false   }; }, methods: {    mouseOver:
 function(name) {    this.selected = true; },

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多