微信小程序用<cover-view>自定义tooltip样式覆盖canvas 且带有点击过几秒tooltip消失功能

wxml

<cover-view wx:if="{{isShowImg}}" class='rich-textq' style='top:{{topL}}px;left:{{leftL}}px;background-color :rgba(50, 50, 50, 0.7);padding:10px;font-size:12px;color:#fff;'>
	<cover-view wx:for='{{itemList}}' style="line-height: 20px;" wx:key="index">{{item}}</cover-view>
</cover-view>

wxcss

.rich-textq{
  position: fixed;
  transform:translateZ(1110);
  top: 0;
  z-index: 99999999999999;
  padding: 10px;
  background:'rgba(50,50,50,0.7)'
}

tooltip

 tooltip: {
            trigger: 'axis',
            transitionDuration: 0,
            axisPointer: {
              // 坐标轴指示器,坐标轴触发有效
              type: 'shadow', // 默认为直线,可选为:'line' | 'shadow'
              shadowStyle: { color: 'rgba(150,150,150,0)' }
            },
            confine: true,
            appendToBody: true,
            position: function (point, params, dom, rect, size) {
              var x = 0; // x坐标位置
              var y = 0; // y坐标位置
              // 当前鼠标位置
              var pointX = point[0];
              var pointY = point[1];
              // 提示框大小
              var boxWidth = 0;
              var boxHeight = 0;
              that.setData({
                isShowImg: true
              })
              wx.createSelectorQuery().selectAll('.rich-textq').boundingClientRect(function (rect) {
                boxWidth = rect[0].width;
                boxHeight = rect[0].height;
                if (boxWidth > pointX) {
                  x = 5;
                } else { // 左边放的下
                  x = pointX - boxWidth;
                }
                if (boxHeight > pointY) {
                  y = 5;
                } else { // 上边放得下
                  y = pointY - boxHeight;
                }
                let itemList = [params[0].name, '车次总数:' + (params[0].value === zeroValue ? 0 : params[0].value)]
                if (params[0].data.vehicleTypeDataList) {
                  let obj = params[0].data.vehicleTypeDataList;
                  for (let k in obj) {
                    itemList.push(k + ":" + obj[k])
                  }
                }
                that.setData({
                  itemList: itemList, // tooltip 要显示的名称及数据
                  topL: pointY, // tooltip 上边距
                  leftL: x, // tooltip 左边距
                })
                that.setTooltipBox() // 点击一下4.5秒后tooltip消失
              }).exec()
            },
            renderMode: 'richText'
          },

效果
在这里插入图片描述
可以看到 tooltip 的层级已经盖住了canvas 而且超出盒子不会被切割 哈哈哈哈哈…
如果需要点击一下过几秒消失代码如下:

  setTooltipBox: function () {
    var that = this;
    if (t) {
      clearTimeout(t);
    }
    t = setTimeout(() => {
      that.setData({
        isShowImg: false,
      })
    }, 4500) // tooltip消失时间
  },
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值