微信小程序?qū)崿F(xiàn)簡單的吸頂效果
本文實例為大家分享了微信小程序?qū)崿F(xiàn)簡單吸頂效果的具體代碼,供大家參考,具體內(nèi)容如下
需求:進(jìn)入頁面后首先看到banner布局,然后是tab切換欄以及頁面內(nèi)容,當(dāng)頁面滾動到一定距離后,tab切換欄始終固定在頂部
wxml部分代碼如下:
<!--pages/test/test.wxml-->
<view style="padding-bottom:30rpx;position:fixed;top:0;width:100%;background:#2ea7e0;">
?? ?<view class="wehx-top_input weui-flex">
?? ??? ?<!-- 搜索框 -->
?? ??? ?<view class="weui-flex__item">
?? ??? ??? ?<view class="wehx-search__form">
?? ??? ??? ??? ?<view class="wehx-search__box">
?? ??? ??? ??? ??? ?<icon class="weui-icon-search_in-box" type="search" size="17" color="#0C98C5"></icon>
?? ??? ??? ??? ??? ?<input type="text" class="wehx-search__input" placeholder-class="wehx-input_placeholder" placeholder="關(guān)鍵字搜索,空格隔開" confirm-type="search" bindconfirm="search" value="{{search}}" />
?? ??? ??? ??? ??? ?<view class="weui-icon-clear" wx:if="{{search}}" bindtap="clearSearch">
?? ??? ??? ??? ??? ??? ?<icon type="clear" size="14" color="#2ea7e0"></icon>
?? ??? ??? ??? ??? ?</view>
?? ??? ??? ??? ?</view>
?? ??? ??? ?</view>
?? ??? ?</view>
?? ??? ?<view bindtap='toSend' style="display:flex;flex-direction:column;justify-content:space-between;align-items:center;padding:0 20rpx;color:#ffffff;">
?? ??? ??? ?<text class="iconfont icon-fabu-copy" style="font-size:40rpx;line-height:40rpx;"></text>
?? ??? ??? ?<view style="color:#ffffff;font-size:20rpx;padding-top:6rpx;">發(fā)單</view>
?? ??? ?</view>
?? ?</view>
</view>
<!-- 用于吸頂后 占位用的 -->
<view style="height:92rpx;width:100%;"></view>
<view style="width: 90%; height: 300rpx; background: red; margin: 30rpx auto;"></view>
<view class="navbar-wrap">
?? ?<view class="column {{isFixedTop?'fixed':''}}" id="navbar">
?? ??? ?<view class="block active">新品推薦</view>
?? ??? ?<view class="block">限時優(yōu)惠</view>
?? ??? ?<view class="block">火爆熱搜</view>
?? ??? ?<view class="block">猜你喜歡</view>
?? ?</view>
</view>
<block wx:for="{{20}}" wx:key="list">
?? ?<view style="width: 100%; height: 120rpx; background: #f0f0f0; margin: 20rpx auto;"></view>
</block>wxss部分代碼如下:
/* pages/test/test.wxss */
view,
text {
? box-sizing: border-box;
? -moz-box-sizing: border-box;
? -webkit-box-sizing: border-box;
}
.navbar-wrap {
? width: 100%;
}
.navbar-wrap .column {
? width: 100%;
? height: 82rpx;
? display: flex;
? flex-direction: row;
? align-items: center;
? justify-content: space-around;
? background: #fff;
? border-bottom: solid 1px #eee;
? /* top: 0; */
? left: 0;
? z-index: 100;
}
.navbar-wrap .column.fixed {
? position: fixed;
? top: 92rpx;
}
/* 以下的代碼不重要 */
.navbar-wrap .column .block {
? width: 25%;
? height: 80rpx;
? line-height: 80rpx;
? text-align: center;
? font-size: 30rpx;
? color: #333;
? letter-spacing: 1px;
? position: relative;
}
.navbar-wrap .column .block::after {
? content: "";
? width: 60%;
? height: 3px;
? border-radius: 2px;
? position: absolute;
? bottom: 0;
? left: 50%;
? transform: translateX(-50%);
? background: transparent;
}
.navbar-wrap .column .block.active {
? color: #1490ce;
? font-weight: bold;
}
.navbar-wrap .column .block.active::after {
? background: linear-gradient(160deg, rgba(8, 220, 230, 0.7) 10%, rgba(0, 140, 255, 0.7) 90%);
}
/* 頂部欄目布局 */
.wehx-search__form {
? overflow: hidden;
? background: #fff;
? border-radius: 30rpx;
}
.wehx-top_input {
? height: 62rpx;
? padding-left: 20rpx;
? background: #2ea7e0;
}
.wehx-search__box {
? position: relative;
? padding-left: 68rpx;
}
.wehx-search__input {
? height: 62rpx;
? font-size: 28rpx;
? border-radius: 32.5rpx;
? margin-right: 50px;
}js部分代碼如下:
Page({
? data: {
? ? navbarInitTop: 0, //導(dǎo)航欄初始化距頂部的距離
? ? isFixedTop: false, //是否固定頂部
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面加載
? ?*/
? onLoad: function (options) {
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面顯示
? ?*/
? onShow: function () {
? ? var that = this;
? ? if (that.data.navbarInitTop == 0) {
? ? ? console.log(that.data.navbarInitTop)
? ? ? //獲取節(jié)點距離頂部的距離
? ? ? wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
? ? ? ? if (rect && rect.top > 0) {
? ? ? ? ? console.log(rect.top - 92)
? ? ? ? ? //92是頁面搜索框一欄整體的高度 ?若無搜索欄一項時可以不加此項目
? ? ? ? ? var navbarInitTop = parseInt(rect.top - 92);
? ? ? ? ? that.setData({
? ? ? ? ? ? navbarInitTop: navbarInitTop
? ? ? ? ? });
? ? ? ? }
? ? ? }).exec();
? ? }
? },
? /**
? ?* 監(jiān)聽頁面滑動事件
? ?*/
? onPageScroll: function (e) {
? ? var that = this;
? ? var scrollTop = parseInt(e.scrollTop); //滾動條距離頂部高度
? ? // console.log(e.scrollTop) //滾動149rpx ?//滾動條距離頂部高度
? ? //判斷'滾動條'滾動的距離 和 '元素在初始時'距頂部的距離進(jìn)行判斷
? ? var isSatisfy = scrollTop >= (that.data.navbarInitTop) ? true : false;
? ? //為了防止不停的setData, 這兒做了一個等式判斷。 只有處于吸頂?shù)呐R界值才會不相等
? ? if (that.data.isFixedTop === isSatisfy) {
? ? ? return false;
? ? }
? ? that.setData({
? ? ? isFixedTop: isSatisfy
? ? });
? }
})json部分代碼如下:
{
? "navigationBarTitleText": "測試",
? "usingComponents": {}
}最終效果:
未吸頂:

