1. Implementation Background Last week, in an activity where users completed tasks and received red envelopes, we needed to implement a pop-up window for receiving red envelopes after the user clicked a button. When the pop-up window was closed and the user returned to the original page, the balance number on the page had to display the effect of scrolling each digit. 2. Implementation ideas In fact, it is to split the incoming n-digit number with rolling into each number to be rolled, and then dynamically create a container containing the corresponding numbers rolled to each digit, and then put it into the incoming target container. Scrolling to each corresponding number can be achieved by dynamically creating 3. Implementation process Since we need to encapsulate it, let's write it in the form of /** * Class that implements the digital scrolling effect*/ class DigitScroll { constructor(options) { //Get the container's DOM. If not, an error is thrown this.container = document.querySelector(options.container); if (!this.container) { throw Error("no container"); } this.container.style.overflow = "hidden"; this.container.style.display = "flex"; //The height of the visible container is also the scrolling interval. The height of the container must be set, otherwise the default is 30px this.rollHeight = parseInt(getComputedStyle(this.container).height) || 30; this.container.style.height = this.rollHeight + "px"; } roll(num) { // Split the number passed in to be scrolled and initialize the container of each digit this.initDigitEle(num); const numEles = this.container.querySelectorAll(".single-num"); // Traverse and generate the scroll queue for each digit. For example, if the scroll reaches 7, generate 7 divs with contents of 0, 1, 2, 3, 4, 5, 6, and 7 for scrolling animation [...numEles].forEach((numEle, index) => { const curNum = 0; let targetNum = Number(this.numberArr[index]); if (curNum >= targetNum) { targetNum = targetNum + 10; } let cirNum = curNum; // Document fragments, put together and insert into the node at once const fragment = document.createDocumentFragment(); // Generate divs corresponding to the target number from 0 to while (targetNum >= cirNum) { const ele = document.createElement("div"); ele.innerHTML = cirNum % 10; cirNum++; fragment.appendChild(ele); } numEle.innerHTML = ""; numEle.appendChild(fragment); //Reset position numEle.style.cssText += "-webkit-transition-duration:0s;-webkit-transform:translateY(0)"; setTimeout(() => { numEle.style.cssText += `-webkit-transition-duration:1s;-webkit-transform:translateY(${ -(targetNum - curNum) * this.rollHeight }px);`; }, 50); }); } // Initialize the container initDigitEle(num) { //Number splitting digits const numArr = num.toString().split(""); // Document fragments, put together and insert into the node at once const fragment = document.createDocumentFragment(); numArr.forEach((item) => { const el = document.createElement("div"); // Numbers are scrolled, non-numbers such as . are not scrolled if (/[0-9]/.test(item)) { el.className = "single-num"; el.style.height = this.rollHeight + "px"; el.style.lineHeight = this.rollHeight + "px"; } else { el.innerHTML = item; el.className = "no-move"; el.style.verticalAlign = "bottom"; } // el.style.float='left'; fragment.appendChild(el); }, []); this.container.innerHTML = ""; this.container.appendChild(fragment); //Store the scrolling numbers this.numberArr = numArr.filter((item) => /[0-9]/.test(item)); } } This is the end of this article about how to use JavaScript to achieve scrolling balance numbers. For more information about how to use JavaScript to achieve scrolling balance numbers, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: border-radius is a method for adding rounded borders to elements
1. Introduction Oracle has released MySQL 8.0GA. ...
Preface In Windows, you can start multiple MySQL ...
1. Modify the firewall settings and open the corr...
Preface As we all know, by default, the MySQL ins...
Docker container connection 1. Network port mappi...
Table of contents The effect of mixed inheritance...
0x00 Introduction WordPress is the most popular C...
“How to make a website look high-end? Or more des...
1. Node server setup + database connection The op...
The first one: normal operation SELECT SUM(ddd) A...
Table of contents 1. exists 1.1 Description 1.2 E...
Table of contents 1. React.Children.map 2. React....
Samba Overview Samba is a free software that impl...
This article uses examples to illustrate the use ...