• vue 鼠标划入划出多传一个参数


    // item可以传递弹窗显示数据, $event相关参数可以用来做弹窗定位用
    @mouseover="handleMouseOver($event, item)" @mouseleave="handleMouseLeave($event, item)"
    
    • 1
    • 2

    举个栗子: 做一个hover提示弹窗组件(用的vue3框架 + less插件)
    可以将组件放在代码的最外层

    <div
       v-show="show"
       ref="recentRef"
       class="recent-item-tip"
       :style="{ left: posX + 'px', top: posY + 'px', opacity: opacity }"
       @mouseover="handleMouseOver2" @mouseleave="handleMouseLeave2"
      >
          {{ '这里添加提示相关信息' }}
    </div>
    
    <script>
    import { ref, ,nextTick } from 'vue';
    // 提示内容
    const titleInfo = ref('')
    // 鼠标的横轴 
    let posX = ref(0);
    // 鼠标纵轴
    let posY = ref(0);
    // 控制显隐
    let show = ref(false);
    // 是否透明
    let opacity = ref(0);
    // 触发hover的DOM
    const recentRef = ref<HTMLDivElement | null>(null);
    const timer = ref(null);
    // 获取鼠标位置
    function handleMouseOver(e, item) {
      titleInfo.value = item.titleInfo;
      // 防抖
      if (timer.value) {
        clearTimeout(timer.value);
      }
      timer.value = setTimeout(() => {
        // 更新列表
        handleChangePos(e, item);
        clearTimeout(timer.value);
      }, 500);
    }
    // 这两个xx2函数是为了当鼠标划入提示弹窗内让弹窗不消失
    function handleMouseOver2(e, item) {
      show.value = true;
    }
    function handleMouseLeave2(e, item) {
      show.value = false;
    }
    // 提示框定位
    function handleChangePosition(e, item) {
      show.value = true;
      pushInfo.value = item.pushTime;
      let clientHeight = document.documentElement.clientHeight;
      let clientWidth = document.documentElement.clientWidth;
      let pointX = e.clientX;
      let pointY = e.clientY;
      nextTick(() => {
        // 内容宽高
        let selfWidth = recentRef.valueOf.clientWidth;
        let selfHeight = 80;
        if (pointX + selfWidth > clientWidth) {
          pointX = clientWidth - selfWidth - 10;
        }
        if (pointY + selfHeight > clientHeight) {
          pointY = clientHeight - selfHeight - 10;
        }
        opacity.value = 1;
        posX.value = pointX;
        posY.value = pointY;
      });
    }
    // 鼠标滑走隐藏
    function handleMouseLeave() {
      // show.value = false;
      // opacity.value = 0;
      // pushInfo.value = '';
      clearTimeout(timer.value);
      timer.value = null;
    }
    </script>
    
    <style lang="less">
      .recent-item-tip {
        height: 80px;
        width: 200px;
        overflow-y: scroll;
        position: fixed;
        z-index: 999;
        top: 0;
        left: 0;
        font-size: 12px;
        padding: 5px;
        line-height: 20px;
        box-sizing: border-box;
        word-break: keep-all;
        background-color: #fff; // #fff
        border: 1px solid #1113171a; // #1113171A
        border-radius: 4px;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.3);
        opacity: 0;
      }
     	//滚动条样式
       ::-webkit-scrollbar {
       width: 5px;
       height: 6px;
       border-radius: 16px;
      }
      ::-webkit-scrollbar-thumb {
        border-radius: 16px;
      }
      ::-webkit-scrollbar-track {
        border-radius: 16px;
      }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
  • 相关阅读:
    凭着这份《微服务架构实战》,带你立足实战落地微服务架构
    Python入门 | 输入语句的细节
    【考研】数据结构(更新到顺序表)
    MATLAB算法实战应用案例精讲-【智能优化算法】黏菌算法(SMA)
    vue封装带确定、取消按钮的弹窗和提示组件,可用promise回调
    驱动文件讲解
    【python初学者日记】用PIL批量给HEIC格式的照片,添加拍摄日期、拍摄地点的水印戳
    代码随想录 动态规划Ⅸ
    优维低代码实践:图片和搜索
    链路追踪Skywalking应用实战
  • 原文地址:https://blog.csdn.net/SSS01233210/article/details/133879121