[标准]总结Vue鼠标事件和键盘事件、以及触屏事件

zhuanbike 2022-2-1 1596

1、鼠标事件:

@click//单击
@mousedown//按下
@mouseup//抬起
@dblclick//双击
@mousemove//移动
@mouseleave//离开
@mouseout //移出
@mouseenter//进入
@mouseover//在(元素上类似 hover)

vue的鼠标事件比js本身的鼠标事件编写要简单不少,具体我还没在canvas中和vue相结合使用。

举例:

<DOCTYPE html><html lang="en">
<head>
    <meta charset="utf-8">
    <title>Vue.js</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <style type="text/css">
    #canvase{
    width:600px;
    padding:100px 20px;
    text-align: center;
    border: 1px solid #333;}
    </style>
    </head>
    
    <body>  
    <div id="vue-app">
        <h1>Event</h1>
        <button @click.once="add(1)">涨一岁</button><!--只能点击一次-->
        <button @click="sub(1)">减一岁</button>
        <button @dblclick="add(10)">涨十岁</button>
        <button @dblclick="sub(10)">减十岁</button><!-- 双击事件-->
        <p>My age is {{ age }}</p>
        <div id="canvase" @mousemove="updateXY">{{ x }},{{ y }}        <span id="stopmove" @mousemove.stop.prevent> 数据停止</span>
        </div>
        <a href="https://www.baidu.com" @click.prevent="alert()">百度</a><!--阻止默认事件-->
    </div>

    <script type="text/javascript">
    //实例化vue对象
    
    new Vue({
    el:"#vue-app",
    data:{
        age:30,
        x:0,
        y:0
    },
    methods:{
        add: function(int) {
            this.age += int;
        },
        sub: function(int) {
            this.age -= int;
        },
        updateXY: function(event){
            // console.log(event);
            this.x = event.offsetX;
            this.y = event.offsetY;
        },
        alert: function(){
            alert("点击成功,但是被prevent属性了");
        }

    }   });

    </script>
    </body>
   </html>


2、键盘事件

vue的键盘事件和js原生写法有所简化,比如我们可以直接用keyup.enter()  来监听 keycode=13这个事件。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <script src="../js/Vue.js" charset="utf-8"></script>
  <script type="text/javascript">
    window.onload = function () {
      var vm = new Vue({
        el: '#box',
        data: {},
        methods: {
          show: function () {
            alert('你按了回车!');
          },
          show2: function () {
            alert('你按了回车!');
          },
          show3: function () {
            alert('你按了上键!');
          },
          show4: function () {
            alert('你按了下键!');
          },
          show5: function () {
            alert('你按了左键!');
          },
          show6: function () {
            alert('你按了右键!');
          }
        }
      });
    }
  </script>
</head>
<body>
<div id="box">
  <input type="text" @keyup.13="show()">
  <hr>
  <input type="text" @keyup.enter="show2()">
  <hr>
  <input type="text" @keyup.up="show3()">
  <hr>
  <input type="text" @keyup.down="show4()">
  <hr>
  <input type="text" @keyup.left="show5()">
  <hr>
  <input type="text" @keyup.right="show6()">
  <hr>
</div>
</body>
</html>

3、触屏事件

touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发。

touchmove事件:当手指在屏幕上滑动的时候连续地触发。在这个事件发生期间,调用preventDefault()事件可以阻止滚动。

touchend事件:当手指从屏幕上离开的时候触发。

touchcancel事件:当系统停止跟踪触摸的时候触发。

上面的这些事件都会冒泡,也都可以取消。虽然这些触摸事件没有在DOM规范中定义,但是它们却是以兼容DOM的方式实现的。所以,每个触摸事件的event对象都提供了在鼠标实践中常见的属性:bubbles(起泡事件的类型)、cancelable(是否用 preventDefault() 方法可以取消与事件关联的默认动作)、clientX(返回当事件被触发时,鼠标指针的水平坐标)、clientY(返回当事件触发时,鼠标指针的垂直坐标)、screenX(当某个事件被触发时,鼠标指针的水平坐标)和screenY(返回当某个事件被触发时,鼠标指针的垂直坐标)。除了常见的DOM属性,触摸事件还包含下面三个用于跟踪触摸的属性。

  touches:表示当前跟踪的触摸操作的touch对象的数组。

  targetTouches:特定于事件目标的Touch对象的数组。

  changeTouches:表示自上次触摸以来发生了什么改变的Touch对象的数组。

  每个Touch对象包含的属性如下。

  clientX:触摸目标在视口中的x坐标。

  clientY:触摸目标在视口中的y坐标。

  identifier:标识触摸的唯一ID。

  pageX:触摸目标在页面中的x坐标。

  pageY:触摸目标在页面中的y坐标。

  screenX:触摸目标在屏幕中的x坐标。

  screenY:触摸目标在屏幕中的y坐标。

  target:触目的DOM节点目标。

<template>
  <div class="main"
       @touchmove="touchmove"
       @touchstart="touchstart"
       @touchend="touchend"
       @touchcancel="touchcancel"
  >
    <div class="ball"
         ref="ball"
    ></div>
  </div>
</template>
<script>
  import vconsole from 'vconsole'
  new vconsole()
  export default {
    name: "t1",
    methods: {
      touchmove(e) {
        console.log('move', e)
        this.$refs.ball.style.left = e.touches[0].clientX - 50 + 'px'
        this.$refs.ball.style.top = e.touches[0].clientY - 50 + 'px'
      },
      touchstart(e) {
        console.log('start', e)
        this.$refs.ball.style.left = e.touches[0].clientX - 50 + 'px'
        this.$refs.ball.style.top = e.touches[0].clientY - 50 + 'px'
      },
      touchend(e) {
        console.log('end', e)
      },
      touchcancel(e) {
        console.log('cancel', e)
      }
    }
  }
</script>
<style scoped>
  .main {
    width: 100vw;
    height: 100vh;
    background: deepskyblue;
    position: relative;
  }
  .ball {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    position: absolute;
    left: 0;
    top: 0;
    background: gray;
  }
</style>


最新回复 (0)
发新帖