@@ -72,9 +72,9 @@ export default class Navbar extends SuperComponent {
7272 } ;
7373
7474 attached ( ) {
75+ this . onMenuButtonBoundingClientRectWeightChange ( ) ;
7576 this . initStyle ( ) ;
7677 this . getLeftRect ( ) ;
77- this . onMenuButtonBoundingClientRectWeightChange ( ) ;
7878 }
7979
8080 detached ( ) {
@@ -146,42 +146,44 @@ export default class Navbar extends SuperComponent {
146146
147147 onMenuButtonBoundingClientRectWeightChange ( ) {
148148 if ( wx . onMenuButtonBoundingClientRectWeightChange ) {
149- wx . onMenuButtonBoundingClientRectWeightChange ( ( res : object ) => this . queryElements ( res ) ) ; // 监听胶囊条长度变化,隐藏遮挡的内容
149+ this . onMenuButtonBoundingClientRectWeightChangeCallback = (
150+ res : WechatMiniprogram . OnMenuButtonBoundingClientRectWeightChangeListenerResult ,
151+ ) => this . queryElements ( res ) ;
152+
153+ wx . onMenuButtonBoundingClientRectWeightChange ( this . onMenuButtonBoundingClientRectWeightChangeCallback ) ;
150154 }
151155 } ,
152156
153157 offMenuButtonBoundingClientRectWeightChange ( ) {
154- if ( wx . offMenuButtonBoundingClientRectWeightChange ) {
155- wx . offMenuButtonBoundingClientRectWeightChange ( ( res : object ) => this . queryElements ( res ) ) ;
158+ if ( this . onMenuButtonBoundingClientRectWeightChangeCallback ) {
159+ wx . offMenuButtonBoundingClientRectWeightChange ( this . onMenuButtonBoundingClientRectWeightChangeCallback ) ;
156160 }
157161 } ,
158162
159163 /**
160164 * 比较胶囊条和navbar内容,决定是否隐藏
161165 * @param capsuleRect API返回值,胶囊条的位置信息
162166 */
163- queryElements ( capsuleRect ) {
167+ queryElements ( capsuleRect : WechatMiniprogram . OnMenuButtonBoundingClientRectWeightChangeListenerResult ) {
164168 Promise . all ( [
165169 getRect ( this , `.${ this . data . classPrefix } __left` ) ,
166170 getRect ( this , `.${ this . data . classPrefix } __center` ) ,
167171 ] ) . then ( ( [ leftRect , centerRect ] ) => {
168- // 部分安卓机型中(目前仅在Magic6/7中复现),仍存在精度问题,暂使用 Math.round() 取整规避
169- if ( Math . round ( leftRect . right ) > capsuleRect . left ) {
170- this . setData ( {
171- hideLeft : true ,
172- hideCenter : true ,
173- } ) ;
174- } else if ( Math . round ( centerRect . right ) > capsuleRect . left ) {
175- this . setData ( {
176- hideLeft : false ,
177- hideCenter : true ,
178- } ) ;
179- } else {
180- this . setData ( {
181- hideLeft : false ,
182- hideCenter : false ,
183- } ) ;
184- }
172+ const capsuleRectLeft = capsuleRect . left ;
173+
174+ const leftRectRight = Math . round ( leftRect . right ) ;
175+ const centerRectRight = Math . round ( centerRect . right ) ;
176+
177+ const hideAll = leftRectRight > capsuleRectLeft ;
178+ const hideCenter = centerRectRight > capsuleRectLeft ;
179+
180+ // eslint-disable-next-line
181+ console . log ( leftRectRight , capsuleRectLeft , centerRectRight ) ;
182+
183+ this . setData ( {
184+ hideLeft : hideAll || false ,
185+ hideCenter : hideAll || hideCenter ,
186+ } ) ;
185187 } ) ;
186188 } ,
187189
0 commit comments