我们上网经常看到一些页面层级指示的效果,比如淘宝、京东、百度百科等,用于指示用户浏览页面到哪里了。可以起到很好的交互效果,现在我们就来自己去实现以下类似的效果。主要用到的api是scrollintoview 和 getboundingclientrect方法,原理是监听页面滚动元素,当元素距离到浏览器视口一定的位置的时候就证明元素开始出现在页面的视口,就可以标识右边的指示栏。
代码如下:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<!doctype html>
<head>
<meta http-equiv= "content-type" content= "text/html; charset=utf-8" />
<title>h5 canvas绘制框选截图和保存截图代码</title>
<style>
*{padding:0;margin:0;}
#scrollbox{margin:0 auto;width:1000px;}
#scrollbox .scroll-tips{position:fixed;right:10px;top:300px;width:100px;}
#scrollbox .scroll-tips li {cursor:pointer;text-align:center;}
#scrollbox .scroll-tips li.active {background:#ccc;}
#scrollbox .ct {height: 550px;line-height:550px;text-align:center;font-size:40px;color:#fff;background:green;}
</style>
</head>
<body>
<div id= "scrollbox" >
<div class= "main" >
<div class= "content" >
<h2 id= "title1" class= "title-bar" data-id= "1" >标题1</h2>
<div class= "ct" >内容1</div>
</div>
<div class= "content" >
<h2 id= "title2" class= "title-bar" data-id= "2" >标题2</h2>
<div class= "ct" style= "height:847px;" >内容2</div>
</div>
<div class= "content" >
<h2 id= "title3" class= "title-bar" data-id= "3" >标题3</h2>
<div class= "ct" style= "height:547px;" >内容3</div>
</div>
<div class= "content" >
<h2 id= "title4" class= "title-bar" data-id= "4" >标题4</h2>
<div class= "ct" style= "height:427px;" >内容4</div>
</div>
<div class= "content" >
<h2 id= "title5" class= "title-bar" data-id= "5" >标题5</h2>
<div class= "ct" style= "height:187px;" >内容5</div>
</div>
<div class= "content" >
<h2 id= "title6" class= "title-bar" data-id= "6" >标题6</h2>
<div class= "ct" style= "height:232px;" >内容6</div>
</div>
</div>
<div class= "scroll-tips" >
<ul>
<li id= "li1" class= "active" data-id= "1" >标题1</li>
<li id= "li2" data-id= "2" >标题2</li>
<li id= "li3" data-id= "3" >标题3</li>
<li id= "li4" data-id= "4" >标题4</li>
<li id= "li5" data-id= "5" >标题5</li>
<li id= "li6" data-id= "6" >标题6</li>
</ul>
</div>
</div>
<script>
let li = $$( '.scroll-tips li' );
let scrolltips = $$( '.title-bar' );
function $$(str) {
return document.queryselectorall(str);
};
for (let i = 0, len = li.length; i < len; i++) {
// 点击右侧标识点左侧滚动到窗口显示位置
li[i].onclick = function () {
let id = this .getattribute( 'data-id' );
$$( '.scroll-tips li.active' )[0].classlist.remove( 'active' );
this .classlist.add( 'active' );
$$( '#title' + id)[0].scrollintoview();
};
};
// 获取当前页面滚动到浏览器视口顶部位置的元素
const getscrolltop = function (domarr) {
for (let i = 0, len = domarr.length; i < len; i++) {
let top = domarr[i].getboundingclientrect().top;
// 表示在一定范围内允许的值
if (top >= 0 && top <= 30) {
return domarr[i];
}
}
};
// 监听滚动方法
window.onscroll = function () {
let liid = getscrolltop(scrolltips) && getscrolltop(scrolltips).getattribute( 'data-id' );
if (liid) {
$$( '.scroll-tips li.active' )[0].classlist.remove( 'active' );
$$( '#li' + liid)[0].classlist.add( 'active' );
}
};
</script>
</body>
</html>
|
运行结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/CodingNoob/article/details/101283771