diff --git a/pages/index/index.js b/pages/index/index.js index 8a6c5b0..2baa48b 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -141,24 +141,37 @@ Page({ const moveY = e.touches[0].clientY; const diffY = moveY - this.data.startY; - // 如果移动距离超过20px,视为拖动 if (Math.abs(diffY) > 20) { this.setData({ isDragging: true }); - // 更新按钮位置 - let newTop = this.data.sidebarBtnTop + diffY * 2; // 转换为rpx + let newTop = this.data.sidebarBtnTop + diffY * 2; - // 限制按钮在屏幕范围内 - const screenHeight = wx.getSystemInfoSync().screenHeight * 2; // 转换为rpx - const btnHeight = 180; // 按钮高度,单位rpx - const boundary = 300; // 边界值,限制拖动范围 + const systemInfo = wx.getSystemInfoSync(); + const screenHeight = systemInfo.screenHeight; + const safeArea = systemInfo.safeArea; + const btnHeight = 90; + const margin = 20; + const tabBarHeight = 70; - if (newTop < boundary) { - newTop = boundary; - } else if (newTop > screenHeight - btnHeight - boundary) { - newTop = screenHeight - btnHeight - boundary; + let minTop, maxTop; + + if (safeArea) { + minTop = safeArea.top + margin; + maxTop = safeArea.bottom - btnHeight - margin - tabBarHeight; + } else { + minTop = margin; + maxTop = screenHeight - btnHeight - margin - tabBarHeight; + } + + const minTopRpx = minTop * 2; + const maxTopRpx = maxTop * 2; + + if (newTop < minTopRpx) { + newTop = minTopRpx; + } else if (newTop > maxTopRpx) { + newTop = maxTopRpx; } this.setData({ @@ -195,12 +208,18 @@ Page({ console.log('首页初始化') const savedBtnTop = wx.getStorageSync('sidebarBtnTop'); const savedBtnHidden = wx.getStorageSync('sidebarBtnHidden'); - if (savedBtnTop !== '') { - this.setData({ - sidebarBtnTop: savedBtnTop, - sidebarBtnHidden: savedBtnHidden || false - }); + const systemInfo = wx.getSystemInfoSync(); + const screenHeight = systemInfo.screenHeight * 2; + + let defaultTop = screenHeight / 2; + if (savedBtnTop !== '' && savedBtnTop !== -100) { + defaultTop = savedBtnTop; } + + this.setData({ + sidebarBtnTop: defaultTop, + sidebarBtnHidden: savedBtnHidden || false + }); this.checkAndRestoreLoginStatus() this.loadGoods() }, diff --git a/pages/index/index.wxml b/pages/index/index.wxml index dbc81bb..b99be5d 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -110,7 +110,7 @@ - +