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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
| <template>
| <view class="tn-sticky-class">
| <view
| class="tn-sticky__wrap"
| :class="[stickyClass]"
| :style="[stickyStyle]"
| >
| <view
| class="tn-sticky__item"
| :style="{
| position: fixed ? 'fixed' : 'static',
| top: stickyTop + 'px',
| left: left + 'px',
| width: width === 'auto' ? 'auto' : width + 'px',
| zIndex: elZIndex
| }"
| >
| <slot></slot>
| </view>
| </view>
| </view>
| </template>
|
| <script>
| export default {
| name: 'tn-sticky',
| props: {
| // 吸顶容器到顶部某个距离的时候进行吸顶
| // 在H5中,customNavBar的高度为45px
| offsetTop: {
| type: Number,
| default: 0
| },
| // H5顶部导航栏的高度
| h5NavHeight: {
| type: Number,
| default: 45
| },
| // 自定义顶部导航栏高度
| customNavHeight: {
| type: Number,
| default: 0
| },
| // 是否开启吸顶
| enabled: {
| type: Boolean,
| default: true
| },
| // 吸顶容器的背景颜色
| backgroundColor: {
| type: String,
| default: '#FFFFFF'
| },
| // z-index
| zIndex: {
| type: Number,
| default: 0
| },
| // 索引值,区分不同的吸顶组件
| index: {
| type: [String, Number],
| default: ''
| }
| },
| computed: {
| elZIndex() {
| return this.zIndex ? this.zIndex : this.$t.zIndex.sticky
| },
| backgroundColorStyle() {
| return this.$t.color.getBackgroundColorStyle(this.backgroundColor)
| },
| backgroundColorClass() {
| return this.$t.color.getBackgroundColorInternalClass(this.backgroundColor)
| },
| stickyClass() {
| let clazz = ''
| clazz += this.elClass
| if (this.backgroundColorClass) {
| clazz += ` ${this.backgroundColorClass}`
| }
| return clazz
| },
| stickyStyle() {
| let style = {}
| style.height = this.fixed ? this.height + 'px' : 'auto'
| if (this.backgroundColorStyle) {
| style.color = this.backgroundColorStyle
| }
| if (this.elZIndex) {
| style.zIndex = this.elZIndex
| }
| return style
| }
| },
| data() {
| return {
| // 监听组件别名
| stickyObserverName: 'tnStickyObserver',
| // 组件的唯一编号
| elClass: this.$t.uuid(),
| // 是否固定
| fixed: false,
| // 高度
| height: 'auto',
| // 宽度
| width: 'auto',
| // 距离顶部的距离
| stickyTop: 0,
| // 左边距离
| left: 0
| }
| },
| watch: {
| offsetTop(val) {
| this.initObserver()
| },
| enabled(val) {
| if (val === false) {
| this.fixed = false
| this.disconnectObserver(this.stickyObserverName)
| } else {
| this.initObserver()
| }
| },
| customNavHeight(val) {
| this.initObserver()
| }
| },
| mounted() {
| this.initObserver()
| },
| methods: {
| // 初始化监听组件的布局状态
| initObserver() {
| if (!this.enabled) return
| // #ifdef H5
| this.stickyTop = this.offsetTop != 0 ? uni.upx2px(this.offsetTop) + this.h5NavHeight : this.h5NavHeight
| // #endif
| // #ifndef H5
| this.stickyTop = this.offsetTop != 0 ? uni.upx2px(this.offsetTop) + this.customNavHeight : this.customNavHeight
| // #endif
|
| this.disconnectObserver(this.stickyObserverName)
| this._tGetRect('.' + this.elClass).then((res) => {
| this.height = res.height
| this.left = res.left
| this.width = res.width
| this.$nextTick(() => {
| this.connectObserver()
| })
| })
| },
| // 监听组件的布局状态
| connectObserver() {
| this.disconnectObserver(this.stickyObserverName)
| // 组件内获取布局状态,不能用uni.createIntersectionObserver,而必须用this.createIntersectionObserver
| const contentObserver = this.createIntersectionObserver({
| thresholds: [0.95, 0.98, 1]
| })
| contentObserver.relativeToViewport({
| top: -this.stickyTop
| })
| contentObserver.observe('.' + this.elClass, res => {
| if (!this.enabled) return
| this.setFixed(res.boundingClientRect.top)
| })
| this[this.stickyObserverName] = contentObserver
| },
| // 设置是否固定
| setFixed(top) {
| const fixed = top < this.stickyTop
| if (fixed) this.$emit('fixed', this.index)
| else if (this.fixed) this.$emit('unfixed', this.index)
| this.fixed = fixed
| },
| // 停止监听组件的布局状态
| disconnectObserver(observerName) {
| const observer = this[observerName]
| observer && observer.disconnect()
| }
| }
| }
| </script>
|
| <style>
| </style>
|
|