微信小程序吸顶组件

微信小程序吸顶组件:

在移动端,页面过长时,为了提高用户体验,会使用吸顶将重要信息固定于顶部,所以封装了一个吸顶组件,方便以后使用

组件界面:

未吸顶
吸顶

可以扫码查看是否符合自己的需求
在这里插入图片描述

wxml:

<!--components/stickyTop/index.wxml-->
<view class="sticky-container" style="height: {{ containerInfo.top <= top ? (containerHeight + 'px') : 'auto' }}">
  <view class="sticky-position {{ containerInfo.top <= top ? 'position-fixed' : ''  }}" style="width: {{ containerInfo.width }}px; left: {{ containerInfo.left }}px; top: {{ top }}px;">
    <slot></slot>
  </view>
</view>

js:(由于小程序没有在组件内没有监听页面滚动的方法,所以在使用此组件时需要传页面滚动的高度,若各位大佬有更好的方法可以交流交流,小弟学习一下~)

// components/stickyTop/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    scrollTop: {
      type: Number,
      value: 0,
      observer(newData, oldData) {
        this.getStickyOffsetTop()
      }
    },
    top: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    containerHeight: '',
    containerInfo: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取当前容器的offsetTop
    getStickyOffsetTop() {
      wx.createSelectorQuery().in(this).select('.sticky-container').boundingClientRect(res => {
        // console.log(res);
        if (res.top < this.properties.top) {
          this.setData({
            containerHeight: res.height
          })
        }
        this.setData({
          containerInfo: res
        })
      }).exec()
    }
  }
})


wxss:

/* components/stickyTop/index.wxss */
.position-fixed{
  position: fixed;
  top: 0;
}

使用:

  1. json文件
"usingComponents": {
    "sticky-top": "../../components/stickyTop"
  }
  1. wxml文件
<!--pages/useStickyTop/index.wxml-->
<view class="container">
  <view class="green-block"></view>
  <view class="green-block"></view>
  <sticky-top scrollTop="{{ scrollTop }}" top="0">
    <view class="red-block">吸顶</view>
  </sticky-top>
  <view class="yellow-block"></view>
  <view class="green-block"></view>

  <sticky-top scrollTop="{{ scrollTop }}" top="200">
    <view class="yellow-block">吸顶-距顶部200的距离</view>
  </sticky-top>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
  <view class="green-block"></view>
</view>
  1. js 文件中
  /**
   * 监听页面滚动事件
   */
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  1. wxss
/* pages/useStickyTop/index.wxss */
.green-block{
  height: 200rpx;
  margin: 40rpx;
  background-color: var(--primary);
}
.red-block{
  margin: 0 40rpx;
  height: 200rpx;
  line-height: 200rpx;
  background-color: rgb(236, 83, 48);
  text-align: center;
  color: #ffffff;
}
.yellow-block{
  margin: 0 40rpx;
  height: 100rpx;
  line-height: 100rpx;
  background-color: rgb(199, 186, 6);
  text-align: center;
  color: #ffffff;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值