HTML5 Touch and CSS3 Animations (Transform and Transition) are used to perform single page gesture Sliding Screen switching in H5. Next, we will introduce the principle of single page gesture Sliding Screen switching in HTML5 through this article, if you need a friend to learn it, H5 single page gesture Sliding Screen switching is implemented using HTML5 Touch event and CSS3 animation (Transform, Transition), as shown below, this article briefly describes its implementation principles and main ideas.
1. Implementation Principle
Suppose there are five pages, each of which occupies 100% of the screen width, create a DIV container viewport, set its width to 500%, and then load the five pages into the container, divide the five pages equally, and set the default location of the container to 0 and overflow to hidden, so that the screen displays the first page by default.
-
Page-1h3> p>
Page-2h3> p>
Page-3h3> p>
Page-4h3> p>
Page-5h3> p>
CSS style:
Copy XML/HTML Code to clipboard
-
.viewport{ width: 500%; height: 100%; display: -webkit-box; overflow: hidden; pointer-events: none; -webkit-transform: translate3d(0,0,0); backface-visibility: hidden; position: relative; }
Register the touchstart, touchmove, and touchend events. When the finger slides on the screen, use the CSS3 transform to set the position of the viewport in real time. For example, to display the second page, set the transform of the viewport: translate3d (100%, 0) is enough. Here we use translate3d to replace translateX. translate3d can automatically enable GPU-accelerated rendering on the mobile phone, making the page slide smoother.
2. Main Ideas
Moving your finger onto the screen, sliding, and leaving the screen is a complete operation. The corresponding operation triggers the following events:
Place your finger on the screen: ontouchstart
Slide your finger on the screen: ontouchmove
Finger off screen: ontouchend
We need to capture the three phases of the touch event to complete the page slide:
Ontouchstart: initializes the variable, records the position of the finger, and records the current time.
-
/* Place your finger on the screen */document. addEventListener ("touchstart", function (e) {e. preventDefault (); var touch = e. touches [0]; startX = touch. pageX; startY = touch. pageY; initialPos = currentPosition; // The initial position before the current slide. style. webkitTransition = ""; // cancel the animation effect startT = new Date (). getTime (); // record the start time isMove = false when the finger is pressed; // whether to slide }. bind (this), false );
Ontouchmove: obtains the current position, calculates the deltaX of the finger movement difference on the screen, and then moves the page following
Copy XML/HTML Code to clipboard
/* Slide your finger on the screen and move the page following your finger */document. addEventListener ("touchmove", function (e) {e. preventDefault (); var touch = e. touches [0]; var deltaX = touch. pageX-startX; var deltaY = touch. pageY-startY; // if the displacement in the X direction is greater than that in the Y direction, it is considered to be sliding left and right if (Math. abs (deltaX)> Math. abs (deltaY) {moveLength = deltaX; var translate = initialPos + deltaX; // The current location to be moved to // If translate> 0 or <maxWidth, if (translate <= 0 & translate> = MaxWidth) {// move the page this. transform. call (viewport, translate); isMove = true;} direction = deltaX> 0? "Right": "left"; // determines the direction in which the finger slides.}. bind (this), false );
Ontouchend: When the finger leaves the screen, it calculates the page on which the screen finally stays. First, calculate the finger stop time on the screen. If deltaT is less than 300 ms, it is considered to be a fast slide. On the contrary, it is a slow slide. The processing of Fast Slide and slow slide is different:
(1) if it is a quick slide, the current page will be completely stuck in the center of the screen (you need to calculate the number of slides required for the current page)
(2) if it is a slow slide, you also need to determine the sliding distance of your fingers on the screen. If the sliding distance does not exceed 50% of the screen width, You need to roll back to the previous page, instead, you must stay on the current page.
Copy XML/HTML Code to clipboard
/* When the finger leaves the screen, calculate the page on which it will eventually stay */document. addEventListener ("touchend", function (e) {e. preventDefault (); var translate = 0; // calculate the time when the finger stays on the screen var deltaT = new Date (). getTime ()-startT; if (isMove) {// Slide left and right // use the animated transition to slide the page to the final position viewport. style. webkitTransition = "0.3 s extract-webkit-transform"; if (deltaT <300) {// if the stay time is less than 300 ms, it is considered as a Fast Slide, no matter the sliding distance, all stay on the next page translate = direction = 'left '? CurrentPosition-(pageWidth + moveLength): currentPosition + pageWidth-moveLength; // if the final position exceeds the boundary position, it stays at the boundary position: translatetranslate = translate> 0? 0: translate; // left boundary translatetranslate = translate <maxWidth? MaxWidth: translate; // right boundary} else {// if the sliding distance is less than 50% of the screen, it is returned to the previous page if (Math. abs (moveLength)/pageWidth <0.5) {translate = currentPosition-moveLength;} else {// If the sliding distance is greater than 50% of the screen, then slide to the next page translate = direction = 'left '? CurrentPosition-(pageWidth + moveLength): currentPosition + pageWidth-moveLength; translatetranslate = translate> 0? 0: translate; translatetranslate = translate <maxWidth? MaxWidth: translate ;}// run the slide command to display the page completely to the screen. this. transform. call (viewport, translate );}}. bind (this), false );
In addition, you must calculate the page number of the current page and set the current page number.
// Calculate the current page number pageNow = Math. round (Math. abs (translate)/pageWidth) + 1; setTimeout (function () {// set the page number. DOM operations need to be placed in the Child thread; otherwise, this will occur. setPageNow ();}. bind (this), 100 );
These are the basic ideas. Of course, you need to pay attention to some details in the actual operation process. I will not elaborate on them here, and they will all be reflected in the Code. I will introduce you to the HTML5 single-page gesture Sliding Screen switching principle. I hope it will be helpful to you!
The above is the HTML5 single page gesture Sliding Screen Switching Principle _ html5 tutorial skills content, more relevant content please follow the PHP Chinese Network (www.php1.cn )!