前端js禁止左键选中操作,并且左键提示框的位置控制。

<template>
  <div class="hello" @cilck="clicks" @contextmenu.prevent="stop($event)">
    <h1 @click="seeWidth">测试</h1>
    <div class="noCopy">测试禁用复制功能</div>
    <div class="noCopy">测试禁用复制功能</div>
    <div class="noCopy">测试禁用复制功能</div>
    <span v-show="issee" id="rightClickMenu" class="el-menu-vertical">
      <ul>
        <li>111</li>
      </ul>
      <ul>
        <li>qqq</li>
        <li>www</li>
        <li>rrr</li>
      </ul>
    </span>
  </div>
</template>

<script>
export default {
  name: 'First',
  data(){
    return {
      issee:false
    }
  },
  methods:{
    //js禁止鼠标选中,来控制复制功能
    //第一种方式 通过js来控制
    、、第二种  通过css来控制 ,见class样式.noCopy
    seeWidth(){
       //document.addEventListener('click', () => {
        //   this.issee = true;
      // })
      document.onselectstart = function(){ return false; };// 禁止文字选择
	
	document.oncopy = function(){ return false; };// 禁止复制
	
	document.oncut = function(){ return false; };// 禁止剪切
	
	document.onpaste = function(){ return false; };// 禁止粘贴	
    },
    stop (event){
      this.issee = true;
      const see=document.body.clientWidth
      const heightt= document.body.clientHeight   
      let menu = document.querySelector("#rightClickMenu");
      menu.style.left =event.clientX + 180>see?see-200+'px':event.clientX + 20 + "px"
      menu.style.top = event.clientY +157 >heightt?heightt-127+'px':event.clientY + "px";
      // menu.style.left = event.clientX + 20 + "px";
      // menu.style.top = event.clientY + "px";
      menu.style.position = "absolute"; // 为新创建的DIV指定绝对定位
      menu.style.width = 160 + "px";
      // return false
    },
     clicks(){
      this.issee = false;
    }
  },
}
</script><style scoped>
.noCopy{
  height:100px;
  /* css控制复制功能 */
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none; /* Chrome/Safari/Opera */
  -khtml-user-select: none; /* Konqueror */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* Internet Explorer/Edge */
  user-select: none;
}
.el-menu-vertical {
  border: 1px solid rgb(84, 92, 100);
  border-radius: 5px;
  z-index: 100;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取鼠标左键选中的内容,我们可以借助JavaScript来实现。可以通过监听鼠标按键的点击事件来判断是否按下了鼠标左键,并在鼠标拖动的过程中实时获取选中的内容。 首先,我们需要给需要选中内容的元素添加一个鼠标按下事件的监听器,当鼠标按下时,获取到鼠标按下的位置坐标。 接着,在鼠标移动的过程中,我们就可以根据鼠标位置坐标和选中的内容区域来获取被选中的文字内容。我们可以通过`window.getSelection()`来获取当前选中的内容。 最后,在鼠标抬起时,我们就可以得到最终选中的内容了。可以通过`window.getSelection().toString()`来获得选中的文字内容。 以下是一个简单的示例代码: ```javascript // 获取选中的内容 function getSelectedText() { const selectedText = window.getSelection().toString(); console.log(selectedText); } // 监听鼠标按下事件,获取按下的位置坐标 document.addEventListener('mousedown', function(event) { if (event.button === 0) { // 判断是否是鼠标左键按下 var startPoint = { x: event.clientX, y: event.clientY }; document.addEventListener('mousemove', mouseMoveHandler); } }); // 监听鼠标抬起事件,获取选中的内容 document.addEventListener('mouseup', function(event) { if (event.button === 0) { // 判断是否是鼠标左键抬起 document.removeEventListener('mousemove', mouseMoveHandler); getSelectedText(); } }); // 鼠标拖动事件,实时获取选中的内容 function mouseMoveHandler(event) { var currentPoint = { x: event.clientX, y: event.clientY }; // 根据起始点和当前点坐标计算选中的内容区域 // 然后可以根据这个区域来获取选中的内容 } ``` 以上就是使用JavaScript获取鼠标左键选中的内容的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值