diff --git a/pages/index/index.js b/pages/index/index.js index c07bc87..bfc0b74 100644 --- a/pages/index/index.js +++ b/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) { diff --git a/pages/index/index.wxml b/pages/index/index.wxml index 2a1f895..e8f50e9 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -1,9 +1,9 @@ - 专业的鸡蛋现货交易平台 + 专业的鸡蛋现货交易平台 - + @@ -107,6 +107,15 @@ + + + + + 顶部 + 招商合作 @@ -155,9 +164,11 @@