vue/javascript 实现拖动功能是文字总会被选中的问题

你们是否遇到过按住鼠标左键拖动div块时经常页面上的文字会被选中了,这样造成了不好的开发体验。以下实例能帮你们解决这一问题。

	//鼠标按下的事件
      onMousedown(event, column) {
        const resizeProxy = this.$refs.resizeProxy;
        resizeProxy.style.left = this.dragState.startLeft + 'px';
        document.onselectstart = function() { return false; };//解决拖动会选中文字的问题
        document.ondragstart = function() { return false; };

        const handleMouseMove = (event) => {
          const deltaLeft = event.clientX - this.dragState.startMouseLeft;
          const proxyLeft = this.dragState.startLeft + deltaLeft;

          resizeProxy.style.left = Math.max(minLeft, proxyLeft) + 'px';
        };

        const handleMouseUp = () => {
          document.removeEventListener('mousemove', handleMouseMove);
          document.removeEventListener('mouseup', handleMouseUp);
          document.onselectstart = null;
          document.ondragstart = null;
        };

        document.addEventListener('mousemove', handleMouseMove);
        document.addEventListener('mouseup', handleMouseUp);
      },

注意上面我只保留了主干的代码,是不完整的代码。
主要看这行

document.onselectstart = function() { return false; };//解决拖动会选中文字的问题

这个就是解决选中文字的问题,按住鼠标左键时进行拖动,让 selectstart事件阻止默认即可。

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue 可以通过监听页面的 `mouseup` 事件实现页面选中文字添加批注的功能。具体实现步骤如下: 1. 监听 `mouseup` 事件,获取选中文字。 ```html <template> <div ref="content" @mouseup="handleMouseUp"> {{ content }} </div> </template> ``` ```javascript export default { data() { return { content: '这是一段文字,可以被选中' } }, methods: { handleMouseUp() { const selection = window.getSelection() const selectedText = selection.toString().trim() if (selectedText) { // 选中文字 } } } } ``` 2. 在选中文字上添加批注。 ```html <template> <div ref="content" @mouseup="handleMouseUp"> {{ content }} <div v-if="showComment" class="comment-box"> <textarea v-model="comment"></textarea> <button @click="saveComment">保存</button> </div> </div> </template> ``` ```javascript export default { data() { return { content: '这是一段文字,可以被选中', showComment: false, comment: '', selectedRange: null } }, methods: { handleMouseUp() { const selection = window.getSelection() const selectedText = selection.toString().trim() if (selectedText) { const range = selection.getRangeAt(0) this.selectedRange = range const rect = range.getBoundingClientRect() const top = rect.top + window.pageYOffset const left = rect.left + window.pageXOffset this.showComment = true this.$nextTick(() => { const commentBox = this.$el.querySelector('.comment-box') commentBox.style.top = `${top}px` commentBox.style.left = `${left}px` }) } else { this.showComment = false } }, saveComment() { const selection = window.getSelection() const range = this.selectedRange const comment = this.comment // 在选中文字上添加批注 const commentNode = document.createElement('span') commentNode.className = 'comment' commentNode.innerText = comment range.surroundContents(commentNode) this.showComment = false } } } ``` 以上是一个简单的实现,可以根据需求进行扩展和优化。需要注意的是,这种实现方式只能在页面内保存批注,刷新页面后批注丢失。如果需要将批注保存到后台,需要通过后台接口进行处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值