This time I will show you how to determine the user's sliding direction in H5 touch events. What are the precautions for determining the user's sliding direction in H5 touch events? Here is a practical case, let's take a look.
Interface
##TouchEvent
TouchEvent is a type that describes the movement of a finger on a touch plane (touch screen, Touchpad, etc.) state change events. This type of event is used to describe one or more touch points, allowing developers to detect the movement of touch points, the increase and decrease of touch points, etc. Each Touch object represents a touch point; each touch point is described by its position, size, shape, pressure level, and target element. The TouchList object represents a list of multiple touch points.Types of touch events
In order to distinguish touch-related state changes, there are many type of touch event. You can determine what type the current event is by checking the TouchEvent.type property of the touch event- touchstart: Fires when the user places a touch point on the touch surface.
- touchend: Triggered when a touch point is removed from the touch surface by the user (when the user lifts a finger off the touch surface).
- touchmove: Triggered when the user moves a touch point on the touch plane.
- touchcancel: Triggered when the contact is interrupted for some reason.
Determine the sliding direction
The basic principle is to record the coordinates of the start sliding (touchStart) and the end sliding (touchEnd) position, and then perform relative position calculations.touchStart:function(e){ startX = e.touches[0].pageX; startY = e.touches[0].pageY; e = e || window.event; }, touchEnd:function(e){ const that = this; endX = e.changedTouches[0].pageX; endY = e.changedTouches[0].pageY; that.upOrDown(startX,startY,endX,endY); }, upOrDown:function (startX, startY, endX, endY) { const that = this; let direction = that.GetSlideDirection(startX, startY, endX, endY); switch(direction) { case 0: console.log("没滑动"); break; case 1: console.log("向上"); break; case 2: console.log("向下"); break; case 3: console.log("向左"); break; case 4: console.log("向右"); break; default: break; } }, //根据起点和终点返回方向 1:向上,2:向下,3:向左,4:向右,0:未滑动 GetSlideDirection:function (startX, startY, endX, endY) { const that = this; let dy = startY - endY; let dx = endX - startX; let result = 0; //如果滑动距离太短 if(Math.abs(dx) = -45 && angle = 45 && angle = -135 && angle = 135 && angle = -180 && angle <p style="text-align: left;"><span style="color: #ff0000">Native JS method<strong></strong></span></p>In addition to the new methods in H5, you can also use native JS to determine the sliding direction of the view. The code is as follows ( Can be run directly): <p style="text-align: left;"></p>It should be noted that chrome’s document.body.scrollTop is always 0 and needs to be changed to document.documentElement.scrollTop<p style="text-align: left;"></p><pre class="brush:php;toolbar:false">nbsp;html> <meta> <title> 脚本之家(jb51.net)</title> <style> p { border: 1px solid black; width: 200px; height: 100px; overflow: scroll; } </style> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <h1 id="HEllo-word">HEllo word</h1> <script> function scroll( fn ) { var beforeScrollTop = document.documentElement.scrollTop, fn = fn || function() {}; console.log('beforeScrollTop',beforeScrollTop); window.addEventListener("scroll", function() { var afterScrollTop = document.documentElement.scrollTop, delta = afterScrollTop - beforeScrollTop; console.log('beforeScrollTop',beforeScrollTop); console.log('afterScrollTop',afterScrollTop); if( delta === 0 ) return false; fn( delta > 0 ? "down" : "up" ); beforeScrollTop = afterScrollTop; }, false); } scroll(function(direction) { console.log(direction) }); </script>I believe you have mastered it after reading the case in this article. Method, for more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How to verify the email address format
The above is the detailed content of How to determine the user's sliding direction in H5 touch events. For more information, please follow other related articles on the PHP Chinese website!

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.