本文實例講述了微信小程序MUI導航欄透明漸變功能。分享給大家供大家參考,具體如下:
導航欄透明漸變效果
實現原理
1. 給page-group設置的背景顏色采用rgba;
2. 通過改變rgba其中a的值來實現透明漸變。
WXML
<view style="height:100%;position:fixed;width:100%;"> <scroll-view scroll-y="false" bindscroll="scroll" style="height:100%;"> <!-- 改變位置,去掉多余的view,直接改變背景色的透明度實現透明漸變 --> <view class="page-group" style="background-color: rgba(138, 43, 226,{{scrollTop / 400 > 0.9 ? 0.9 : scrollTop / 400}});"> <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;}/*去掉多余的class,直接設置背景色為rgba格式*/.page-group{ display: table; width: 100%; table-layout: fixed; background-color: rgba(138, 43, 226,0); position: fixed; top: 0; left: 0; z-index: 10;}.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.優點是去掉了多余的view空和class代碼,減少了代碼,實現了相同的效果。
2.缺點就是在微信開發者工具中,保存刷新不能獲取當前位置scrollTop,只有滾動才能獲取scrollTop。
希望本文所述對大家微信小程序開發有所幫助。
新聞熱點
疑難解答