Detailed explanation of Touch events in JavaScript

1. Introduction to touch event

  • touchstart event: triggered when a finger touches the screen, even if a finger is already on the screen;
  • touchmove event: triggered continuously when a finger slides on the screen. During this event, calling the preventDefault() event can prevent scrolling;
  • touchend event: occurs when the user removes his finger from the element.
  • touchcancel event: occurs when the touch event is interrupted. Different devices will interrupt the touch event in different actions. If this "error" occurs, it is considered a good habit to include this event to clean up the code.

After each touch event is triggered, an event object will be generated. The event object additionally includes the following three touch lists

namemeaning
touchesList of all touch points on the current screen
targetTouchesA list of all touch points on the current object;
changedTouchesA list of touch points related to the current (raised) event;

For example, distinguish these three attributes in touch events:

1. Touch the screen with a finger to trigger an event. At this time, these three attributes have the same value.
2. Touch the screen with your second finger. At this time, touches There are two elements, and each finger touch point is a value.
   When two fingers touch the same element, targetTouches and touches The values are the same, otherwise targetTouches There is only one value.
   changedTouches At this time, there is only one value, which is the touch point of the second finger, because the second finger is the cause of the event.
3. Touch the screen with two fingers at the same time changedTouches There are two values, and each finger's touch point has a value
4. When the finger slides, all three values change
5. One finger off the screen, touches and targetTouches The corresponding elements in are removed at the same time, and changedTouches There will still be elements.
6. After all your fingers leave the screen, touches and targetTouches There will be no more values, changedTouches There will also be a value,
   This value is the contact point of the last finger to leave the screen.

Each touch in the touch list consists of touch objects (including touch information). The main attributes are as follows:

attributemeaning
clientXThe X coordinate of the contact relative to the left edge of the visual viewport. Does not include any rolling offset. Read only attribute
clientYThe Y coordinate of the contact relative to the upper edge of the visual viewport. Does not include any rolling offset. Read only attribute
pageXThe X coordinate of the contact relative to the left edge of the HTML document. When there is an offset of horizontal scrolling, this value contains the offset of horizontal scrolling. Read only attribute
pageYThe Y coordinate of the contact relative to the upper edge of the HTML document. When there is an offset of horizontal scrolling, this value contains the offset of vertical scrolling. Read only attribute
screenXThe X coordinate of the contact relative to the left edge of the screen. Read only attribute
screenYY coordinate of the contact relative to the upper edge of the screen. Read only attribute
radiusXThe horizontal axis (X axis) radius of the smallest ellipse that can surround the contact surface between the user and the touch plane. The unit of this value is the same as screenX. Read only attribute
radiusYThe vertical axis (Y axis) radius of the smallest ellipse that can surround the contact surface between the user and the touch plane. The unit of this value is the same as screen. Read only attribute
rotationAngleIt is such an angle value: the ellipse in the positive direction described by radiusX and radiusY needs to be rotated clockwise to most accurately cover the contact surface between the user and the touch plane. Read only attribute
forceThe pressure of the finger pressing the touch plane, a floating point number from 0.0 (no pressure) to 1.0 (maximum pressure). Read only attribute
targetWhen the contact is first tracked (in the touchstart event), the contact is located in the HTML element. Even in the process of contact movement, the position of the contact has left the effective interaction area of the element, or the element has been removed from the document. Note: if this element is removed during touch, the event will still point to it, but it will not bubble the event to the window or document object. Therefore, if an element may be removed during the touch process, the best practice is to bind the listener of the touch event to the element itself to prevent the event bubbling from the element from being detected from its upper element after the element is removed. Read only properties
identifierUnique identifier of Touch object. This identifier remains unchanged during the whole process of a Touch action moving on the plane. It can be used to judge whether the tracked process is the same Touch process. Read only properties

2. Simple case of using touch event in Vue

effect:

Full code:

<template>
  <div class="test">
    <div class="wrap">
      Background area
      <div class="animate__animated animate__zoomIn insert" v-if="showDeviceImg">insert draw</div>
    </div>
    <div class="drawer" ref="drawer">Touch the sliding area</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showDeviceImg:false,
      startY: 0,
      endY:0,
    };
  },
  methods: {
    handleTounchListener() {
      this.$refs.drawer.addEventListener('touchstart', (event) => {
        // Get the initial Y coordinate of the touch
        console.log(event)
        this.startY = event.targetTouches[0].pageY;
      })
      this.$refs.drawer.addEventListener('touchmove', (event) => {
        // If there are two contact points, return
        if (event.targetTouches.length > 1){ return }

        this.endY = event.targetTouches[0].pageY;
        const distance = this.endY - this.startY; // Calculate the vertical coordinate difference between the start and end positions
        if(distance > 60){
          // Slide down
          this.showDeviceImg = true;
        }else if(distance < -60){
          // wipe up
          this.showDeviceImg = false;
        }
        event.preventDefault(); // Block default scrolling
      })
      this.$refs.drawer.addEventListener('touchend', (event) => {
        console.log('touchEnd',event.targetTouches)
      })
      this.$refs.drawer.addEventListener('touchcancel', (event) => {
        console.log('touchCancel', event)
      })
    },
  },
  mounted() {
    this.handleTounchListener()
  },
};
</script>

<style lang="stylus" scoped>
.test {
  font-size: 25px;
  text-align: center;
  .wrap{
    width: 100%;
    min-height: 10vh;
    background-color: red;
    .insert{
      width: 100%;
      height: 30vh;
      background-color: green;
    }
  }
  .drawer{
    width: 100%;
    height: 100vh;
    background-color: pink;
  }
}
</style>  

Keywords: Javascript Front-end

Added by VirtualOdin on Wed, 24 Nov 2021 23:22:39 +0200