微信小程序----导航栏透明渐变一

来源:互联网 发布:建模数据哪里找 编辑:程序博客网 时间:2024/05/02 04:26

导航栏透明渐变效果

导航栏透明渐变

实现原理

  1. 利用position:absolute在导航下定位一个view作为背景渐变使用;
  2. 通过改变改view的opacity来实现透明渐变。

WXML

<!--pages/scroll/scroll.wxml--><view style="height:100%;position:fixed;width:100%;">  <scroll-view scroll-y="false"  bindscroll="scroll" style="height:100%;">    <view class="page-group">      <view class="page-group-position" style="opacity:{{scrollTop / 400 > 0.9 ? 0.9 : scrollTop / 400}}"></view>      <view class="page-nav-list"><text>首页</text></view>      <view class="page-nav-list"><text>活动</text></view>      <view class="page-nav-list"><text>菜单</text></view>      <view class="page-nav-list"><text>我的</text></view>    </view>    <view class="page-banner">      banner    </view>    <view class="goods-list">      goods-list1    </view>    <view class="goods-list list2">      goods-list2    </view>    <view class="goods-list list3">      goods-list3    </view>    <view class="goods-list list4">      goods-list4    </view>  </scroll-view></view>

WXSS

.page-banner{height: 500rpx;background-color: greenyellow;padding: 20rpx;color:#fff;padding-top: 100rpx;}.page-group{  display: table;  width: 100%;  table-layout: fixed;  position: fixed;  top: 0;  left: 0;  z-index: 10;}.page-group-position{  width: 100%;  height: 100%;  position: absolute;  top: 0;  left: 0;  background-color: blueviolet;  opacity: 0;  z-index: -1;}.page-nav-list{  padding:30rpx 0 ;  display: table-cell;  text-align: center;  width: 100%;  color: #fff;}.goods-list{  height: 500rpx;  background-color: green;  padding: 20rpx;  color:#fff;}.list2{background-color: blue;}.list3{background-color: yellow;}.list4{background-color: red;}

JS

Page({  data: {    scrollTop: null  },  //滚动条监听  scroll: function (e) {    this.setData({ scrollTop: e.detail.scrollTop })  },})

总结:
1. 需要scroll-view组件配合使用才能获取scrollTop;
2. scrollTop / 400 > 0.9,这里400的距离是根据需求改变,0.9是背景最后的半透明值,可以根据页面调节。

DEMO下载

更多微信小程序实例请点击:http://blog.csdn.net/m0_38082783/article/details/78853722