JavaScript 獲取滾動條位置并將頁面滑動到錨點
這篇來記錄下最近工作中遇到的一個問題,在app原生和前端h5混合開發(fā)的過程中,其中一個頁面是選擇城市列表的頁面,類似于美團餓了么城市選擇,銀行app中銀行列表選擇,通訊錄中快速定位到聯(lián)系人選擇的錨點位置等這樣的功能,作為剛?cè)腴T不久的我來說,感覺這個功能還是有一點壓力。下面我來分享一下我所查到的一些實現(xiàn)方法。
什么是錨點問題對于pc端網(wǎng)頁來說,常見的網(wǎng)頁右側(cè)的回到頂部按鈕,點擊直接跳轉(zhuǎn)到網(wǎng)頁最上面,就是錨點的實現(xiàn);
對于移動端來說,打開你手機的通訊錄,點擊右側(cè)的字母,頁面直接跳轉(zhuǎn)到對應(yīng)字母的聯(lián)系人,這也是錨點的實現(xiàn)。
常見的解決方法1.<a>標簽中href屬性設(shè)置為跳轉(zhuǎn)元素的id的值<style> #mydiv{ height: 1200px; width: 100%; background-color: pink; position: relative; } a{ position: absolute; top: 1000px; left: 1000px; } </style> <div id='mydiv'> 我是網(wǎng)頁頂部 </div> <a href='http://m.b3g6.com/bcjs/16418.html#mydiv' rel='external nofollow' >回到頂部</a>
上面的辦法相當于設(shè)置一個超鏈接,a標簽直接跳轉(zhuǎn),但是這樣回改變?yōu)g覽器地址欄中的地址,感覺不太實用
2.原生js獲取滾動條位置,并作出改變scrollTop<style> body{ position: relative; } h1{ margin: 0 auto; } .mybtn1{ position: fixed; left: 200px; top: 500px; } .mybtn2{ position: fixed; left: 200px; top: 550px; } </style><body> <h1 id='topH1'>1</h1> <h1>2</h1> <h1>3</h1> <h1>4</h1> <h1>5</h1> <h1>6</h1> <h1>7</h1> <h1>1</h1> <h1>2</h1> <h1>3</h1> <h1>4</h1> <h1>5</h1> <h1>6</h1> <h1>7</h1> <h1>1</h1> <h1>2</h1> <h1>3</h1> <h1>4</h1> <h1>5</h1> <h1>6</h1> <h1 id='tobtmH1'>7</h1> <button onclick='toTop()'>回到頂部</button> <script> function toTop(){ var topH1 = document.getElementById('topH1') document.documentElement.scrollTop=topH1.offsetTop window.pageYOffset=topH1.offsetTop document.body.scrollTop=topH1.offsetTop ; } </script> </body>
這種方法就是給按鈕添加點擊事件,觸發(fā)點擊事件后改變滾動條位置,但是這種辦法需要處理兼容型問題比較麻煩,pc端移動端親測有效。
3.element.scrollIntoview使得滾動條根據(jù)視圖發(fā)生變化<style> body{ position: relative; } .mydiv{ margin-top: 100px; border: 1px solid pink; } h1{ margin: 0 auto; } .mybtn1{ position: fixed; left: 200px; top: 500px; } .mybtn2{ position: fixed; left: 200px; top: 550px; }</style><body> <div class='mydiv'> <h1 id='topH1'>1</h1> <h1>2</h1> <h1>3</h1> <h1>4</h1> <h1>5</h1> <h1>6</h1> <h1>7</h1> <h1>1</h1> <h1>2</h1> <h1>3</h1> <h1>4</h1> <h1>5</h1> <h1>6</h1> <h1>7</h1> <h1>1</h1> <h1>2</h1> <h1>3</h1> <h1>4</h1> <h1>5</h1> <h1>6</h1> <h1 id='tobtmH1'>7</h1></div> <button onclick='toTop()'>回到頂部</button> <button onclick='toBtm()'>去到底部</button> <script> window.onload=function(){ } // 調(diào)用方法為element.scrollIntoview() //參數(shù)為true時,頁面或者容器發(fā)生滾動,使得element的頂部與視圖容器頂部對齊 //參數(shù)為false時,使得element的底部與視圖容器底部對齊 function toTop(){ var topH1 = document.getElementById(’topH1’) topH1.scrollIntoView(true) } function toBtm() { var tobtmH1 = document.getElementById(’tobtmH1’) tobtmH1.scrollIntoView(false) } </script> </body>
上面這種方法是將錨點跳轉(zhuǎn)到視圖的頂部或者底部,沒有太多弊端,pc端移動端親測有效。
進階的解決方法進階的方法就是調(diào)用第三發(fā)插件better-scroll,這種方法還沒有親測,查看資料也沒有太多的坑,需要的自己添加使用下。
以上就是JavaScript 獲取滾動條位置并將頁面滑動到錨點的詳細內(nèi)容,更多關(guān)于JavaScript 滾動條滑動到錨點的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP基礎(chǔ)知識VBScript基本元素講解2. Python 利用Entrez庫篩選下載PubMed文獻摘要的示例3. Python 制作查詢商品歷史價格的小工具4. Linux刪除系統(tǒng)自帶版本Python過程詳解5. Python3 json模塊之編碼解碼方法講解6. python 使用事件對象asyncio.Event來同步協(xié)程的操作7. Python sublime安裝及配置過程詳解8. Python 合并拼接字符串的方法9. Python插件機制實現(xiàn)詳解10. ASP.NET MVC使用jQuery ui的progressbar實現(xiàn)進度條

網(wǎng)公網(wǎng)安備