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

爱被打了一巴掌 2022-11-29 11:55 444阅读 0赞

vue鼠标移入移除事件实例

文章目录

  • vue鼠标移入移除事件实例
    • 效果图:
    • 一. 技术分析
    • 二.实例的实现分析
        1. 初始状态
        1. 鼠标移入状态
        1. 鼠标移出状态
    • 三.源代码
    • 四.举一反三的例子

效果图:

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

在这里插入图片描述

一. 技术分析

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的位置

三.源代码

  1. <template>
  2. <div class="index">
  3. 我是index
  4. <div class="all" v-on:mouseenter="mouChange($event)" v-on:mouseleave="mouChange1($event)">
  5. <div class="top" :class="mouseenter==true?'change':'change1'"></div>
  6. <div class="bottom" :class="mouseenter==true?'change':'change1'"></div>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'index',
  13. data () {
  14. return {
  15. mouseenter: false
  16. };
  17. },
  18. components: {},
  19. computed: {},
  20. mounted () { },
  21. methods: {
  22. mouChange () {
  23. this.mouseenter = true
  24. },
  25. mouChange1 () {
  26. this.mouseenter = false
  27. }
  28. }
  29. }
  30. </script>
  31. <style lang='less' scoped>
  32. .all {
  33. width: 100px;
  34. height: 200px;
  35. background-color: #afc;
  36. border: 3px solid #000;
  37. position: relative;
  38. margin-top: 300px;
  39. margin-left: 100px;
  40. .top {
  41. width: 100px;
  42. height: 200px;
  43. background-color: #acf;
  44. position: absolute;
  45. top: -0px;
  46. z-index: 100;
  47. }
  48. .bottom {
  49. width: 100px;
  50. height: 200px;
  51. background-color: red;
  52. position: absolute;
  53. top: 200px;
  54. z-index: 100;
  55. }
  56. }
  57. .all .change {
  58. animation: myMou 0.5s;
  59. animation-fill-mode: forwards;
  60. }
  61. .all .change1 {
  62. animation: myMou1 0.5s;
  63. animation-fill-mode: forwards;
  64. }
  65. @keyframes myMou {
  66. 0% {
  67. margin-top: 0px;
  68. }
  69. 100% {
  70. margin-top: -200px;
  71. }
  72. }
  73. @keyframes myMou1 {
  74. 0% {
  75. margin-top: -200px;
  76. }
  77. 100% {
  78. margin-top: 0px;
  79. }
  80. }
  81. </style>

四.举一反三的例子

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,444人围观)

还没有评论,来说两句吧...

相关阅读