Vue 鼠标移入移出事件
2024-08-26 10:41:00
Vue中鼠标移入移出事件
@mouseover和@mouseleave 然后绑定style
现在开始代码示例
<template>
<div class="pc">
<h1>{{ msg }}</h1>
<div
class="demo"
@mouseover="mouseOver"
@mouseleave="mouseLeave"
:style="active"
>
<p ref="acp">我是内容</p>
</div>
</div>
</template> <script>
export default {
name: "HelloWorld",
data() {
return {
msg: "HelloWorld,I am PC",
active: "",
};
},
methods: {
// 移入
mouseOver() {
this.active = "background-color:black";
// 操作dom 获取p标签改变其样式
var acps = this.$refs.acp
acps.style.color = "red"
},
// 移出
mouseLeave() {
this.active = "";
this.$refs.acp.style=''
}
}
};
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.pc {
.demo {
width: 100%;
height: 300px;
background-color: lightcoral;
p {
color: limegreen;
}
}
}
</style>
1、给需要使用移入移出事件的添加事件:
@mouseover @mouseleave
2、绑定style 这个 `active` 是绑定名 可以自己随意更换
:style="active"
3、在 data 里定义 绑定的类名
data() {
return {
msg: "HelloWorld,I am PC",
active: "",
};
},
4、在 methods 里定义事件 要改变内部的元素 通过ref 操作dom
methods: {
mouseOver() {
this.active = "";
var acps = this.$refs.acp
acps.style.color="red"
},
mouseLeave() {
this.active = "";
this.$ref.acp.style=''
}
}
这样移入移出就完成了
最新文章
- bzoj3087: Coci2009 misolovke
- prepareStatement与Statement的区别
- Firefox火狐添加书签功能失灵解决办法
- Java for LeetCode 061 Rotate List
- 三层架构和MVC模式
- Unity3d之MonoBehaviour的可重写函数整理
- TreeView递归取值
- Xcode中,调试console窗口输出error: Couldn&#39;t materialize struct: the variable &#39;cell&#39; has no location, it may have been optimized out的问题
- Spring MVC 解读——<;mvc:annotation-driven/>;(转)
- leetcode72. Edit Distance
- hdu 1240 Asteroids! (三维bfs)
- C# sql操作
- 基于visual Studio2013解决C语言竞赛题之1053洗牌
- printf与++的puzzle
- node将excel内容转json
- dedecms搜索提示"关键字不能小于2个字节!"
- pythonl练习
- BuautifulSoup4库详解
- linux 邮件服务器—Extmail
- Html5计算MD5值
热门文章
- C#事件の事件聚合器(二)
- 1.03-get_params2
- jsp页面无法使用EL
- 开源框架bboss单点登录demo跑起来
- X86-64寄存器和栈帧--牛掰降解汇编函数寄存器相关操作
- [CF1137E]Train Car Selection[维护凸壳]
- FineUIMvc新特性速递(大间距模式,隐藏菜单垂直滚动条)
- 深入理解Java虚拟机(类文件结构+类加载机制+字节码执行引擎)
- 提取jedis源码的一致性hash代码作为通用工具类
- .net Core 调用微信Jsapi接口,H5解析二维码