mobile-optimized list scrolling performance--paging load
Source: Internet
Author: User
<span id="Label3"></p><p><p>Now in the mobile phone list scrolling everywhere, take now everyone is in use, the circle of Friends is a list, and friends hair Information interface is a List.</p></p><p><p>If the list is not long, for example, your friend Circle has only two or three people sharing their news, That's fine, if you are a Big-screen mobile phone is not expected to scroll already finished.</p></p><p><p>But is that possible? There are also only a few. We now brush the circle of friends every day that is a few screen several screen such Brush.</p></p><p><p>We set a screen with five people to share the dynamic, each dynamic inside there are 5 Dom nodes, that is, a screen with 25 Dom Nodes.</p></p><p><p>We began to brush the circle of friends, only to see the four screen is already 100 nodes, and then brush down to hundreds or even thousands of DOM nodes are out, then your phone is not card into cheung.</p></p><p><p>But do we actually brush our friends ' circles more and more down the card? No. Why is it?</p></p><p><p><strong>That's what we're talking about today. a method to optimize scrolling performance of mobile phone-side list--paging load</strong></p></p><p><p>What is page load, basically means to divide a list into many pages, I just show you a few pages (N) at the beginning, when I scroll down to the bottom, the next page, and the top page is Removed.</p></p><p><p>similarly, when scrolling up, the previous page is displayed, and the bottom page is Removed. This way, the display will always have only n pages, and DOM nodes will always be only Nx25.</p></p><p><p>So what are the issues that need to be handled by paging?</p></p><p><p>1. Scroll down to remove the topmost page, and then when I want to scroll up, the previous page has been removed, how can I get to the previous page of the content?</p></p><p><p>2. Since there is a DOM node removed and added, then my scrollbar position to remove the page at the same time how to set up to make the user look like a seamless connection?</p></p><p><p>3. How can I tell if I have scrolled to the top or bottom of the page?</p></p><p><p>4. When scrolling to the bottom or top, what exactly do we want to do?</p></p><p><p>We will solve each other.</p></p><p><p>The first problem is that we only need to use two arrays to access the removed content, ok.</p></p><pre><pre><span style="color: #0000ff;">var</span> <span style="color: #008000;">//</span> the <span style="color: #008000;">page before the current page that was removed</span> <span style="color: #0000ff;">var</span> <span style="color: #008000;">//</span> the <span style="color: #008000;">page that is behind the current page that is removed</span></pre></pre><p><p>When we want to remove the previous page, we just need to get the first page of html, push it into the preremovearray. At the same time if there is content in Nextremovearray (meaning that you have browsed through the following), you only need to perform</p></p><pre><pre>Nextremovearray.pop (nextremovearray.length-1);</pre></pre><p><p>You can get to the next Page.</p></p><p><p></p></p><p><p>The second question, about the position of the scroll bar, is in two Cases.</p></p><p><p>1. Pull Down</p></p><p><p>We get the position of the current scroll bar by document.body.scrollTop, because pull down to remove the contents of the first page, so</p></p><p><p>Document.body.scrollTop = Document.body.scrollTop-height of the first page</p></p><p><p>2. Pull Up</p></p><p><p>Let's say we now have page 2nd, page 3, scrolling to the top, Document.body.scrollTop = 0, Loading the contents of the previous page, and after removing page 3rd, page 1th, page 2,</p></p><p><p>Our scroll bars are set at the top of page 2nd so that the user looks like a seamless connection, so</p></p><p><p>Document.body.scrollTop = document.body.scrollTop + height of first page</p></p><p><p></p></p><p><p>3. Judging whether the top is simple</p></p><pre><pre><span style="color: #0000ff;">var</span> <span style="color: #0000ff;">function</span> <span style="color: #000000;">() { </span><span style="color: #0000ff;">return</span> document.body.scrollTop = = = 0<span style="color: #000000;">;};</span></pre></pre><p><p>Return true is to the top</p></p><p><p>Determine if you need to get a few values to the bottom</p></p><p><p>One is document.body.scrollTop, the position of the current scroll bar</p></p><p><p>One is window.screen.height, the height of the current screen</p></p><p><p>One is document.body.clientHeight, the total height of the current entire page</p></p><p><p>Only when Document.body.scrollTop + window.screen.height = document.body.clientHeight, i.e.</p></p><pre><pre><span style="color: #0000ff;">var</span> <span style="color: #0000ff;">function</span> <span style="color: #000000;">() { </span><span style="color: #0000ff;">return</span> document.body.scrollTop + window.screen.height = =<span style="color: #000000;"> document.body.clientHeight;};</span></pre></pre><p><p>When True is returned, it scrolls to the Bottom.</p></p><p><p></p></p><p><p>4. When scrolling to the top, we need to determine whether there is a value in the preremovearray, there is a page in front, no means that this is the first page, the first page we do not need to do any processing.</p></p><p><p>If it's not the first page, we're going to do</p></p><p><p>1). gets the last page in the current list, and nextremovearray into the array</p></p><p><p>2). Add the contents of the previous page into the list</p></p><p><p>3). set the position of the scroll bar</p></p><p><p>4). Remove the last page</p></p><p><p>Scrolling down is also the same truth, but whether there is value in the Nextremovearray we have to do the corresponding Operation.</p></p><p><p>If you have values, go to the previous page of Nextremovearray to add the contents to the List.</p></p><p><p>Without a value, a new node is generated and added to the List.</p></p><p><p></p></p><p><p>The final code is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>PAGE = 1;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">number <span style="color: #008000;">of initialization pages</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>PAGESIZE = 5;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">show several data per page</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>$list = $ (' #list ');<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">List</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Preremovearray = [];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">page before the current page that was removed</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Nextremovearray = [];<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">page that is behind the current page that is removed</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>init =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() {initpage (); Initevent ();};</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Initpage =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() {renderData ();};</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Initevent =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span><span style="color: #000000;"><span style="color: #000000;">$page; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span><span style="color: #000000;"><span style="color: #000000;">length; Window.onscroll</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(istop ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(preremovearray[0<span style="color: #000000;"><span style="color: #000000;">]) {length</span></span>=<span style="color: #000000;"><span style="color: #000000;">preremovearray.length; $page</span></span>= $ ('. Page ') [$ ('. page '). length-1]);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the last page displayed in the current list</span></span>Nextremovearray.push ($page [0].outerhtml);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the last page of content array</span></span>$list. prepend (preremovearray.pop (length-1));<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add previous page content to list</span></span>Document.body.scrollTop = Document.body.scrollTop + $ ($ ('. page ') [0]). height ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set scroll bar position</span></span>$page. Remove ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Remove Last page</span></span><span style="color: #000000;"><span style="color: #000000;"> } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isbottom ()) {$page</span></span>= $ ($ ('. page ') [0]);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the first page shown in the current list</span></span>Preremovearray.push ($page [0].outerhtml);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the first page of content array</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(nextremovearray[0]) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If you have browsed the following content</span></span>Length =<span style="color: #000000;"><span style="color: #000000;">nextremovearray.length; $list. Append (nextremovearray.pop (length</span></span>-1));<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Add the next page of content into the list</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span>{<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If you have not browsed the following content</span></span><span style="color: #000000;"><span style="color: #000000;">Rendersinglepage (); } Document.body.scrollTop</span></span>= Document.body.scrolltop-$page. Height ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set scroll bar position</span></span>$page. Remove ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Remove First page</span></span><span style="color: #000000;"><span style="color: #000000;"> } };};</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>RenderData =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>InnerHTML = ' '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">I set it to always show only two pages, so we start by loading two pages of data First.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>i = 0; I < 2; i++<span style="color: #000000;"><span style="color: #000000;">) {innerHTML</span></span>+=<span style="color: #000000;"><span style="color: #000000;">GetData (); } $list. Append (innerHTML);};</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Rendersinglepage =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>InnerHTML = ' '<span style="color: #000000;"><span style="color: #000000;">; InnerHTML</span></span>+=<span style="color: #000000;"><span style="color: #000000;">GetData (); $list. Append (innerHTML);};</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>GetData =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>InnerHTML = ' '<span style="color: #000000;"><span style="color: #000000;">; InnerHTML</span></span>+ = ' <div class= ' page page-' + page + ' "> '<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>i = 0; I < PAGESIZE; i++<span style="color: #000000;"><span style="color: #000000;">) {innerHTML</span></span>+ = ' <li> ' + PAGE + ' </li> ';<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">for the convenience of seeing clearly, we give each line of data a mark that it belongs to the first page of the</span></span><span style="color: #000000;"><span style="color: #000000;">} InnerHTML</span></span>+ = ' </div> '<span style="color: #000000;"><span style="color: #000000;">; PAGE</span></span>++<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">innerHTML;};</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Isbottom =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Document.body.scrollTop + Window.screen.height = =<span style="color: #000000;"><span style="color: #000000;">document.body.clientHeight;};</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Istop =<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">() { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Document.body.scrollTop = = 0<span style="color: #000000;"><span style="color: #000000;">;}; Init ();</span></span></pre><p><p></p></p><p><p>PS: to facilitate development, the above code introduces the Zepto.js</p></p><p><p>mobile-optimized list scrolling performance--paging load</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service