This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.vue
104 lines (97 loc) · 2.28 KB
/
demo.vue
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<template>
<div>
<demo-block title="基础用法">
<van-sticky :scroll-top="pageScrollTop">
<van-button type="primary" custom-style="margin-left: 15px">
基础用法
</van-button>
</van-sticky>
</demo-block>
<demo-block title="吸顶距离">
<van-sticky :offset-top="50" :scroll-top="pageScrollTop">
<van-button type="info" custom-style="margin-left: 115px">
吸顶距离
</van-button>
</van-sticky>
</demo-block>
<demo-block title="指定容器">
<view id="container" style="height: 150px; background-color: #fff;">
<van-sticky :container="container" :scroll-top="pageScrollTop">
<van-button type="warning" custom-style="margin-left: 215px;">
指定容器
</van-button>
</van-sticky>
</view>
</demo-block>
<demo-block title="嵌套在 scroll-view 内使用">
<scroll-view
@scroll="onScroll"
scroll-y
id="scroller"
style="height: 200px; background-color: #fff;"
>
<view style="height: 400px; padding-top: 50px;">
<van-sticky
:scroll-top="scrollTop"
:offset-top="offsetTop"
>
<van-button type="warning">
嵌套在 scroll-view 内
</van-button>
</van-sticky>
</view>
</scroll-view>
</demo-block>
</div>
</template>
<script>
export default {
data() {
return {
container: null,
scrollTop: 0,
offsetTop: 0,
pageScrollTop: 0
}
},
onPageScroll(e) {
this.pageScrollTop = e.scrollTop
this.setContainer()
},
methods: {
onScroll(event) {
uni
.createSelectorQuery()
.select('#scroller')
.boundingClientRect(res => {
this.scrollTop = event.detail.scrollTop
this.offsetTop = res.top
})
.exec()
},
setContainer() {
uni
.createSelectorQuery()
.select('#container')
.boundingClientRect(res => {
this.container = res
})
.exec()
}
}
}
</script>
<style>
page {
height: 200vh;
}
.van-button {
margin-left: 16px;
}
.sticky-container {
position: relative;
z-index: -1;
height: 150px;
background-color: #fff;
}
</style>