aforementioned This article is very short~ textI recently did a small activity, which required an effect: sliding to turn pages . It's probably like this: <!-- HTML code --> <div class="page-container"> <div class="page" style="background: #dc3b26">1</div> <div class="page" style="background: #f3b03d">2</div> <div class="page" style="background: #44a2f8">3</div> </div> In CSS, first of all, because we are sliding to turn the pages, we must ensure that "there is always only one screen", which can be controlled in the global style sheet , and then "each page" must occupy the entire parent element - here we actually use the feature that "div is a block element, arranged vertically without external force". /** CSS style */ html, body{ margin: 0; padding: 0; width: 100%; height: 100vh; overflow: hidden; font-size: 60px; } .page-container{ width: 100%; height: 100%; } .page-container .page{ width: 100%; height: 100%; } Its JS implementation is also very simple: because it is on the mobile terminal,
// Here is the file that controls the global js // Globally prevent the browser's default behavior document.addEventListener("touchstart",function(e){ if(e.cancelable){ e.preventDefault(); } },{passive: false}) // {passive: false} tells the previous code that there may be a need to reset the behavior document.addEventListener("touchmove",function(e){ if(e.cancelable){ e.preventDefault(); } },{passive: false}) // JavaScript code let curPageIndex=0; let pageContainer = document.querySelector(".page-container"); let pageNumber=pageContainer.children.length; //Number of pages// Document window height (here is the height of one screen) let cHeight=document.documentElement.clientHeight; // Set the margin-top of the page container to an appropriate value so that it appears in view function toPage(){ pageContainer.style.transition="all .5s ease"; pageContainer.style.marginTop=-curPageIndex*cHeight+"px"; // Remove the transition effect after the change is completed! pageContainer.addEventListener("transitionend",function(){ pageContainer.style.transition="none"; },{once:true}) } toPage() pageContainer.ontouchstart=function(e){ let y=e.changedTouches[0].clientY; //The vertical coordinate of the finger pressed pageContainer.ontouchmove=function(e){ let dis=e.changedTouches[0].clientY-y; //Calculate distance // Calculate margin-top of page-container let mtop=-curPageIndex*cHeight+dis if(mtop>0){ mtop=0; }else if(mtop<-(pageNumber-1)*cHeight){ mtop=-(pageNumber-1)*cHeight; } //Change position in real time pageContainer.style.marginTop=mtop+"px"; } pageContainer.ontouchend=function(e){ let dis = e.changedTouches[0].clientY-y; // If the sliding distance is too short, return to the position before sliding if(Math.abs(dis)<=60){ toPage() }else if(dis>0 && curPageIndex>0){ curPageIndex--; toPage() }else if(dis<0 && curPageIndex<pageNumber-1){ curPageIndex++; toPage() } // After the finger leaves, cancel the monitoring event pageContainer.ontouchmove=null; pageContainer.ontouchend=null; } } So far, functionality seems perfect. But this time, we add a button to the first page: <div class="page" style="background: #dc3b26"> <button onclick="alert('哈哈哈哈')" class="but">click me!</button> 1 </div> Then go to the page to see the effect: invalid! This is because we added the code to "prevent browser default events" in the global js file above. But as shown above, banning everything will always cause some troubles. What should we do? <button data-default="true" onclick="alert('哈哈哈哈')" class="but">click me!</button> Change the content of the above "File that controls global js" to the following: // Globally prevent browser default behavior document.addEventListener("touchstart",function(e){ if(e.target.dataset.default){ return; } if(e.cancelable){ e.preventDefault(); } },{passive: false}) document.addEventListener("touchmove",function(e){ if(e.target.dataset.default){ return; } if(e.cancelable){ e.preventDefault(); } },{passive: false}) It's OK: In fact, there is another "solution": As mentioned above, the mobile click is actually simulated by the mouse event, so we can start with the mousestart event; and because the button element is a (child) element in the "first page", we can use the method of preventing the event from bubbling : <!-- button is a normal button --> <button class="but">click me!</button> document.querySelector(".but").addEventListener("touchstart",function(e){ e.stopPropagation(); alert('嘎哈哈'); },false)
This is the end of this article about the implementation of sliding page turning effects and click event issues on mobile terminals. For more relevant content on sliding page turning effects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Linux platform mysql enable remote login
>>: Talk about important subdirectory issues in Linux system
Use HSSFWorkbook in Apache.POI to export to Excel...
RPM package management A packaging and installati...
This article shares the specific code of Bootstra...
background We can use react-color to implement th...
In an article a long time ago, I talked about the...
The innodb_autoinc_lock_mode parameter controls t...
Let's take a look at the process of installin...
Preface: Fully encapsulating a functional module ...
Cascading and Cascading Levels HTML elements are ...
Scenario Yesterday the system automatically backe...
What is a generator? A generator is some code tha...
Table of contents 1. Introduction: 2. Prototype c...
1. HTML font color setting In HTML, we use the fo...
Table of contents 1. Understanding the stack stru...
1. Background I recently made a website, uidea, w...