微信小程序实现页面上滑展示不同的样式

app.js

// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.calculateNavigationBar()

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  calculateNavigationBar() {
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight,
          topHeight = menuButtonObject.top, //胶囊按钮与顶部的距离
          dotHeight = menuButtonObject.height,
          navHeight = topHeight + dotHeight,
          // navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2, //导航高度
          safeBottomHeight = res.screenHeight - res.safeArea.bottom
        this.globalData.customTopHeiht = {
          navHeight: navHeight,
          topHeight: topHeight,
          dotHeight: dotHeight,
          statusBarHeight: statusBarHeight,
          safeBottomHeight: safeBottomHeight,
        }
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  globalData: {
    userInfo: null,
    customTopHeiht: {},
  }
})

实现片段

wxml

<view  class="topbg {{ isFixedTop ? 'topbg1' : '' }}" style="height: {{ customTopHeiht.navHeight + 12 }}px;">
  <view class="toptitle {{ isFixedTop ? 'toptitle1' : '' }}" style="top: {{ customTopHeiht.topHeight }}px;line-height:{{customTopHeiht.dotHeight}}px;height: {{customTopHeiht.dotHeight}}px;">
    <block wx:if="{{ isFixedTop }}">
      <input placeholder="请输入关键字"/>
    </block>
    <block wx:else>测试上滑</block>
  </view>
</view>
<view class="box" style="margin-top: {{ customTopHeiht.navHeight + 12 }}px;">
  <view class="single" wx:for="{{ num }}" wx:key="index">{{ index }}</view>
</view>

js

const app = getApp()
Page({
    data: {
        num: 20,
        isFixedTop: false
    },
    onLoad() {
        this.setData({ customTopHeiht: app.globalData.customTopHeiht })
    },
    onPageScroll: function (e) {
        if (this.data.timer) {
            clearTimeout(this.data.timer)
        }
        this.data.timer = setTimeout(() => {
            let scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
            if (scrollTop > 300) {
                setTimeout(() => {
                    this.setData({
                        isFixedTop: true
                    })
                }, 200)
            } else  {
                this.setData({
                    isFixedTop: false
                })
            }
            clearTimeout(this.data.timer)
        }, 500)
    }
})

wxss

.box {
  padding: 30rpx;
  box-sizing: border-box;
}
.single {
  height: 300rpx;
  line-height: 300rpx;
  margin-bottom: 30rpx;
  box-shadow: 0rpx 0rpx 2rpx 2rpx #eee;
}

.single:last-child {
  margin-bottom: 0rpx;
}
.topbg {
  width: 100%;
  position: fixed;
  top: 0rpx;
  left: 0rpx;
  z-index: 100000;
  background: linear-gradient(180deg, #EEB992 0%, #EEB992 100%, transparent 100%);
}

.topbg1 {
  background: linear-gradient(180deg, #F8EFEA 0%, #FCF8F5 100%, transparent 100%);
}

.topbg .toptitle {
  width: 100%;
  color: #000000;
  font-size: 34rpx;
  font-weight: bold;
  height: fit-content;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  z-index: 1000;
}

.topbg .toptitle1 {
  padding-left: 30rpx;
  justify-content: flex-start;
}

.topbg input {
  width: 60%;
  line-height: 48rpx;
  height: 48rpx;
  display: block;
  font-size: 24rpx;
  padding-left: 30rpx;
  border-radius: 24rpx;
  border: 2rpx solid rgb(184, 181, 181);
}

json

{
  "usingComponents": {},
  "navigationStyle":"custom"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值