Browse Source

feat: 添加滚动隐藏搜索框和回到顶部功能

- 滚动时搜索框自动隐藏/显示
- 添加回到顶部按钮
- 优化滚动交互体验
pull/1/head
Trae AI 2 months ago
parent
commit
82cae82759
  1. 55
      pages/index/index.js
  2. 15
      pages/index/index.wxml
  3. 70
      pages/index/index.wxss

55
pages/index/index.js

@ -21,6 +21,15 @@ Page({
sidebarBtnTop: 500,
sidebarBtnHidden: false,
// 搜索区域相关
searchSectionVisible: true,
lastScrollTop: 0,
isScrollLocked: false,
// 回到顶部按钮
showBackToTop: false,
scrollTop: 0,
// 搜索相关
searchKeyword: '',
selectedRegion: '全国',
@ -772,7 +781,42 @@ Page({
// 滚动事件处理
onScroll: function(e) {
const { scrollTop, scrollHeight, clientHeight } = e.detail;
const { scrollTop } = e.detail;
if (this.data.isScrollLocked) {
this.setData({ lastScrollTop: scrollTop });
return;
}
const scrollDirection = scrollTop > this.data.lastScrollTop ? 'down' : 'up';
const scrollDelta = Math.abs(scrollTop - this.data.lastScrollTop);
if (scrollDelta < 10) {
this.setData({ lastScrollTop: scrollTop });
return;
}
if (scrollDirection === 'down' && scrollTop > 100 && this.data.searchSectionVisible) {
this.setData({ searchSectionVisible: false, isScrollLocked: true });
setTimeout(() => {
this.setData({ isScrollLocked: false });
}, 300);
} else if (scrollDirection === 'up' && !this.data.searchSectionVisible && scrollTop < 50) {
this.setData({ searchSectionVisible: true, isScrollLocked: true });
setTimeout(() => {
this.setData({ isScrollLocked: false });
}, 300);
}
if (scrollTop > 300 && !this.data.showBackToTop) {
this.setData({ showBackToTop: true });
} else if (scrollTop <= 300 && this.data.showBackToTop) {
this.setData({ showBackToTop: false });
}
this.setData({ lastScrollTop: scrollTop });
const { scrollHeight, clientHeight } = e.detail;
const distanceToBottom = scrollHeight - scrollTop - clientHeight;
const app = getApp();
@ -783,6 +827,15 @@ Page({
app.globalData.showTabBar = true;
},
// 回到顶部
scrollToTop: function() {
this.setData({
scrollTop: 0,
searchSectionVisible: true,
showBackToTop: false
});
},
// 上拉加载更多
onReachBottom: function() {
if (this.data.hasMoreData && !this.data.loadingMore) {

15
pages/index/index.wxml

@ -1,9 +1,9 @@
<view class="container">
<!-- 标题 -->
<view class="title">专业的鸡蛋现货交易平台</view>
<view class="title {{searchSectionVisible ? '' : 'title-hidden'}}">专业的鸡蛋现货交易平台</view>
<!-- 搜索区域 -->
<view class="search-section">
<view class="search-section {{searchSectionVisible ? 'search-visible' : 'search-hidden'}}">
<view class="search-bar">
<view class="search-input-wrapper">
<!-- 将地区选择器放在最左边 -->
@ -107,6 +107,15 @@
<!-- 侧边栏弹出层 -->
<view wx:if="{{showSidebar}}" class="sidebar-overlay" bindtap="toggleSidebar"></view>
<!-- 回到顶部按钮 -->
<view
class="back-to-top-btn {{showBackToTop ? 'visible' : ''}}"
bindtap="scrollToTop"
>
<text class="back-to-top-icon">↑</text>
<text class="back-to-top-text">顶部</text>
</view>
<view wx:if="{{showSidebar}}" class="sidebar">
<view class="sidebar-title">招商合作</view>
<view class="sidebar-menu">
@ -155,9 +164,11 @@
<view class="goods-section">
<scroll-view
class="goods-list"
id="goodsScrollView"
bindscrolltolower="onReachBottom"
bindscroll="onScroll"
scroll-y="true"
scroll-top="{{scrollTop}}"
refresher-enabled="{{true}}"
refresher-default-style="none"
refresher-background="#f8f8f8"

70
pages/index/index.wxss

@ -26,7 +26,36 @@ page {
margin: 20rpx;
color: #333;
text-align: center;
flex: 0 1 auto;
flex-shrink: 0;
transition: all 0.3s ease;
}
.title-hidden {
opacity: 0;
transform: translateY(-20rpx);
height: 0;
margin: 0;
padding: 0;
overflow: hidden;
}
/* 搜索区域样式 */
.search-section {
flex-shrink: 0;
transition: all 0.3s ease;
}
.search-visible {
opacity: 1;
transform: translateY(0);
height: auto;
}
.search-hidden {
opacity: 0;
transform: translateY(-100%);
height: 0;
overflow: hidden;
}
/* 搜索区域样式 */
@ -1326,3 +1355,42 @@ wx-button:not([size=mini]) {
font-size: 24rpx;
color: #ccc;
}
/* 回到顶部按钮 */
.back-to-top-btn {
position: fixed;
right: 30rpx;
bottom: 200rpx;
width: 100rpx;
height: 100rpx;
background: linear-gradient(135deg, #1677ff 0%, #4096ff 100%);
border-radius: 50%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
box-shadow: 0 4rpx 16rpx rgba(22, 119, 255, 0.4);
opacity: 0;
transform: translateY(20rpx);
transition: all 0.3s ease;
z-index: 999;
pointer-events: none;
}
.back-to-top-btn.visible {
opacity: 1;
transform: translateY(0);
pointer-events: auto;
}
.back-to-top-icon {
font-size: 36rpx;
color: white;
font-weight: bold;
}
.back-to-top-text {
font-size: 18rpx;
color: white;
margin-top: 2rpx;
}

Loading…
Cancel
Save