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

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

举个栗子: 做一个hover提示弹窗组件(用的vue3框架 + less插件)

可以将组件放在代码的最外层

javascript 复制代码
<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>
相关推荐
GuWenyue8 小时前
放弃云端API!一套React+WebGPU本地LLM方案,零数据上传、离线可用
前端·人工智能·前端框架
原则猫9 小时前
函数/变量提升
前端
独泪了无痕10 小时前
Vue3 Hooks使用实战解析
前端·vue.js
shawxlee12 小时前
vue3在public下封装config.js自定义配置动态数据,可在打包后直接修改,方便后端部署及后续维护
前端·javascript·经验分享·vue·团队开发·js·项目优化
用户0595401744612 小时前
把记忆存储的回归测试从手工换成 Playwright + GitHub Actions,线上缺陷降低 80%
前端·css
触底反弹12 小时前
🚀 从 DOM0 级到 React 合成事件:前端事件监听的 20 年演进史
前端·react.js·面试
kyriewen12 小时前
我给前端项目的接口请求套了6层保护——才发现以前一直在裸奔
前端·javascript·面试
颜酱12 小时前
04 | 召回前置准备:搭好召回所需的四个数据库
前端·人工智能·后端
郝亚军14 小时前
如何安装webstorm、Node.js和vue CLI
前端·javascript·vue.js
IT_陈寒14 小时前
React的useEffect依赖项把我坑惨了
前端·人工智能·后端