今天小穎給你們分享下怎麼用IScroll實現下拉刷新,上拉加載更多,而且,當我點擊每一個 li 下的 a 標籤後,頁面跳轉到另外一個頁面,在新頁面中點擊回退,實現頁面返回上一個頁面而且,回滾到你剛點擊的哪一個位置。javascript
頁面效果:css
1.下載iscroll.jshtml
2.html+css+jsjava
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <title>iScroll demo: scrollbars</title> <style type="text/css"> * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } a { text-decoration: none; } html { -ms-touch-action: none; } body, ul, li { padding: 0; margin: 0; border: 0; } body { font-size: 12px; font-family: ubuntu, helvetica, arial; overflow: hidden; /* this is important to prevent the whole page to bounce */ } #header { position: absolute; z-index: 2; top: 0; left: 0; width: 100%; height: 45px; line-height: 45px; background: #CD235C; padding: 0; color: #eee; font-size: 20px; text-align: center; font-weight: bold; } #footer { position: absolute; z-index: 2; bottom: 0; left: 0; width: 100%; height: 48px; background: #444; padding: 0; border-top: 1px solid #444; } #wrapper { position: absolute; z-index: 1; top: 45px; bottom: 48px; left: 0; width: 100%; background: #ccc; overflow: hidden; } #scroller { position: absolute; z-index: 1; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 100%; -webkit-transform: translateZ(0); -moz-transform: translateZ(0); -ms-transform: translateZ(0); -o-transform: translateZ(0); transform: translateZ(0); -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; -webkit-text-size-adjust: none; -moz-text-size-adjust: none; -ms-text-size-adjust: none; -o-text-size-adjust: none; text-size-adjust: none; } #scroller ul { list-style: none; padding: 0; margin: 0; width: 100%; text-align: left; } #scroller li { padding: 0 10px; height: 40px; line-height: 40px; border-bottom: 1px solid #ccc; border-top: 1px solid #fff; background-color: #fafafa; font-size: 14px; } </style> </head> <body onload="loaded()"> <div id="header">iScroll</div> <div id="wrapper"> <div id="scroller"> <ul> <li> <a href="src/a_1.html">Pretty row 1</a> </li> <li><a>Pretty row 2</a></li> <li><a>Pretty row 3</a></li> <li><a>Pretty row 4</a></li> <li><a href="src/a_1.html">Pretty row 5</a></li> <li><a>Pretty row 6</a></li> <li><a>Pretty row 7</a></li> <li><a>Pretty row 8</a></li> <li><a>Pretty row 9</a></li> <li><a href="src/a_1.html">Pretty row 10</a></li> <li><a>Pretty row 11</a></li> <li><a>Pretty row 12</a></li> <li><a>Pretty row 13</a></li> <li><a>Pretty row 14</a></li> <li><a>Pretty row 15</a></li> <li><a>Pretty row 16</a></li> <li><a href="src/a_1.html">Pretty row 17</a></li> <li><a>Pretty row 18</a></li> <li><a>Pretty row 19</a></li> <li><a>Pretty row 20</a></li> <li><a>Pretty row 21</a></li> <li><a>Pretty row 22</a></li> <li><a>Pretty row 23</a></li> <li><a href="src/a_1.html">Pretty row 24</a></li> <li><a>Pretty row 25</a></li> <li><a>Pretty row 26</a></li> <li><a>Pretty row 27</a></li> <li><a>Pretty row 28</a></li> <li><a>Pretty row 29</a></li> <li><a>Pretty row 30</a></li> <li><a>Pretty row 31</a></li> <li><a>Pretty row 31</a></li> <li><a>Pretty row 33</a></li> <li><a>Pretty row 34</a></li> <li><a href="src/a_1.html">Pretty row 35</a></li> <li><a>Pretty row 36</a></li> <li><a>Pretty row 37</a></li> <li><a>Pretty row 38</a></li> <li><a>Pretty row 39</a></li> <li><a>Pretty row 40</a></li> <li><a>Pretty row 41</a></li> <li><a>Pretty row 42</a></li> <li><a>Pretty row 43</a></li> <li><a>Pretty row 44</a></li> <li><a>Pretty row 45</a></li> <li><a>Pretty row 46</a></li> <li><a>Pretty row 47</a></li> <li><a href="src/a_1.html">Pretty row 48</a></li> <li><a>Pretty row 49</a></li> <li><a>Pretty row 50</a></li> <li><a>Pretty row 51</a></li> <li><a>Pretty row 52</a></li> <li><a>Pretty row 53</a></li> </ul> </div> </div> <div id="footer"></div> </body> <script type="text/javascript" src="js/iscroll.js"></script> <script type="text/javascript"> var myScroll; // 判斷瀏覽器是否支持localStorage if (window.Storage && window.localStorage && window.localStorage instanceof Storage) { var storage = window.localStorage; } //獲取全部li的節點 var list = document.querySelectorAll('li a'); //給每一個li綁定事件 for (var i = 0; i < list.length; i++) { list[i].id='li_'+(i+1);//動態給每一個a標籤添加id list[i].onclick = function(i) {//動態給每一個a標籤添加onclick事件 if(this.id){ // 記錄當前a的位置 storage.setItem("li_index", this.id.substring(3)); } }; } function loaded() { myScroll = new IScroll('#wrapper', { scrollbars: true, mouseWheel: true, interactiveScrollbars: true, shrinkScrollbars: 'scale', fadeScrollbars: true }); var li_index = storage.getItem("li_index"); if (li_index) { li_index = parseInt(li_index); // 實現回滾 myScroll.scrollToElement(document.querySelector('#scroller li:nth-child('+li_index+')'), null, null, true); } } </script> </html>
3.a_1.htmlweb
<!DOCTYPE html> <html> <head> <title>詳情</title> </head> <body> <button onclick="history.go(-1)">後退</button> </body> </html>