@@ -28,7 +28,7 @@ Component({
2828 } ,
2929
3030 lifetimes : {
31- async attached ( ) {
31+ attached ( ) {
3232 this . init ( )
3333 }
3434 } ,
@@ -75,7 +75,9 @@ Component({
7575 // 改变量用于标识是否正在滑动 Sidebar
7676 // 滑动侧栏的时候需要禁止页面滚动去改变 Sidebar 激活项
7777 // 不然会出现 Sidebar 激活项乱跳动的问题
78- isMoving : false
78+ isMoving : false ,
79+ // sidebar-item 节点 rect 信息
80+ sidebarItemRect : { }
7981 } ,
8082 // Anchor 节点信息
8183 _anchor : {
@@ -95,7 +97,9 @@ Component({
9597 // Tip 提示绝对定位的top值
9698 tipTop : 0 ,
9799 // 是否显示 Tip
98- showTip : false
100+ showTip : false ,
101+ // tip 高度
102+ tipHeight : 0
99103 } ,
100104
101105 observers : {
@@ -122,7 +126,7 @@ Component({
122126 // 解析 Sidebar Rect 信息
123127 await this . parseSidebarRect ( )
124128 // 解析 SidebarItem Rect 信息
125- this . parseSidebarItemRect ( )
129+ await this . parseSidebarItemRect ( )
126130 // 获取 index-anchor 所有组件实例
127131 await this . parseIndexAnchors ( )
128132 // 解析 Anchor Rect 信息
@@ -152,18 +156,39 @@ Component({
152156 * 把 Sidebar 每个 Item 的中点位置存到 data 中
153157 * 用于 Tip 定位使用
154158 */
155- parseSidebarItemRect ( ) {
159+ async parseSidebarItemRect ( ) {
156160 // Sidebar 索引个数
157161 const sidebarLength = this . data . sidebarData . length
158- // Sidebar 单个索引高度
162+ // 获取 sidebar-item 节点
163+ const sidebarItemRect = await nodeUtil . getNodeRectFromComponent ( this , '.sidebar-item' )
164+ // Sidebar 单个索引高度(包含了 margin 空隙)
159165 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+ } )
161177 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 ) )
164182 }
183+
184+ const tipHeight = parseInt ( tipFields . height . replace ( 'px' , '' ) )
165185 this . setData ( {
186+ tipHeight,
187+ // tip 旋转后,中线位置下移了 20.5%
188+ tipHeightOverflow : tipHeight * 0.205 ,
189+ [ '_sidebar.sidebarItemRect' ] : sidebarItemRect ,
166190 [ '_sidebar.sidebarItemHeight' ] : sidebarItemHeight ,
191+ [ '_sidebar.sidebarItemRealHeight' ] : sidebarItemRealHeight ,
167192 [ '_sidebar.sidebarItemCenterPoints' ] : sidebarItemCenterPoints
168193 } )
169194 } ,
@@ -403,14 +428,14 @@ Component({
403428 // 300 毫秒后隐藏 Tip
404429 setTimeout ( ( ) => {
405430 this . switchTipShow ( false )
406- } , 300 )
431+ } , 500 )
407432 this . switchIsMovingSidebar ( false )
408433 } ,
409434
410435 /**
411436 * 监听 点击侧边栏
412437 */
413- onTapSidebar ( event ) {
438+ onTapSidebar ( event ) {
414439 // 把事件对象传入触摸滑动监听函数即可
415440 this . onTouchMove ( event )
416441 }
0 commit comments