微信小程序?qū)崿F(xiàn)簡(jiǎn)單的吸頂效果
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)簡(jiǎn)單吸頂效果的具體代碼,供大家參考,具體內(nèi)容如下
需求:進(jìn)入頁(yè)面后首先看到banner布局,然后是tab切換欄以及頁(yè)面內(nèi)容,當(dāng)頁(yè)面滾動(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)鍵字搜索,空格隔開(kāi)" 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">限時(shí)優(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)聽(tīng)頁(yè)面加載 ? ?*/ ? onLoad: function (options) { ? }, ? /** ? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示 ? ?*/ ? onShow: function () { ? ? var that = this; ? ? if (that.data.navbarInitTop == 0) { ? ? ? console.log(that.data.navbarInitTop) ? ? ? //獲取節(jié)點(diǎn)距離頂部的距離 ? ? ? wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) { ? ? ? ? if (rect && rect.top > 0) { ? ? ? ? ? console.log(rect.top - 92) ? ? ? ? ? //92是頁(yè)面搜索框一欄整體的高度 ?若無(wú)搜索欄一項(xiàng)時(shí)可以不加此項(xiàng)目 ? ? ? ? ? var navbarInitTop = parseInt(rect.top - 92); ? ? ? ? ? that.setData({ ? ? ? ? ? ? navbarInitTop: navbarInitTop ? ? ? ? ? }); ? ? ? ? } ? ? ? }).exec(); ? ? } ? }, ? /** ? ?* 監(jiān)聽(tīng)頁(yè)面滑動(dòng)事件 ? ?*/ ? onPageScroll: function (e) { ? ? var that = this; ? ? var scrollTop = parseInt(e.scrollTop); //滾動(dòng)條距離頂部高度 ? ? // console.log(e.scrollTop) //滾動(dòng)149rpx ?//滾動(dòng)條距離頂部高度 ? ? //判斷'滾動(dòng)條'滾動(dòng)的距離 和 '元素在初始時(shí)'距頂部的距離進(jìn)行判斷 ? ? var isSatisfy = scrollTop >= (that.data.navbarInitTop) ? true : false; ? ? //為了防止不停的setData, 這兒做了一個(gè)等式判斷。 只有處于吸頂?shù)呐R界值才會(huì)不相等 ? ? if (that.data.isFixedTop === isSatisfy) { ? ? ? return false; ? ? } ? ? that.setData({ ? ? ? isFixedTop: isSatisfy ? ? }); ? } })
json部分代碼如下:
{ ? "navigationBarTitleText": "測(cè)試", ? "usingComponents": {} }
最終效果:
未吸頂:
吸頂后:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript事件Event對(duì)象詳解(屬性、方法、自定義事件)
Event對(duì)象代表事件的狀態(tài),比如事件在其中發(fā)生的元素、鍵盤按鍵的狀態(tài)、鼠標(biāo)的位置、鼠標(biāo)按鈕的狀態(tài),這篇文章主要給大家介紹了關(guān)于JavaScript事件Event對(duì)象(屬性、方法、自定義事件)的相關(guān)資料,需要的朋友可以參考下2024-01-01原生JS封裝ajax 傳json,str,excel文件上傳提交表單(推薦)
這篇文章主要介紹了原生JS封裝ajax 傳json,str,excel文件上傳提交表單(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06javascript 動(dòng)態(tài)腳本添加的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇javascript 動(dòng)態(tài)腳本添加的簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10