


How to implement gesture sliding screen switching in HTML5 single page
This time I will show you how to implement HTML5 single page gesture sliding screen switching. How to implement HTML5 single page gesture sliding screen switching? What are the precautions for HTML5 single-page gesture sliding screen switching? The following is a practical case, let’s take a look.
H5 single page gesture sliding screen switching is implemented using HTML5 touch events (Touch) and CSS3 animation (Transform, Transition)
1. Implementation principle
Assume there are 5 pages, each page occupies 100% of the screen width, then create a DIV container viewport, set its width (width) to 500%, then load the 5 pages into the container, and let this Five pages divide the entire container equally. Finally, the default position of the container is set to 0 and the overflow is set to hidden, so that the screen displays the first page by default.
<div id="viewport" class="viewport"> <div class="pageview" style="background: #3b76c0" > <h3 >页面-1</h3> </div> <div class="pageview" style="background: #58c03b;"> <h3>页面-2</h3> </div> <div class="pageview" style="background: #c03b25;"> <h3>页面-3</h3> </div> <div class="pageview" style="background: #e0a718;"> <h3>页面-4</h3> </div> <div class="pageview" style="background: #c03eac;"> <h3>页面-5</h3> </div> </div>
CSS style:
.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 touchstart, touchmove and touchend events. When your finger slides on the screen, use CSS3 transform to set the position of the viewport in real time, for example, to display the second page, just set the viewport's transform: translate3d(100%,0,0). Here we use translate3d instead of translateX. translate3d can actively turn on the mobile phone's GPU to accelerate rendering, making the page slide more smoothly.
2. Main idea
It is a complete operation process from placing your finger on the screen, sliding operation, and then leaving the screen. The corresponding operation will trigger the following events:
Finger on the screen: ontouchstart
Finger sliding on the screen: ontouchmove
Finger off the screen: ontouchend
We need to capture these three stages of touch events to complete Page sliding:
ontouchstart: Initialize variables, record the position of the finger, record the current time
/*手指放在屏幕上*/ document.addEventListener("touchstart",function(e){ e.preventDefault(); var touch = e.touches[0]; startX = touch.pageX; startY = touch.pageY; initialPos = currentPosition; //本次滑动前的初始位置 viewport.style.webkitTransition = ""; //取消动画效果 startT = new Date().getTime(); //记录手指按下的开始时间 isMove = false; //是否产生滑动 }.bind(this),false);
ontouchmove: Get the current position, calculate the movement difference deltaX of the finger on the screen, and then Make the page follow the movement
/*手指在屏幕上滑动,页面跟随手指移动*/ document.addEventListener("touchmove",function(e){ e.preventDefault(); var touch = e.touches[0]; var deltaX = touch.pageX - startX; var deltaY = touch.pageY - startY; //如果X方向上的位移大于Y方向,则认为是左右滑动 if (Math.abs(deltaX) > Math.abs(deltaY)){ moveLength = deltaX; var translate = initialPos + deltaX; //当前需要移动到的位置 //如果translate>0 或 < maxWidth,则表示页面超出边界 if (translate <=0 && translate >= maxWidth){ //移动页面 this.transform.call(viewport,translate); isMove = true; } direction = deltaX>0?"right":"left"; //判断手指滑动的方向 } }.bind(this),false);
I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How to use video and audio tags and progress bars in H5
New interaction between H5 and C3 What are the features
The above is the detailed content of How to implement gesture sliding screen switching in HTML5 single page. For more information, please follow other related articles on the PHP Chinese website!

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

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
