微信小程序?qū)崿F(xiàn)吸頂效果
最開(kāi)始的時(shí)候,在小程序中實(shí)現(xiàn)吸頂效果,開(kāi)發(fā)工具看起來(lái)還挺好的,但是在真機(jī)上就會(huì)有問(wèn)題了。 原因是我不停的去 setData 會(huì)導(dǎo)致操作反饋延遲嚴(yán)重,無(wú)法及時(shí)將操作處理結(jié)果及時(shí)傳遞到視圖層。

后面就對(duì)代碼進(jìn)行了調(diào)整,避免不停的去setData
效果圖
吸頂前

吸頂后

代碼部分
wxml
<view style="width: 90%; height: 300rpx; background: #f0f0f0; margin: 30rpx auto;"></view>
<view style="width: 90%; height: 300rpx; background: #f0f0f0; 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 class="column" wx:if="{{isFixedTop}}"></view>
</view>
<block wx:for="{{20}}" wx:key="list">
<view style="width: 100%; height: 120rpx; background: #f0f0f0; margin: 20rpx auto;"></view>
</block>
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: 80rpx;
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;
}
/* 以下的代碼不重要 */
.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%);
}
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) {
//獲取節(jié)點(diǎn)距離頂部的距離
wx.createSelectorQuery().select('#navbar').boundingClientRect(function(rect) {
if (rect && rect.top > 0) {
var navbarInitTop = parseInt(rect.top);
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)條距離頂部高度
//判斷'滾動(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
});
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
layer 刷新某個(gè)頁(yè)面的實(shí)現(xiàn)方法
今天小編就為大家分享一篇layer 刷新某個(gè)頁(yè)面的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
JavaScript判斷瀏覽器對(duì)CSS3屬性是否支持的多種方法
其實(shí)在使用css3的一些屬性時(shí),為了兼顧低端瀏覽器對(duì)CSS3的不友好性,往往需要知道某些瀏覽器是否支持要使用的CSS3屬性,以此來(lái)做向下適配。比如常見(jiàn)的CSS3動(dòng)畫就很有必要檢測(cè)瀏覽器是否支持。下面本文就分享了幾種方法,有需要的朋友們可以參考借鑒。2016-11-11
Typescript 中的 interface 和 type 到底有什么區(qū)別詳解
這篇文章主要介紹了Typescript 中的 interface 和 type 到底有什么區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
JavaScript+Node.js寫一款markdown解析器
這篇文章主要介紹了利用JavaScript和Node.js寫一款markdown解析器,首先編寫getHtml函數(shù),傳入markdown文本字符串,下面更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-02-02

