After I finished reading JavaScript DOM, I had a deeper understanding of the interpreted JavaScript scripting language, and made my JavaScript code more standardized. Next, let’s move on to the technical issue I want to share with you today: scrolling on a full-screen page. The implemented code is very simple, but discovering the problems therein requires long-term experience, reading experience, and the programmer's rich imagination. Let's first take a look at the two final renderings and the content printed by console.log: 1. Click on page 2 effect and print result: After clicking, a string of values printed in the pagelist[this.index].rollCount timer is Math.ceil(rollData.num); after 5 seconds, another counter is automatically executed to clear the pagelist[this.index].rollCount timer. 2. Click Page 2 again, and the printed result: After clicking, it will first check whether the pagelist[this.index].rollCount timer exists, and if it does, it will be cleared; if the page corresponding to the click has been reached, it will print out that it is not executed and return. Please see the examples below for details, where various situations have been explained in detail. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Full screen page turning effect</title> <style> * { padding: 0; margin: 0; } html, body { width: 100%; height: 100%; color: #fff; } ul { list-style: none } #nav { position: fixed; top: 50px; left: 50px; } #nav li { width: 80px; height: 50px; text-align: center; line-height: 50px; border: 2px solid #fff; cursor: pointer; } #nav li:nth-child(1) { background: #f60; } #nav li:nth-child(2) { background: #63c; } #nav li:nth-child(3) { background: #3c6; } #nav li:nth-child(4) { background: #f9c; } #page { width: 100%; height: 100%; } #page li { width: 100%; height: 100%; } </style> </head> <body> <ul id="page"> <li></li> <li></li> <li></li> <li></li> </ul> <ul id="nav"> <li>Page 1</li> <li>Page 2</li> <li>Page 3</li> <li>Page 4</li> </ul> <script> function rollingPage() { var pageul = document.getElementById("page"); var pagelist = pageul.children; var navul = document.getElementById("nav"); var navlist = navul.children; for (i = 0; i < navlist.length; i++) { //Get all styles getComputedStyle var bgcolor = getComputedStyle(navlist[i], "").backgroundColor; // alert(bgcolor); pagelist[i].style.background = bgcolor; //Define an index object for the current element and save the subscript of the current element navlist[i].index = i; //Declare variables as objects var rollData = { num: 0, target: 0 }; navlist[i].onclick = function () { //The distance between the page corresponding to the clicked button and the top of the entire page rollData.target = pagelist[this.index].offsetTop; //Determine whether the clicked element is the current one. If so, do not continue executing var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; var x = this.index; //Judge whether the page corresponding to the currently clicked button has an element attribute timer if (pagelist[this.index].rollCount) { console.log("exists"); clearInterval(pagelist[this.index].rollCount); /*Prevent continuous clicks from causing direct return and causing the page to not load completely, so add a judgment to prevent it. * In this way, even if the page is not fully reached, even after the above is cleared, *The counter pagelist[this.index].rollCount will also be executed downwards * */ /*But I found that things will never be perfect and need to be constantly improved. *At the moment when I was chatting with my friend, I clicked the button corresponding to the current page again. *But I found that the return below was not executed. *Look again at the value of Math.ceil(rollData.num) printed in the counter, which is 1. *Therefore, Math.ceil(rollData.num) + 1 == h * x || *Math.ceil(rollData.num) - 1 == h * x * * Another reason for this is that, * at window.scrollTo(0, Math.ceil(rollData.num) + 1) * and after subtracting one and not adding alkali, the saved value is Math.ceil(rollData.num) without adding alkali. * */ if (Math.ceil(rollData.num) + 1 == h * x || Math.ceil(rollData.num) - 1 == h * x || Math.ceil(rollData.num) == h * x) { console.log("Not executed"); //If it exists and the value the scroll bar scrolls to is equal to the current page, //The counter is not executed downwards. return; } } //Timer to scroll the page pagelist[this.index].rollCount = setInterval(function () { //Math.ceil() rounds in the direction of greater than rollData.num = rollData.num + (rollData.target - rollData.num) / 10; console.log(Math.ceil(rollData.num)); //1. Move the scroll bar to the position of h*x, window.scrollTo() //2. Determine the case where the absolute value of the rollData.num attribute after being taken by Math.ceil is 1 before and after x*h. //The reason for the above 2 operations is that when printing the value of Math.ceil(rollData.num), it is found that sometimes //the difference between the value before and after x*h is 1 if (Math.ceil(rollData.num) + 1 == h * x) { window.scrollTo(0, Math.ceil(rollData.num) + 1); } else if (Math.ceil(rollData.num) - 1 == h * x) { window.scrollTo(0, Math.ceil(rollData.num) - 1); } else { window.scrollTo(0, Math.ceil(rollData.num)); } }, 30); /*After 5 seconds, if the conditions are met, the pagelist[x].rollCount counter can be cleared to prevent the upper counter from running continuously* */ setTimeout(function () { //After five seconds, if Math.ceil(rollData.num) and the difference between the previous and the next is 1, // Clear the timer above if (Math.ceil(rollData.num) + 1 == h * x || Math.ceil(rollData.num) - 1 == h * x || Math.ceil(rollData.num) == h * x) { console.log("Automatically clear" + x); clearInterval(pagelist[x].rollCount); } }, 5000); } } } addLoadEvent(rollingPage); function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function () { oldonload(); func(); } } } </script> </body> </html> After completing the above optimization, I found that I still need to read more books in the future, go to open source websites to read codes written by experts, and constantly use my imagination to improve my own code. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to set the width and height of html table cells
>>: Detailed Analysis of the Selection of MySQL Common Index and Unique Index
Table of contents Preface 1. Routing lazy loading...
We will install phpMyAdmin to work with Apache on...
Set a background image for the table. You can use...
Preface After a long time of reading various mate...
The previous three articles introduced common bac...
The CSS counter attribute is supported by almost ...
Recently the company has arranged to do some CCFA...
1. Before configuring the IP address, first use i...
<br />My previous article about CSS was not ...
All the following codes are between <head>.....
Password Mode PDO::__construct(): The server requ...
Nine simple examples analyze the use of HTML form...
Software Version Windows: Windows 10 MySQL: mysql...
meta is an auxiliary tag in the head area of htm...
cause I recently started to refactor the project,...