vue鼠标移入移除带动画事件实例

vue鼠标移入移除事件实例

效果图:

效果图
在这里插入图片描述

在这里插入图片描述

一. 技术分析

vue所有的鼠标事件:

  1. 单击

    @click=‘click’

  2. 按下

    @mousedown=‘down’

  3. 抬起

    @mouseup=‘up’

  4. 双击

    @dblclick=‘dblclick’

  5. 移动

    @mousemove=‘move’

  6. 移除

    @mouseout=‘out’

  7. 离开

    @mouseleave=‘out’

  8. 进入

    @mouseenter=‘enter’

  9. @mouseover=‘enter’

  • 这个实例中只用到了8.进入和7.离开。
  • 6.移除mouseout和7.离开mouseleave的区别:
    • 不论鼠标指针离开被选元素还是任何子元素,都会触发mouseout事件
    • 只有在鼠标指针离开被选元素时,才会触发mouseleave事件

二.实例的实现分析

1. 初始状态

在这里插入图片描述

div高200px分为两部分每部分高200px,初始状态上部分显示,下部分隐藏

2. 鼠标移入状态

在这里插入图片描述
当鼠标移入时,整体向上移动200px,上部分隐藏漏出下部分

3. 鼠标移出状态

当鼠标移出时,整体不是向下移动,而是从上边200px的位置回到0px的位置

三.源代码

<template>
  <div class="index">
    我是index
    <div class="all" v-on:mouseenter="mouChange($event)" v-on:mouseleave="mouChange1($event)">
      <div class="top" :class="mouseenter==true?'change':'change1'"></div>
      <div class="bottom" :class="mouseenter==true?'change':'change1'"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'index',
  data () {
    return {
      mouseenter: false
    };
  },

  components: {},

  computed: {},

  mounted () { },

  methods: {
    mouChange () {
      this.mouseenter = true
    },
    mouChange1 () {
      this.mouseenter = false
    }
  }
}

</script>
<style lang='less' scoped>
.all {
  width: 100px;
  height: 200px;
  background-color: #afc;
  border: 3px solid #000;
  position: relative;
  margin-top: 300px;
  margin-left: 100px;
  .top {
    width: 100px;
    height: 200px;
    background-color: #acf;
    position: absolute;
    top: -0px;
    z-index: 100;
  }
  .bottom {
    width: 100px;
    height: 200px;
    background-color: red;
    position: absolute;
    top: 200px;
    z-index: 100;
  }
}
.all .change {
  animation: myMou 0.5s;
  animation-fill-mode: forwards;
}
.all .change1 {
  animation: myMou1 0.5s;
  animation-fill-mode: forwards;
}
@keyframes myMou {
  0% {
    margin-top: 0px;
  }
  100% {
    margin-top: -200px;
  }
}
@keyframes myMou1 {
  0% {
    margin-top: -200px;
  }
  100% {
    margin-top: 0px;
  }
}
</style>

四.举一反三的例子

在这里插入图片描述

  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值