前言
這篇來記錄下最近工作中遇到的一個問題,在app原生和前端h5混合開發的過程中,其中一個頁面是選擇城市列表的頁面,類似于美團餓了么城市選擇,銀行app中銀行列表選擇,通訊錄中快速定位到聯系人選擇的錨點位置等這樣的功能,作為剛入門不久的我來說,感覺這個功能還是有一點壓力。下面我來分享一下我所查到的一些實現方法。
什么是錨點問題
對于pc端網頁來說,常見的網頁右側的回到頂部按鈕,點擊直接跳轉到網頁最上面,就是錨點的實現;
對于移動端來說,打開你手機的通訊錄,點擊右側的字母,頁面直接跳轉到對應字母的聯系人,這也是錨點的實現。
常見的解決方法
1.<a>標簽中href屬性設置為跳轉元素的id的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< style > #mydiv{ height: 1200px; width: 100%; background-color: pink; position: relative; } a{ position: absolute; top: 1000px; left: 1000px; } </ style > < div id = "mydiv" > 我是網頁頂部 </ div > < a href = "#mydiv" rel = "external nofollow" >回到頂部</ a > |
上面的辦法相當于設置一個超鏈接,a標簽直接跳轉,但是這樣回改變瀏覽器地址欄中的地址,感覺不太實用
2.原生js獲取滾動條位置,并作出改變scrollTop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<style> body{ position : relative ; } h 1 { margin : 0 auto ; } .mybtn 1 { position : fixed ; left : 200px ; top : 500px ; } .mybtn 2 { position : fixed ; left : 200px ; top : 550px ; } </style> <body> <h 1 id= "topH1" > 1 </h 1 > <h 1 > 2 </h 1 > <h 1 > 3 </h 1 > <h 1 > 4 </h 1 > <h 1 > 5 </h 1 > <h 1 > 6 </h 1 > <h 1 > 7 </h 1 > <h 1 > 1 </h 1 > <h 1 > 2 </h 1 > <h 1 > 3 </h 1 > <h 1 > 4 </h 1 > <h 1 > 5 </h 1 > <h 1 > 6 </h 1 > <h 1 > 7 </h 1 > <h 1 > 1 </h 1 > <h 1 > 2 </h 1 > <h 1 > 3 </h 1 > <h 1 > 4 </h 1 > <h 1 > 5 </h 1 > <h 1 > 6 </h 1 > <h 1 id= "tobtmH1" > 7 </h 1 > <button class= "mybtn1" onclick= "toTop()" >回到頂部</button> <script> function toTop(){ var topH 1 = document.getElementById( "topH1" ) document.documentElement.scrollTop=topH 1 .offsetTop window.pageYOffset=topH 1 .offsetTop document.body.scrollTop=topH 1 .offsetTop ; } </script> </body> |
這種方法就是給按鈕添加點擊事件,觸發點擊事件后改變滾動條位置,但是這種辦法需要處理兼容型問題比較麻煩,pc端移動端親測有效。
3.element.scrollIntoview使得滾動條根據視圖發生變化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<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 class= "mybtn1" onclick= "toTop()" >回到頂部</button> <button class= "mybtn2" onclick= "toBtm()" >去到底部</button> <script> window.onload= function (){ } // 調用方法為element.scrollIntoview() //參數為true時,頁面或者容器發生滾動,使得element的頂部與視圖容器頂部對齊 //參數為false時,使得element的底部與視圖容器底部對齊 function toTop(){ var topH1 = document.getElementById( 'topH1' ) topH1.scrollIntoView( true ) } function toBtm() { var tobtmH1 = document.getElementById( 'tobtmH1' ) tobtmH1.scrollIntoView( false ) } </script> </body> |
上面這種方法是將錨點跳轉到視圖的頂部或者底部,沒有太多弊端,pc端移動端親測有效。
進階的解決方法
進階的方法就是調用第三發插件better-scroll,這種方法還沒有親測,查看資料也沒有太多的坑,需要的自己添加使用下。
以上就是JavaScript 獲取滾動條位置并將頁面滑動到錨點的詳細內容,更多關于JavaScript 滾動條滑動到錨點的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/zaishiyu/p/14259049.html