美文网首页
鼠标点击移动事件

鼠标点击移动事件

作者: FrankFang7 | 来源:发表于2022-03-17 17:22 被阅读0次
<template>
  <div id="box">
    <div id="line" :style="style"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      x: 0,
      y: 0,
      style: {}
    }
  },
  methods: {
    mouseDown (e) {
      e = e || window.Event
      if (!this.x && !this.y) {
        this.x = e.pageX
        this.y = e.pageY
      } else {
        this.x = 0
        this.y = 0
        this.style = {}
      }
    },
    mouseMove (e) {
      if (this.x && this.y) {
        const line = document.getElementById('line')
        line.classList = ['line']
        e = e || window.Event
        const style = {
          left: this.x + 'px',
          top: this.y + 'px',
          width: e.pageX - this.x + 'px',
          height: e.pageY - this.y + 'px'
        }
        this.style = style
      }
    }
  },
  mounted () {
    /* 这里是给整个页面添加了一个鼠标移动的监听事件 e为事件对象 */
    const box = document.getElementById('box')
    box.addEventListener('mousemove', this.mouseMove)
    box.addEventListener('mousedown', this.mouseDown)
  },
  beforeDestroy () {
    document.removeEventListener('mousemove', this.mouseMove)
    document.removeEventListener('mousedown', this.mouseDown)
  }
}
</script>

<style>
#box {
  height: 100vh;
  width: 100%;
  position: relative;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
}
#box #line {
  position: fixed;
  border: 1px solid red;
}
</style>

相关文章

  • Fabric.js 右键菜单

    本文简介 Fabric.js 默认没提供 鼠标右键事件,只有 鼠标按键点击 、鼠标按键抬起 、鼠标移动 等事件。但...

  • js事件入门(2)

    2.鼠标事件 鼠标事件就是用户与页面的许多交互时通过鼠标移动或者鼠标点击等触发的事件. #2.1.onmoused...

  • 鼠标点击移动事件

  • 事件

    (1)、鼠标事件: 鼠标点击事件(单击): onclick 鼠标点击事件(...

  • 鼠标事件

    1. 鼠标事件 // 点击事件 onclick // 双击事件 ondblclick // 鼠标右键点击事件 on...

  • DOM事件传播机制

    事件:JavaScript 和 HTML的交互是通过事件实现的。 事件是某个行为或者触发,比如点击、鼠标移动: 当...

  • DOM事件传播机制

    事件:JavaScript 和 HTML的交互是通过事件实现的。 事件是某个行为或者触发,比如点击、鼠标移动: 当...

  • 常用事件

    鼠标事件 onclick 点击事件点击鼠标左键触发 ondblclick双击事件双击鼠标左键触发 onmouseo...

  • 事件冒泡、捕获

    事件 JavaScript和HTML的交互是通过事件实现的。 事件是某个行为或者触发,比如点击、鼠标移动 当用户点...

  • JavaScript常用事件总结归纳

    1、鼠标事件 onclick 鼠标点击事件,当鼠标左键点击时候会触发。 ondbclick 当鼠标双击时候会触发...

网友评论

      本文标题:鼠标点击移动事件

      本文链接:https://www.haomeiwen.com/subject/qnavdrtx.html