吸頂后:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單介紹JavaScript數(shù)據(jù)類型之隱式類型轉(zhuǎn)換
這篇文章主要介紹了簡單介紹JavaScript數(shù)據(jù)類型之隱式類型轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2015-12-12
腳本之家貼圖轉(zhuǎn)換+轉(zhuǎn)貼工具用到的js代碼超級推薦
[紅色]腳本之家貼圖轉(zhuǎn)換+轉(zhuǎn)貼工具用到的js代碼超級推薦...2007-04-04
JavaScript中的宏任務(wù)和微任務(wù)執(zhí)行順序
在?JavaScript?中,宏任務(wù)和微任務(wù)是指在執(zhí)行代碼的過程中的兩種不同的任務(wù)類型,這篇文章主要介紹了JavaScript中的宏任務(wù)和微任務(wù)執(zhí)行順序,需要的朋友可以參考下2022-12-12
ant design中實現(xiàn)table的表格行的拖拽
這篇文章主要介紹了ant design中實現(xiàn)table的表格行的拖拽,文章圍繞table表格行拖拽實現(xiàn)的相關(guān)資料展開詳細(xì)的代碼內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下2022-03-03
JS控件bootstrap suggest plugin使用方法詳解
這篇文章主要介紹了JS控件bootstrap suggest plugin的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03

