js:获取鼠标点击位置,弹出提示框

在这里插入图片描述

完整代码

<script>
  // 监听鼠标点击事件
  document.addEventListener('click', function(event){
      
      
    console.log('mouseup');
    let box = document.querySelector('#tip-box');

    if(!box){
      
      
      box = document.createElement('div');
      box.classList.add('tip-box');
      box.setAttribute('id', 'tip-box')
      document.body.append(box);
    }

    box.innerHTML = 'Hello';
    box.style.top = event.clientY + 'px';
    box.style.left = event.clientX + 'px';
    box.style.display = 'block';

  })
</script>

<style>
  .tip-box {
      
      
    border: 1px solid #cfcfcf;
    border-radius: 3px;
    padding: 15px 22px;
    position: fixed;
    min-width: 300px;
    max-width: 350px;
    width: auto;
    height: auto;
    min-height: 100px;
    background: #efefef;
    display: none;
    line-height: 1.5em;
}
</style>

参考
JavaScript实现划词翻译

猜你喜欢

转载自blog.csdn.net/mouday/article/details/125362325