欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序?qū)崿F(xiàn)頂部固定 底部分頁(yè)滾動(dòng)效果

 更新時(shí)間:2022年10月10日 14:47:44   作者:努力中的小白羊  
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)頂部固定底部分頁(yè)滾動(dòng)效果,本文大概給大家分享三種解決方案,每種方案給大家詳細(xì)剖析通過(guò)代碼解析哪種方案更適合,感興趣的朋友跟隨小編一起看看吧

常見(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)畫移至頂部;下面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)畫調(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)文章

最新評(píng)論