问题详情
js获取页面光标坐标(x轴y轴)
解决方案
// 获取当前位置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.xthis.top = cursorPosition.y}, 180)},},
更多信息:
前端 - js获取页面光标坐标(x轴y轴)? - SegmentFault 思否