JavaScript 獲取滾動(dòng)條位置并將頁面滑動(dòng)到錨點(diǎn)
這篇來記錄下最近工作中遇到的一個(gè)問題,在app原生和前端h5混合開發(fā)的過程中,其中一個(gè)頁面是選擇城市列表的頁面,類似于美團(tuán)餓了么城市選擇,銀行app中銀行列表選擇,通訊錄中快速定位到聯(lián)系人選擇的錨點(diǎn)位置等這樣的功能,作為剛?cè)腴T不久的我來說,感覺這個(gè)功能還是有一點(diǎn)壓力。下面我來分享一下我所查到的一些實(shí)現(xiàn)方法。
什么是錨點(diǎn)問題對(duì)于pc端網(wǎng)頁來說,常見的網(wǎng)頁右側(cè)的回到頂部按鈕,點(diǎn)擊直接跳轉(zhuǎn)到網(wǎng)頁最上面,就是錨點(diǎn)的實(shí)現(xiàn);
對(duì)于移動(dòng)端來說,打開你手機(jī)的通訊錄,點(diǎn)擊右側(cè)的字母,頁面直接跳轉(zhuǎn)到對(duì)應(yīng)字母的聯(lián)系人,這也是錨點(diǎn)的實(shí)現(xiàn)。
常見的解決方法1.<a>標(biāo)簽中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://www.aoyou183.cn/bcjs/16418.html#mydiv' rel='external nofollow' >回到頂部</a>
上面的辦法相當(dāng)于設(shè)置一個(gè)超鏈接,a標(biāo)簽直接跳轉(zhuǎn),但是這樣回改變?yōu)g覽器地址欄中的地址,感覺不太實(shí)用
2.原生js獲取滾動(dòng)條位置,并作出改變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>
這種方法就是給按鈕添加點(diǎn)擊事件,觸發(fā)點(diǎn)擊事件后改變滾動(dòng)條位置,但是這種辦法需要處理兼容型問題比較麻煩,pc端移動(dòng)端親測(cè)有效。
3.element.scrollIntoview使得滾動(dòng)條根據(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時(shí),頁面或者容器發(fā)生滾動(dòng),使得element的頂部與視圖容器頂部對(duì)齊 //參數(shù)為false時(shí),使得element的底部與視圖容器底部對(duì)齊 function toTop(){ var topH1 = document.getElementById(’topH1’) topH1.scrollIntoView(true) } function toBtm() { var tobtmH1 = document.getElementById(’tobtmH1’) tobtmH1.scrollIntoView(false) } </script> </body>
上面這種方法是將錨點(diǎn)跳轉(zhuǎn)到視圖的頂部或者底部,沒有太多弊端,pc端移動(dòng)端親測(cè)有效。
進(jìn)階的解決方法進(jìn)階的方法就是調(diào)用第三發(fā)插件better-scroll,這種方法還沒有親測(cè),查看資料也沒有太多的坑,需要的自己添加使用下。
以上就是JavaScript 獲取滾動(dòng)條位置并將頁面滑動(dòng)到錨點(diǎn)的詳細(xì)內(nèi)容,更多關(guān)于JavaScript 滾動(dòng)條滑動(dòng)到錨點(diǎn)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析2. 一個(gè) 2 年 Android 開發(fā)者的 18 條忠告3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程6. Android studio 解決logcat無過濾工具欄的操作7. 什么是Python變量作用域8. vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼9. Spring的異常重試框架Spring Retry簡(jiǎn)單配置操作10. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼
