@@ -28,7 +28,7 @@ Component({
28
28
} ,
29
29
30
30
lifetimes : {
31
- async attached ( ) {
31
+ attached ( ) {
32
32
this . init ( )
33
33
}
34
34
} ,
@@ -75,7 +75,9 @@ Component({
75
75
// 改变量用于标识是否正在滑动 Sidebar
76
76
// 滑动侧栏的时候需要禁止页面滚动去改变 Sidebar 激活项
77
77
// 不然会出现 Sidebar 激活项乱跳动的问题
78
- isMoving : false
78
+ isMoving : false ,
79
+ // sidebar-item 节点 rect 信息
80
+ sidebarItemRect : { }
79
81
} ,
80
82
// Anchor 节点信息
81
83
_anchor : {
@@ -95,7 +97,9 @@ Component({
95
97
// Tip 提示绝对定位的top值
96
98
tipTop : 0 ,
97
99
// 是否显示 Tip
98
- showTip : false
100
+ showTip : false ,
101
+ // tip 高度
102
+ tipHeight : 0
99
103
} ,
100
104
101
105
observers : {
@@ -122,7 +126,7 @@ Component({
122
126
// 解析 Sidebar Rect 信息
123
127
await this . parseSidebarRect ( )
124
128
// 解析 SidebarItem Rect 信息
125
- this . parseSidebarItemRect ( )
129
+ await this . parseSidebarItemRect ( )
126
130
// 获取 index-anchor 所有组件实例
127
131
await this . parseIndexAnchors ( )
128
132
// 解析 Anchor Rect 信息
@@ -152,18 +156,39 @@ Component({
152
156
* 把 Sidebar 每个 Item 的中点位置存到 data 中
153
157
* 用于 Tip 定位使用
154
158
*/
155
- parseSidebarItemRect ( ) {
159
+ async parseSidebarItemRect ( ) {
156
160
// Sidebar 索引个数
157
161
const sidebarLength = this . data . sidebarData . length
158
- // Sidebar 单个索引高度
162
+ // 获取 sidebar-item 节点
163
+ const sidebarItemRect = await nodeUtil . getNodeRectFromComponent ( this , '.sidebar-item' )
164
+ // Sidebar 单个索引高度(包含了 margin 空隙)
159
165
const sidebarItemHeight = this . data . _sidebar . height / sidebarLength
160
-
166
+ // Sidebar 单个索引真实高度
167
+ const sidebarItemRealHeight = sidebarItemRect . height
168
+ // 获取 sidebar-item margin-top 属性
169
+ const sidebarItemFields = await nodeUtil . getNodeFieldsFromComponent ( this , '.sidebar-item' , {
170
+ computedStyle : [ 'margin-top' ]
171
+ } )
172
+ // 获取 tip height 属性
173
+ // 只能用 height 获取高度,因为 tip 旋转后,rect的宽高发生了变化
174
+ const tipFields = await nodeUtil . getNodeFieldsFromComponent ( this , '.tip' , {
175
+ computedStyle : [ 'height' ]
176
+ } )
161
177
const sidebarItemCenterPoints = [ ]
162
- for ( let i = 0 ; i < sidebarLength ; i ++ ) {
163
- sidebarItemCenterPoints . push ( i * sidebarItemHeight )
178
+ const sidebarItemMarginTop = sidebarItemFields [ 'margin-top' ] . replace ( 'px' , '' )
179
+
180
+ for ( let i = 1 ; i <= sidebarLength ; i ++ ) {
181
+ sidebarItemCenterPoints . push ( ( i * 2 - 1 ) * sidebarItemRealHeight / 2 + i * parseInt ( sidebarItemMarginTop ) )
164
182
}
183
+
184
+ const tipHeight = parseInt ( tipFields . height . replace ( 'px' , '' ) )
165
185
this . setData ( {
186
+ tipHeight,
187
+ // tip 旋转后,中线位置下移了 20.5%
188
+ tipHeightOverflow : tipHeight * 0.205 ,
189
+ [ '_sidebar.sidebarItemRect' ] : sidebarItemRect ,
166
190
[ '_sidebar.sidebarItemHeight' ] : sidebarItemHeight ,
191
+ [ '_sidebar.sidebarItemRealHeight' ] : sidebarItemRealHeight ,
167
192
[ '_sidebar.sidebarItemCenterPoints' ] : sidebarItemCenterPoints
168
193
} )
169
194
} ,
@@ -403,14 +428,14 @@ Component({
403
428
// 300 毫秒后隐藏 Tip
404
429
setTimeout ( ( ) => {
405
430
this . switchTipShow ( false )
406
- } , 300 )
431
+ } , 500 )
407
432
this . switchIsMovingSidebar ( false )
408
433
} ,
409
434
410
435
/**
411
436
* 监听 点击侧边栏
412
437
*/
413
- onTapSidebar ( event ) {
438
+ onTapSidebar ( event ) {
414
439
// 把事件对象传入触摸滑动监听函数即可
415
440
this . onTouchMove ( event )
416
441
}
0 commit comments