1. 程式人生 > 程式設計 >微信小程式wxs實現吸頂效果

微信小程式wxs實現吸頂效果

在.js檔案中使用page的onPageScroll事件和scroll-view的scroll事件中產生卡頓 (setData 渲染會阻塞其它指令碼執行,導致了整個使用者互動的動畫過程會有延遲), 所以使用wxs響應事件來實現吸頂效果。wxs響應事件基礎庫 2.4.4 開始支援,低版本需做相容處理。

附上文件連結:wxs響應事件

吸頂效果

使用scroll-view元件實現滾動效果,頁面和scroll-view元件的高度設成100%,當豎向滾動條大於等於導航到頂部距離時,導航變成固定定位,固定顯示在頂部,反之,導航取消定位。

<!-- wxml檔案 -->
<wxs module="scroll1" src="./scroll1.wxs"></wxs> <!-- 引入wxs檔案 -->
<scroll-view bindscroll="{{scroll1.scrollEvent}}" data-top="{{navTop}}" style='height:100%;' scroll-y>
 <image src='/images/top_image.png' mode='aspectFill' class='nav-image'></image>
 <view class='navigation'>
  <view wx:for="{{navBarList}}" wx:key="">{{item}}</view>
 </view>
 <view class='div' wx:for="{{8}}" wx:key="">第{{index}}部分</view>
</scroll-view>
/* wxss檔案 */
page{
 font-size: 30rpx;
 background: #fea;
 height: 100%;
}
.div{
 width: 100%;height: 500rpx;
}
.nav-image{
 width: 100%;height: 400rpx;
 vertical-align: middle;
}
.navigation{
 width: 100%;
 height: 100rpx;
 display: flex;
 justify-content: center;
 align-items: center;
 background: #fff;
 top:0;left:0; /*只有使用定位才起效果*/
}
.navigation view{
 padding: 15rpx 20rpx;
 margin: 0 20rpx;
}
//wxs檔案
var scrollEvent = function (e,ins) {
 var scrollTop = e.detail.scrollTop;
 var navTop = e.currentTarget.dataset.top;
 if (scrollTop >= navTop) {
  ins.selectComponent('.navigation').setStyle({
   "position": 'fixed'
  })
 } else {
  ins.selectComponent('.navigation').setStyle({
   "position": 'static'
  })
 }
}
module.exports = {
 scrollEvent: scrollEvent
}
//js檔案
Page({
 /**
  * 頁面的初始資料
  */
 data: {
  navBarList: ['喜歡','點贊','收藏'],navTop:0,//導航距頂部距離
 },/**
  * 生命週期函式--監聽頁面載入
  */
 onLoad: function (options) {
  this.getNavTop();
 },/**
  * 獲取導航距頂部距離
  */
 getNavTop() {
  var that = this;
  var query = wx.createSelectorQuery();
  query.select('.navigation').boundingClientRect(function (data) {
   that.setData({
    navTop: data.top,})
  }).exec();
 },})

漸變導航效果

當豎向滾動條滾動到指定位置時,出現導航條,導航條透明度也隨滾動條位置發生變化。

<!-- wxml檔案 -->
<wxs module="scroll2" src="./scroll2.wxs"></wxs>
<scroll-view bindscroll="{{scroll2.scrollEvent}}" style='height:100%;' scroll-y>
 <!-- 頭部 -->
 <image src='/images/top_image.png' mode='aspectFill' class='nav-image'></image>
 <view class='nav-icon'>
  <navigator open-type='navigateBack' class='back-icon'></navigator>
 </view>
 <view class='nav-bar' style='opacity:0;'>
  <navigator open-type='navigateBack' class='back-icon'></navigator>
  <view>我是導航條</view>
 </view>
 <!-- 頭部 END -->
 <view class='div' wx:for="{{8}}" wx:key="">第{{index}}部分</view>
</scroll-view>
/* wxss檔案 */
page{font-size: 30rpx;background: rgba(200,58,57,0.3);height: 100%;}
.div{width: 100%;height: 500rpx;}
.nav-image{width: 100%;height: 400rpx;vertical-align: middle;}
.nav-icon,.nav-bar{
 position: fixed;
 top: 0;left: 0;
 height: 120rpx;
}
.nav-bar{
 width: 100%;
 display: flex;
 align-items: center;
 background: #fff;
}
.back-icon{
 width: 100rpx;
 height: 100%;
 display: flex;
 justify-content: center;
 align-items: center;
}
.back-icon::after{ /*利用偽類元素模擬出返回icon*/
 content: "";
 display: block;
 width: 25rpx;height: 25rpx;
 border-top: 5rpx solid #fff;
 border-left: 5rpx solid #fff;
 transform: rotate(-45deg);
}
.nav-bar .back-icon::after{border-color: #000;}
//wxs檔案
var scrollEvent=function(e,ins){
 var scrollTop=e.detail.scrollTop;
 if(scrollTop>100){
  ins.selectComponent(".nav-icon").setStyle({
   "opacity":"0"
  })
  var dot = (scrollTop-100)/50;
  ins.selectComponent(".nav-bar").setStyle({
   "opacity": dot
  })
 }else{
  ins.selectComponent(".nav-bar").setStyle({
   "opacity": "0"
  })
  var dot = (100-scrollTop) / 50;
  ins.selectComponent(".nav-icon").setStyle({
   "opacity": dot
  })
 }
};
module.exports={
 scrollEvent: scrollEvent
};

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。