3 个回答
✓ 已被采纳
    // 获取当前位置
    getPosition() {
      setTimeout(() => {
        // 获取当前光标位置getSelection()
        const selection = window.getSelection()
        if (selection.rangeCount === 0) {
          return false
        }
        // 获取选中区域的范围 获取光标的范围
        const range = selection.getRangeAt(0)
        // 获取选中区域的边界 获取当前光标的坐标点 这个方法里面都有getBoundingClientRect()
        const { top, left } = range.getBoundingClientRect()
        // 获取当前光标的位置
        const cursorPosition = {
          x: left,
          y: top + range.getBoundingClientRect().height,
        }
        this.left = cursorPosition.x
        this.top = cursorPosition.y
      }, 180)
    },
  },
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题