微信小程序仿淘宝,向下滚动,头部导航栏背景由透明编程白色(animate)

开发电商类的小程序过程中,有很多导航栏在滑动的时候背景色渐变,下面就来实现一下:

借助小程序的animate方法实现过渡效果,只不过目前这一功能只能绑定scroll-view元素。

1.wxml页面

<scroll-view
  id="scrollBox"
  class="view-page"
  scroll-y="{{true}}"
  throttle="{{false}}"
  enhanced="{{true}}"
  binddragstart="scrollStart"
  bindscrolltoupper="scrollToUp"
  >  
  <view class="navbar custom-class" style='height:{{navHeight}}px;'>
    <button class="navbtn back" bindtap="_navBack" style='top:{{navTop}}px;' >
      <text class="iconfont icon-jiantou-left"></text>
    </button>
    <view class='navbar-title' style='top:{{navTop}}px;'>通告详情</view>
  </view>
</scroll-view>

2.js页面

_animate() {
    wx.createSelectorQuery().select('#scrollBox').fields({
      scrollOffset: true,
      size: true,
    }, (res) => {
      //为每个元素添加过渡动画
      this.animate('.navbar', [{
        backgroundColor: 'transparent'
      }, {
        backgroundColor: '#ffffff'
      }], 1000, {
        scrollSource: '#scrollBox',
        timeRange: 1000,
        startScrollOffset: 0,
        endScrollOffset: 40,
      }),
      this.animate('.back', [{
        color: '#fff',
        backgroundColor: 'rgba(0, 0, 0, 0.4)'
      }, {
        color: '#333',
        backgroundColor: 'transparent'
      }], 1000, {
        scrollSource: '#scrollBox',
        timeRange: 1000,
        startScrollOffset: 0,
        endScrollOffset: 40,
      }),
      this.animate('.navbar-title', [{
        opacity: '0',
      }, {
        opacity: '1',
      }], 1000, {
        scrollSource: '#scrollBox',
        timeRange: 1000,
        startScrollOffset: 0,
        endScrollOffset: 40,
      })
    }).exec()
},

附加功能:

在滑动的时候有时候需要动态改变时间栏的颜色,这就需要监听scroll-view的滚动情况,bindscrolltoupper可以监听滚动到顶部时的回调事件,binddragstart可以监听滚动开始时的回调事件,此方法需要借助enhanced={{true}}时才有效果(小程序开发工具中没有效果,但在真机测试中有效果,至今不知道为什么)

//滚动开始改变时间栏颜色
scrollStart: function () {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: 'transparent'
    })
},
//滚动到顶部改变时间栏颜色
scrollToUp: function () {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: 'transparent',
    })
},

好了,一个仿淘宝的滚动渐变导航栏就完成了!

完整案例

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

inticaler

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值