微信小程序?qū)崿F(xiàn)頂部固定 底部分頁(yè)滾動(dòng)效果
常見(jiàn)商品頁(yè)效果:頂部banner+分類,下面商品列表。

方案說(shuō)明:
方案1:整個(gè)頁(yè)面滾動(dòng),滾動(dòng)至某個(gè)位置fixed圖中“頂部box2”,分頁(yè)頁(yè)面觸底加載
方案2:頁(yè)面高度為屏幕高度,商品部分使用scroll-view,scroll-view初始高度為屏幕高度-頂部高度,只滾動(dòng)scroll-view。
思路說(shuō)明:
1 將整個(gè)頁(yè)面分為上下兩部分,整個(gè)頁(yè)面高度100vh(原因1:scroll-view高度需要固定高度;原因2:出現(xiàn)兩個(gè)滾動(dòng)條)
2 頁(yè)面上半部分包括banner(box1)以及固定的搜索及tab(box2)
3 根據(jù)頂部box的高度,算出下面scroll-view的高度(windowHieght - 200)
4 scroll-view滑動(dòng)到 頂部box1+margin10的高度,將box1隱藏,box2動(dòng)畫(huà)移至頂部;下面scroll高度+滾動(dòng)高度(或box1高度) + margin10高度(確保scroll的商品吸頂之后任然沾滿屏幕)
方案3:使用插件
選擇的是方案2,為什么不選擇方案1,我們來(lái)剖析一下。
方案1適合頁(yè)面交互比較簡(jiǎn)單,根據(jù)頁(yè)面滾動(dòng)高度隱藏展示即可。
場(chǎng)景1:tab吸頂之后,切換tab請(qǐng)求數(shù)據(jù),頁(yè)面就會(huì)渲染為最初樣式,不會(huì)吸頂。(請(qǐng)求會(huì)重新setData數(shù)據(jù),有些數(shù)據(jù)有多有少)
復(fù)制以下代碼可以直接演示demo效果
demo.wxml
<!--pages/demo/demo.wxml-->
<view class="wrap">
<view class="top-box" style="{{boxStyle}}">
<view class="top-box1" style="{{box1Style}}">頂部box1</view>
<!-- <view class="top-box2"></view> -->
<scroll-view class="top-box2" scroll-into-view="{{scrollId}}" scroll-x="true"scroll-with-animation="true" >
<block wx:for="{{cates}}" wx:key="index">
<view class="{{item.id === currentId?'cate-item-act cate-item':'cate-item'}}" data-id="{{item.id}}" id="good{{item.id}}" bindtap="cateChange">{{item.name}}</view>
</block>
</scroll-view>
</view>
<scroll-view scroll-y="true" class="scroll-con" style="{{scrollViewStyle}}" bindscroll="goodsViewScroll" bindscrolltoupper="goodsViewScrollTop">
<view class="scroll-con-item" wx:for="{{cates}}" wx:key="index">{{item.name}}</view>
</scroll-view>
</view>demo.js
Page({
data: {
cates:[
{id:null,name:'全部'},
{id:1,name:'分類1'},
{id:2,name:'分類2'},
{id:3,name:'分類3'},
{id:4,name:'分類4'},
{id:5,name:'分類5'},
{id:6,name:'分類6'},
{id:7,name:'分類7'},
{id:8,name:'分類8'}
],
currentId:null,
serviceList:[
{id:1,name:'1'},
{id:2,name:'2'},
{id:3,name:'3'},
{id:4,name:'4'},
{id:5,name:'5'},
{id:6,name:'6'},
{id:7,name:'7'},
{id:8,name:'8'}
],
scrollId:null,//滑動(dòng)id,切換tab效果
animationStyle:'',
isNeedFixed:false
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad(options) {
let that = this;
wx.getSystemInfo({
success: function (res, rect) {
that.setData({
// 商品scroll高度 = 可使用窗口 - (頂部box的高度+margin20 -20(底部留白))
scrollViewHeight: parseInt(res.windowHeight - 220 -20),
scrollViewStyle:`height:${parseInt(res.windowHeight - 220-20)}px`
})
}
})
},
cateChange(e){
let currentId = e.currentTarget.dataset.id;
let scrollId = e.currentTarget.id;
this.setData({
currentId,
scrollId
})
},
// 加this.data.isNeedFixed條件防止頻繁的setdata
// 1 隱藏box1,box2會(huì)自動(dòng)吸頂 box2置頂
// 2 設(shè)置scroll-view高度+120, 設(shè)置頂部box高度為box2高度
goodsViewScroll(e){
console.log(e.detail.scrollTop, this.data.isNeedFixed)
if(e.detail.scrollTop >= 120 ){
console.log('可以動(dòng)畫(huà)調(diào)整位置了')
this.setData({
isNeedFixed:true,
box1Style:`height:0px;`,
boxStyle:`height:80px;`,
scrollViewStyle: `height:${this.data.scrollViewHeight + 120}px`,
}
}
// if(e.detail.scrollTop < 120 ) {
// console.log('需要保持原樣')
// this.setData({
// isNeedFixed:false,
// box1Style:`height:110px;`,
// boxStyle:`height:200px;`,
// scrollViewStyle: `height:${this.data.scrollViewHeight}px`,
// },()=>{
// wx.pageScrollTo({
// scrollTop: 0,
// })
// })
// }
},
goodsViewScrollTop(e){
this.setData({
isNeedFixed:false,
box1Style:`height:110px;`,
boxStyle:`height:200px;`,
scrollViewStyle: `height:${this.data.scrollViewHeight}px`,
})
}
})demo.wxss
.wrap {
height: 100vh;
}
.top-box {
height: 200px;
height: 200px;
background-color: #fff;
border: 1px solid #d1d1d1;
transition:height 0.2s;
-webkit-transition:height 0.2s; /* Safari */
}
.top-box1 {
height: 110px;
width: 100%;
margin-bottom: 10px;
background-color: #3293FF;
overflow: hidden;
transition:height 0.2s;
-webkit-transition:height 0.2s; /* Safari */
}
.top-box2 {
height: 80px;
width: 100%;
background-color: #ffbe32;
white-space: nowrap;
padding: 50rpx 0;
box-sizing: border-box;
}
.top-box2 .cate-item {
display: inline-block;
padding: 10rpx 20rpx;
font-size: 26rpx;
margin-right: 20rpx;
color: #767A84;
}
.top-box2 .cate-item:last-child{
margin-right: 0rpx;
}
.top-box2 .cate-item-act {
background: #3293FF;
color: #fff;
border-radius: 48rpx;
}
.scroll-con {
padding: 0 10px;
margin-top: 20px;
box-sizing: border-box;
background-color: #fff;
}
.scroll-con-item {
height: 100px;
width: 100%;
background-color: salmon;
margin-bottom: 10px;
}
.ani-btn {
display: inline-block;
padding: 20rpx;
margin: 10rpx;
border: 1px solid #d1d1d1;
}
@keyframes move{ from{transform: translateY( 30px)} to {transform: translateY( 0px)}}說(shuō)明1:scroll-into-view 設(shè)置哪個(gè)方向可滾動(dòng),則在哪個(gè)方向滾動(dòng)到該元素
scroll-view設(shè)置x軸滾動(dòng)到scrollId位置 scroll-x="true"scroll-into-view="{{scrollId}}"
item子元素設(shè)置id="good{{item.id}}" 由于id不能已數(shù)字開(kāi)頭,所以前面拼了"good"
說(shuō)明2:為什么要在bindscrolltoupper觸頂事件中處理初始化樣式,而不是在bindscroll的時(shí)候處理?
使用bindscroll處理,滑動(dòng)會(huì)有來(lái)回閃動(dòng)的情況
這些只是大致思路,還有很多細(xì)節(jié)需要處理和考.......
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)頂部固定 底部分頁(yè)滾動(dòng)效果的文章就介紹到這了,更多相關(guān)小程序頂部固定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript語(yǔ)法約定和程序調(diào)試原理解析
這篇文章主要介紹了JavaScript語(yǔ)法約定和程序調(diào)試原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
JavaScript手寫(xiě)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的快捷鍵庫(kù)
前端開(kāi)發(fā)中,有時(shí)項(xiàng)目會(huì)遇到一些快捷鍵需求,比如綁定快捷鍵,展示快捷鍵,編輯快捷鍵等需求,所以本文就來(lái)用JavaScript手寫(xiě)一個(gè)簡(jiǎn)單的快捷鍵庫(kù)吧2024-02-02
js數(shù)組常用操作方法小結(jié)(增加,刪除,合并,分割等)
這篇文章主要介紹了js數(shù)組常用操作方法,結(jié)合實(shí)例總結(jié)了javascript數(shù)組的增加、刪除、合并、分割等操作技巧,需要的朋友可以參考下2016-08-08
javasciprt下jquery函數(shù)$.post執(zhí)行無(wú)響應(yīng)的解決方法
這篇文章主要介紹了javasciprt下jquery函數(shù)$.post執(zhí)行無(wú)響應(yīng)的解決方法,需要的朋友可以參考下2014-03-03
js密碼強(qiáng)度實(shí)時(shí)檢測(cè)代碼
這篇文章主要為大家詳細(xì)介紹了js密碼強(qiáng)度實(shí)時(shí)檢測(cè)代碼,密碼強(qiáng)度的判斷, 在注冊(cè)網(wǎng)站用戶的時(shí)候, 是一個(gè)必須要做的事情,如何實(shí)現(xiàn)js密碼強(qiáng)度檢測(cè),感興趣的小伙伴們可以參考一下2016-03-03
javascript字母大小寫(xiě)轉(zhuǎn)換的4個(gè)函數(shù)詳解
這篇文章主要介紹了javascript字母大小寫(xiě)轉(zhuǎn)換的4個(gè)函數(shù)詳解,需要的朋友可以參考下2014-05-05

