微信小程序,scroll-view中实现tab吸顶效果

页面的布局是这样子的

需求是选项一、二、三、四要能吸顶,如果不是放在scroll-view中,可用 css position:fixed; 来实现,但现在是想在scroll-view中实现这个效果,经过一番折腾,终于另辟蹊径找到了一条不一样的路子。

整体布局没做多大改动,只是将下半部份的主体内容,放到了一个scroll-view里面,然后内部的子scroll-view滚动时,判断一下导航条距离顶部的位置,如果大于0,则重置子scroll-view的scroll-top为0。

<!--index.wxml-->
<view class="container">
  <view class="main">
    <scroll-view scroll-y="{{true}}" style="height:{{scrollViewHeight}}px;">
      <view class="block">asdfasdfasdf</view>
      <view class="block">asdfasdfasdf</view>
      <view class="nav" id="nav">
        <view class="item item-hover">选项一</view>
        <view class="item">选项二</view>
        <view class="item">选项三</view>
        <view class="item">选项四</view>
      </view>
      <scroll-view scroll-y="{{true}}" scroll-top="{{subScrollTop}}" style="height:{{subScrollViewHeight}}px;" scroll-with-animation="true" bindscroll="scrollChange">
        <view class="block">asdfasdfasdf</view>
        <view class="block">asdfasdfasdf</view>
        <view class="block">asdfasdfasdf</view>
        <view class="block">asdfasdfasdf</view>
        <view class="block">asdfasdfasdf</view>
        <view class="block">asdfasdfasdf</view>
      </scroll-view>
    </scroll-view>
  </view>

  <view class="footer" id="footer">
    <button>按钮一</button>
    <button>按钮二</button>
    <button>按钮三</button>
  </view>
</view>
// index.js
Page({
  data:{
    scrollViewHeight: 0,
    windowHeight: 0,
    footerViewHeight: 0,
    subScrollViewHeight: 0,
    subScrollTop: 0,
  },
  onReady: function(e){
    var that = this;

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
        });

        //计算主scroll-view和footer页脚高度
        wx.createSelectorQuery().select('#footer').boundingClientRect( (res) => {
          var footerViewHeight = res.height;
          var scrollViewHeight = that.data.windowHeight - footerViewHeight;
          that.setData({
            scrollViewHeight: scrollViewHeight,
            footerViewHeight: footerViewHeight
          });
        }).exec();
    
        //计算子scroll-view组件高度
        wx.createSelectorQuery().select('#nav').boundingClientRect( (res) => {
          if(res && parseInt(res.height)>0){
            that.setData({
              subScrollViewHeight: that.data.scrollViewHeight - res.height
            });
          }
        }).exec();
      }
    });
  },
  scrollChange: function(e){
    var that = this;
    //当子scroll-view向上滚动的时候,判断nav导航条距离顶部的距离,如果大于0,则还没到顶部,此时使用scroll-top属性,将子scroll-view的滚动条重置到0
    wx.createSelectorQuery().select('#nav').boundingClientRect( (res) => {
      if(e.detail.deltaY<0){
       // console.log('向上滚动')
        if(res.top>0){
          that.setData({
            subScrollTop:0
          });
        }
      }
    }).exec();
  },
})
/**index.wxss**/
page{
  height: 100%;
}
.container{
  width: 750rpx;
  height: 100%;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.main{
  width: 750rpx;
  flex: 1;
}
.nav{
  height: 150rpx;
  background-color:blue;
  color:#fff;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.block{
  height: 300rpx;
  background-color: #f00;
  margin: 10rpx 0;
}

.footer{
  width: 750rpx;
  height: 150rpx;
  background-color: #ccc;
  display: flex;
  flex-direction: row;
  align-items: center;
}
.footer button{
  width: 200rpx;
  height: 50rpx;
  font-size: 26rpx;
  display: flex;
  align-items: center;
}

效果如下:

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,实现微信小程序 scroll-view 的 scroll-into-view 无效果可能是由以下几个原因导致的: 1. scroll-into-view 属性的值与要跳转到的锚点标记的 id 属性值一致。例如,如果要跳转到一个 id 为 "anchor1" 的锚点标记,那么 scroll-into-view 的值应该设置为 "anchor1"。 2. scroll-view 容器高度不足:如果 scroll-view 容器的高度不足以显示要跳转到的锚点标记,那么 scroll-into-view 将无法生效。确保 scroll-view 容器的高度足够大,以容纳所有的内容和要跳转到的锚点标记。 3. scroll-into-view 设置在子组件上:scroll-into-view 属性应该设置在 scroll-view 组件上,而不是其子组件上。确保 scroll-into-view 属性被正确地设置在 scroll-view 组件上。 4. scroll-into-view 设置在动态生成的内容上:如果要跳转到的锚点标记是在动态生成的内容,那么需要在动态生成内容之后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容渲染完成后才能生效。 5. scroll-into-view 设置在隐藏的内容上:如果要跳转到的锚点标记是在初始状态下是隐藏的内容,那么需要在显示该内容后再设置 scroll-into-view 属性。因为 scroll-into-view 属性需要等待内容显示后才能生效。 以下是一个示例代码,演示了如何在微信小程序使用 scroll-view 的 scroll-into-view 实现锚点跳转效果: ```html <scroll-view scroll-into-view="{{toView}}" scroll-y="true" style="height: 300px;"> <view id="anchor1">锚点1</view> <view id="anchor2">锚点2</view> <view id="anchor3">锚点3</view> <view id="anchor4">锚点4</view> <view id="anchor5">锚点5</view> </scroll-view> <button bindtap="scrollToAnchor">跳转到锚点2</button> ``` ```javascript Page({ data: { toView: '' }, scrollToAnchor: function() { this.setData({ toView: 'anchor2' }) } }) ``` 在上述示例scroll-view 组件设置了 scroll-into-view 属性为 toView 变量的值,当点击按钮时,toView 变量的值被设置为 "anchor2",从而实现了跳转到锚点2的效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值