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=''
}
}

这样移入移出就完成了



最新文章

  1. bzoj3087: Coci2009 misolovke
  2. prepareStatement与Statement的区别
  3. Firefox火狐添加书签功能失灵解决办法
  4. Java for LeetCode 061 Rotate List
  5. 三层架构和MVC模式
  6. Unity3d之MonoBehaviour的可重写函数整理
  7. TreeView递归取值
  8. Xcode中,调试console窗口输出error: Couldn&#39;t materialize struct: the variable &#39;cell&#39; has no location, it may have been optimized out的问题
  9. Spring MVC 解读——&lt;mvc:annotation-driven/&gt;(转)
  10. leetcode72. Edit Distance
  11. hdu 1240 Asteroids! (三维bfs)
  12. C# sql操作
  13. 基于visual Studio2013解决C语言竞赛题之1053洗牌
  14. printf与++的puzzle
  15. node将excel内容转json
  16. dedecms搜索提示"关键字不能小于2个字节!"
  17. pythonl练习
  18. BuautifulSoup4库详解
  19. linux 邮件服务器—Extmail
  20. Html5计算MD5值

热门文章

  1. C#事件の事件聚合器(二)
  2. 1.03-get_params2
  3. jsp页面无法使用EL
  4. 开源框架bboss单点登录demo跑起来
  5. X86-64寄存器和栈帧--牛掰降解汇编函数寄存器相关操作
  6. [CF1137E]Train Car Selection[维护凸壳]
  7. FineUIMvc新特性速递(大间距模式,隐藏菜单垂直滚动条)
  8. 深入理解Java虚拟机(类文件结构+类加载机制+字节码执行引擎)
  9. 提取jedis源码的一致性hash代码作为通用工具类
  10. .net Core 调用微信Jsapi接口,H5解析二维码