Browse Source

修复搜索框收起来时页面强行下滑和过渡动画不兼容问题

pull/1/head
Trae AI 2 months ago
parent
commit
e223ac4c96
  1. 29
      pages/index/index.js
  2. 1
      pages/index/index.wxml
  3. 10
      pages/index/index.wxss

29
pages/index/index.js

@ -25,8 +25,6 @@ Page({
searchSectionVisible: true, searchSectionVisible: true,
lastScrollTop: 0, lastScrollTop: 0,
isScrollLocked: false, isScrollLocked: false,
searchSectionOpacity: 1,
searchSectionTransform: 0,
isSearchBarFullyHidden: false, isSearchBarFullyHidden: false,
// 回到顶部按钮 // 回到顶部按钮
@ -826,33 +824,22 @@ Page({
onScroll: function(e) { onScroll: function(e) {
const { scrollTop } = e.detail; const { scrollTop } = e.detail;
let searchSectionOpacity = 1; // 只控制搜索区域的完全显示/隐藏状态,不再使用内联样式控制过渡
let searchSectionTransform = 0; // 所有过渡效果通过CSS类统一控制
let isSearchBarFullyHidden = false; let isSearchBarFullyHidden = false;
if (scrollTop > 50) { if (scrollTop > 200) {
// 当滚动距离超过50rpx时,开始逐渐隐藏 // 当滚动距离超过200rpx时,完全隐藏搜索区域
const hideProgress = Math.min((scrollTop - 50) / 150, 1); // 0-1的隐藏进度,延长隐藏过程 isSearchBarFullyHidden = true;
searchSectionOpacity = 1 - hideProgress;
searchSectionTransform = -20 * hideProgress;
// 当隐藏进度达到100%时,完全隐藏并收缩高度
if (hideProgress >= 1) {
isSearchBarFullyHidden = true;
}
} }
// 当滚动回到顶部时,重置所有状态 // 当滚动回到顶部时,显示搜索区域
if (scrollTop <= 50) { if (scrollTop <= 150) {
searchSectionOpacity = 1;
searchSectionTransform = 0;
isSearchBarFullyHidden = false; isSearchBarFullyHidden = false;
} }
// 更新搜索框状态和样式 // 更新搜索框状态
this.setData({ this.setData({
searchSectionOpacity: searchSectionOpacity,
searchSectionTransform: searchSectionTransform,
isSearchBarFullyHidden: isSearchBarFullyHidden, isSearchBarFullyHidden: isSearchBarFullyHidden,
lastScrollTop: scrollTop lastScrollTop: scrollTop
}); });

1
pages/index/index.wxml

@ -2,7 +2,6 @@
<!-- 顶部搜索区域容器 --> <!-- 顶部搜索区域容器 -->
<view <view
class="top-section {{isSearchBarFullyHidden ? 'top-hidden' : ''}}" class="top-section {{isSearchBarFullyHidden ? 'top-hidden' : ''}}"
style="opacity: {{searchSectionOpacity}}; transform: translateY({{searchSectionTransform}}rpx);"
> >
<!-- 标题 --> <!-- 标题 -->
<view class="title">专业的鸡蛋交易平台</view> <view class="title">专业的鸡蛋交易平台</view>

10
pages/index/index.wxss

@ -26,8 +26,11 @@ page {
margin-bottom: 20rpx; margin-bottom: 20rpx;
overflow: hidden; overflow: hidden;
transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
will-change: opacity, transform, padding, margin, max-height; will-change: opacity, transform, height;
max-height: 200rpx; /* 设置一个具体的最大高度,确保过渡效果生效 */ max-height: 200rpx;
opacity: 1;
transform: translateY(0);
height: auto;
} }
/* 顶部搜索区域完全隐藏状态 - 用于平滑收缩高度 */ /* 顶部搜索区域完全隐藏状态 - 用于平滑收缩高度 */
@ -37,7 +40,8 @@ page {
margin-bottom: 0; margin-bottom: 0;
pointer-events: none; pointer-events: none;
opacity: 0; opacity: 0;
transform: translateY(-100%); transform: translateY(-50rpx);
height: 0;
} }
/* 标题样式 */ /* 标题样式 */

Loading…
Cancel
Save