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

方案說明:
方案1:整個(gè)頁面滾動(dòng),滾動(dòng)至某個(gè)位置fixed圖中“頂部box2”,分頁頁面觸底加載
方案2:頁面高度為屏幕高度,商品部分使用scroll-view,scroll-view初始高度為屏幕高度-頂部高度,只滾動(dòng)scroll-view。
思路說明:
1 將整個(gè)頁面分為上下兩部分,整個(gè)頁面高度100vh(原因1:scroll-view高度需要固定高度;原因2:出現(xiàn)兩個(gè)滾動(dòng)條)
2 頁面上半部分包括banner(box1)以及固定的搜索及tab(box2)
3 根據(jù)頂部box的高度,算出下面scroll-view的高度(windowHieght - 200)
4 scroll-view滑動(dòng)到 頂部box1+margin10的高度,將box1隱藏,box2動(dòng)畫移至頂部;下面scroll高度+滾動(dòng)高度(或box1高度) + margin10高度(確保scroll的商品吸頂之后任然沾滿屏幕)
方案3:使用插件
選擇的是方案2,為什么不選擇方案1,我們來剖析一下。
方案1適合頁面交互比較簡單,根據(jù)頁面滾動(dòng)高度隱藏展示即可。
場景1:tab吸頂之后,切換tab請求數(shù)據(jù),頁面就會(huì)渲染為最初樣式,不會(huì)吸頂。(請求會(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)聽頁面加載
*/
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)畫調(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)}}說明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ù)字開頭,所以前面拼了"good"
說明2:為什么要在bindscrolltoupper觸頂事件中處理初始化樣式,而不是在bindscroll的時(shí)候處理?
使用bindscroll處理,滑動(dòng)會(huì)有來回閃動(dòng)的情況
這些只是大致思路,還有很多細(xì)節(jié)需要處理和考.......
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)頂部固定 底部分頁滾動(dòng)效果的文章就介紹到這了,更多相關(guān)小程序頂部固定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
JavaScript中時(shí)間日期函數(shù)new?Date()詳解(5種獲取時(shí)間戳的函數(shù))
這篇文章主要給大家介紹了關(guān)于JavaScript中時(shí)間日期函數(shù)new?Date()的相關(guān)資料,主要講的是JS中5種獲取時(shí)間戳的函數(shù),new Date()是JavaScript中用于獲取當(dāng)前日期和時(shí)間的內(nèi)置函數(shù),需要的朋友可以參考下2024-04-04
JS實(shí)現(xiàn)table表格固定表頭且表頭隨橫向滾動(dòng)而滾動(dòng)
這篇文章主要介紹了JS實(shí)現(xiàn)table表格固定表頭且表頭可以隨橫向滾動(dòng)而滾動(dòng),需要的朋友可以參考下2017-10-10
js獲取url參數(shù)代碼實(shí)例分享(JS操作URL)
這篇文章主要介紹了js分析url獲取url參數(shù),可以獲取?前面部分、#及后面部分,大家看代碼吧2013-12-12
JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉查找樹的定義與表示方法
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉查找樹的定義與表示方法,簡單講述了二叉查找樹的概念、特點(diǎn)及javascript針對二叉查找樹的創(chuàng)建、插入、遍歷等操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-04-04
JavaScript欄目列表隱藏/顯示簡單實(shí)現(xiàn)
隱藏側(cè)邊欄,并將圖片換成右箭頭圖片;顯示側(cè)邊欄,并將圖片換成左箭頭,這樣的效果想必大家都很熟悉吧,接下來實(shí)現(xiàn)下,感興趣的朋友可以參考下哈2013-04-04
用Javascript實(shí)現(xiàn)Windows任務(wù)管理器的代碼
在Windows系統(tǒng)上,自從98系統(tǒng)以來就提供了腳本宿主(Windows Scripting Host 簡稱WSH)的功能,WSH可以加載并運(yùn)行JS和VBS腳本,并支持調(diào)用系統(tǒng)的COM組件,在COM組件的支持下腳本可以輕松實(shí)現(xiàn)非常強(qiáng)大的功能2012-03-03

