css - 如何在 a 上实现鼠标悬停在 VueJS 中?
问题描述
任何人都可以帮助实现 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
伪类完全做到这一点。
.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>
推荐阅读
- python - python:将时区添加到时间变量 TUC 到 (UTC+2)
- video-streaming - 什么 HEVC 编解码器标签与 fMP4、hvc1 或 hev1 一起使用?
- apache - Apache 无法访问 Tomcat:AH00957、AH00959 和 AH00896
- python - 从具有多个值的字典中获取单个键的值的键
- java - 如何使用 split() 在 java 中将段落拆分为正确的句子?
- angular - 使用 oauth2 从 Discord API 获取信息
- angular - 角多分量收音机
- r - 使用函数在 data.table 中创建可变数量的列
- node.js - firebase 服务后如何访问功能
- flutter - Flutter 版本管理。错误:-bash:fvm:找不到命令