Gir*_* BN 5 css vue-router vue-component vuejs2
任何人都可以帮助实现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>
Run Code Online (Sandbox Code Playgroud)
脚本
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";
}
}
Run Code Online (Sandbox Code Playgroud)
样式:
.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;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这个?
谢谢,
:hover您几乎可以在 CSS 中使用伪类完全做到这一点。
.image {
/* your CSS for the image class */
}
.image.hovered, .image:hover {
border-left-color: blue;
}
.image:hover {
background-color: #575a51;
}
Run Code Online (Sandbox Code Playgroud)
您的模板只需要
<ul class="image" @mouseover.once="$event.target.classList.add('hovered')">
Run Code Online (Sandbox Code Playgroud)
当第一次将鼠标悬停在元素上时,这会将该类添加hovered到元素中,从而在背景颜色返回到其默认值时保持蓝色边框颜色。
new Vue({el: '#app'})Run Code Online (Sandbox Code Playgroud)
.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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
524 次 |
| 最近记录: |