iview的Tree组件设置Tooltip鼠标悬浮文字

在解决问题中考虑到的点:

  1. 树形组件要是过多,会出现鼠标滚轮,鼠标滚轮的时候也会触发鼠标悬浮事件,这时候tooltip会显示出上次滚轮经过的文字,并且会出现tooltip闪动。

解决方法:

  1. 使用防抖函数在组件的render里设置鼠标滚轮事件mousewheel

具体代码

data() {
	return {
		timeout: null,
      	showTooltip: true
	}
}

// tree 组件渲染 设置Tooltip
    renderContent(h, { root, node, data }) {
      let texts = "";
      if (
        data.title !== null &&
        data.title !== undefined &&
        data.title !== ""
      ) {
        texts = data.title;
      }
      return h(
        "div",
        {
          style: {
            display: "inline-block",
            width: "100%",
            cursor: "pointer",
          },
          on: {
            // 监听鼠标滚轮 防抖函数解决滚轮滚动出现tooltip闪烁
            mousewheel: () => {
              // 不滚动时清除定时器 不显示tooltip
              clearTimeout(this.timeout);
              this.showTooltip = false
              this.timeout = setTimeout(() => {
                this.showTooltip = true
                clearTimeout(this.timeout);
              }, 500);
            },
            // 设置事件为被动 不会因为主线程影响到这个事件
            passive: true
          },
        },
        this.showTooltip ? [
          h(
            "Tooltip",
            {
              props: {
                placement: "top",
                transfer: true,
                maxWidth: 270,
              },
            },
            [
              texts,
              h(
                "span",
                {
                  slot: "content",
                  style: {
                    whiteSpace: "normal",
                  },
                },
                data.title
              ),
            ]
          ),
        ] : [
              h(
                "span",
                {
                  slot: "content",
                  style: {
                    whiteSpace: "normal",
                  },
                },
                data.title
              ),
            ]
      );
    },
  },
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值