vue鼠标点击和滑动锚点

效果

在这里插入图片描述

<ul class="list">
  <li
    class="item"
    v-for="item in letters"
    :key="item"
    :ref="item"
    @click="handleLetterClick"
    @touchstart.prevent="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
  >
    {{ item }}
  </li>
</ul>
	// 点击锚点
	handleLetterClick(e) {
      this.$emit("change", e.target.innerText); // A B C D
    },
    // 滑动锚点
    handleTouchStart() {
      this.touchStatus = true;
    },
    handleTouchMove(e) {
      // 思路就是获取A到顶部的高度,滑到B的时候让B的高度减去A 的高度就知道那个字母
      if (this.touchStatus) {
        // 当上下可以拖动的时候
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          // 正在滑动的字母到顶部的距离减去头部的距离
          const touchY = e.touches[0].clientY - 79;
          // A字母到顶部的距离 浅蓝色下沿
          const startY = this.$refs["A"][0].offsetTop // 207
          // 移动中距离顶部的高度的减去第一个到顶部的高度 / 每个字母的高度
          const index = Math.floor((touchY - startY) / 20);
          if (index >= 0 && index < this.letters.length) {
            this.$emit("change", this.letters[index]); // A B C D
          }
        }, 8);
      }
    },
    handleTouchEnd() {
      this.touchStatus = false;
    },

	// 列表组件
	// 模板需要动态的绑定ref 为A B C D即可
	watch: {
	    letter() {
	      if (this.letter) {
	        const element = this.$refs[this.letter][0];
	        // 进行滚动
	        this.scroll.scrollToElement(element, 500);
	      }
	    },
	  },
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值