国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序導航欄滑動定位功能示例(實現CSS3的positionsticky效果)

2019-11-19 12:13:53
字體:
來源:轉載
供稿:網友

本文實例講述了微信小程序導航欄滑動定位功能。分享給大家供大家參考,具體如下:

實現的效果

實現的原理

1. 通過對scroll的監聽獲取滾動條的scrollTop值;

2. 在導航的class判斷scrollTop;

3. 切換position:fixed與position:relative。

WXML

<view style="height:100%;position:fixed;width:100%;"> <scroll-view scroll-y="false" bindscroll="scroll" style="height:100%;">  <view class="page-bottom-content">   <text>{{text}}</text>  </view>  <view class="page-banner">   banner  </view>  <view class="page-group {{scrollTop > 360 ? 'page-group-position' : ''}}">   <view class="page-nav-list"><text>首頁</text></view>   <view class="page-nav-list"><text>活動</text></view>   <view class="page-nav-list"><text>菜單</text></view>   <view class="page-nav-list"><text>我的</text></view>  </view>  <view class="goods-list">   goods-list  </view> </scroll-view></view>

WXCSS

.page-banner{height: 500rpx;background-color: greenyellow;padding: 20rpx;color:#fff;}.page-group{ display: table; background-color: blueviolet; width: 100%; table-layout: fixed; position: relative; top: 0; left: 0;}.page-group-position{ position: fixed;}.page-nav-list{ padding:30rpx 0 ; display: table-cell; text-align: center; color: #fff;}.goods-list{ height: 2000rpx; background-color: green; padding: 20rpx; color:#fff;}

JS

Page({ data: {  scrollTop: null }, //滾動條監聽 scroll: function (e) {  this.setData({ scrollTop: e.detail.scrollTop }) },})

總結:

1. 要獲取scrollTop,在微信小程序中我們需要:<scroll-view scroll-y="false" bindscroll="scroll" style="height:100%;"></scroll-view>

2. 微信小程序要綁定bindscroll事件,需要綁定在scroll-view組件上,同時設置scroll-y和height。

3. 如果scroll-view的高設置100%,就需要在其外層添加一個固定高的盒子,否則監聽不會生效。

4. 通過scroll事件獲取scrollTop:this.setData({ scrollTop: e.detail.scrollTop })

5. 導航欄class的切換:

scrollTop > 360 ? 'page-group-position' : ''

實質:

scrollTop > 360 ? 'fixed' : 'relative'

缺點:

1. 由于有獲取scrollTop的過程,所以會出現定位不及時,也就是導航閃動的效果;
2. 沒有原生CSS3的position:sticky流暢,體驗比較差;
3. 由于我目前還未找到直接獲取page-group的offsetTop方法,所以直接采用的是360固定值,此效果是在蘋果6進行的測試。

此效果只是提供一個思路,不建議使用在項目中,體驗太差,有待優化。如果有更好思路的大神,敬請指教。

Demo源碼:

點擊此處本站下載

希望本文所述對大家微信小程序開發有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 防城港市| 合水县| 永和县| 虎林市| 英德市| 临朐县| 合肥市| 友谊县| 涡阳县| 宿松县| 潼南县| 城固县| 陵水| 墨玉县| 清水县| 惠来县| 绥棱县| 吐鲁番市| 高密市| 应用必备| 宁明县| 汉沽区| 北宁市| 淮阳县| 临高县| 灵璧县| 胶南市| 黄浦区| 阿拉善右旗| 分宜县| 万安县| 普安县| 南丰县| 南开区| 桐柏县| 连城县| 北流市| 宁强县| 蓬安县| 宁强县| 炎陵县|