vue鼠标悬停显示文字
时间: 2023-08-11 07:08:39 浏览: 87
你可以使用Vue的绑定指令v-bind
或简写符号:
来绑定鼠标悬停事件mouseover
和mouseout
,然后使用CSS样式来控制显示和隐藏文字。具体实现可以参考以下代码:
<template>
<div>
<p v-bind:title="message" v-on:mouseover="show" v-on:mouseout="hide">鼠标悬停显示文字</p>
</div>
</template>
<script>
export default {
data() {
return {
message: '这是要显示的文字'
}
},
methods: {
show() {
// 根据需要添加其他逻辑
this.$el.querySelector('p').style.color = 'red'
},
hide() {
// 根据需要添加其他逻辑
this.$el.querySelector('p').style.color = 'black'
}
}
}
</script>
<style>
p[title]:hover:after {
content: attr(title);
padding: 5px;
color: white;
background-color: black;
position: absolute;
z-index: 1;
}
</style>
以上代码实现了鼠标悬停在<p>
标签上时显示message
变量中的文字,鼠标移开时隐藏。同时,鼠标悬停时文字的样式会发生变化,这里使用了CSS伪类:hover
和:after
来实现。
相关推荐















