JavaScript event analysis in touch screens_javascript skills
The examples in this article describe JavaScript events in touch screens. Share it with everyone for your reference. The specific analysis is as follows:
1. Touch event
ontouchstart
ontouchmove
ontouchend
ontouchcancel Currently, all mobile browsers support these four touch events, including IE. Since touch screens also support MouseEvent, their order needs to be noted: touchstart → mouseover → mousemove → mousedown → mouseup → click1
Examples are as follows:
/** * onTouchEvent */ var div = document.getElementById("div"); //touchstart类似mousedown div.ontouchstart = function(e){ //事件的touches属性是一个数组,其中一个元素代表同一时刻的一个触控点, //从而可以通过touches获取多点触控的每个触控点 //由于我们只有一点触控,所以直接指向[0] var touch = e.touches[0]; //获取当前触控点的坐标,等同于MouseEvent事件的clientX/clientY var x = touch.clientX; var y = touch.clientY; }; //touchmove类似mousemove div.ontouchmove = function(e){ //可为touchstart、touchmove事件加上preventDefault从而阻止触摸时, //浏览器的缩放、滚动条滚动等 e.preventDefault(); }; //touchend类似mouseup div.ontouchup = function(e){ //nothing to do };
2. Gesture events Gestures refer to the use of multi-touch to perform operations such as rotation and stretching, such as magnification and rotation of pictures and web pages. Gesture events are triggered only when two or more fingers touch at the same time. One thing we need to pay attention to about scaling is the position coordinates of the element: we usually use offsetX, getBoundingClientRect and other methods to obtain the position coordinates of the element. However, in mobile browsers, the page is often scaled during use, and the scaled element coordinates will change. ? The answer is that it varies. Let's use a scenario to illustrate this problem: After page A is loaded, JavaScript obtains the coordinates of the element in the document as (100,100), and then the user zooms in on the page. At this time, JavaScript is used to output the coordinates of the element again, which is still (100,100). However, the element's responsive area on the screen will be offset based on the scaling. You can open the demo of the Brick Breaker game, wait until the page is fully loaded, and then zoom in. At this time, you will find that even if your finger touches outside the "touch here" area, you can still control the ball board because the area is offset. The offset will persist unless the page is refreshed or scaling is resumed.
/** * onGestureEvent */ var div = document.getElementById("div"); div.ongesturechange = function(e){ //scale代表手势产生的缩放比例,小于1是缩小,大于1是放大,原始为1 var scale = e.scale; //rotation代表旋转手势的角度,值区间[0,360],正值顺时针旋转,负值逆时针 var angle = e.rotation; };
3. Gravity sensing Gravity sensing is relatively simple. You only need to add the onorientationchange event to the body node. In this event, the value representing the current mobile phone orientation is obtained from the window.orientation property. The value list of window.orientation is as follows:
0: Same direction as when the page was first loaded
-90: Rotated 90° clockwise relative to the original direction
180: turned 180°
90: Turned 90° counterclockwise. According to my testing, Android 2.1 does not yet support gravity sensing. The above are the current touch screen events. These events have not yet been incorporated into the standard, but they are already widely used. I have Android 2.1 and have not tested it in other environments.
I hope this article will be helpful to everyone’s JavaScript programming design.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

When using win10 tablet mode, we can use some external devices to expand the control of the computer using the touch screen. However, many users cannot touch the screen when using the tablet mode. How to solve the problem? What to do if win10 tablet mode cannot touch the screen: 1. First, connect the external touch device and display device. 2. Then go to Settings from the Start menu and select. 3. Then find the multi-monitor settings in one column, select the new monitor, and select. 4. Then open the control panel and enter the options. 5. Then we choose to enter (if you do not connect a touch device, this item will not be available) 6. Then we can configure the pen and touch screen display. 7. After the configuration is completed, we can use it directly

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:
