温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序怎么实现吸顶效果

发布时间:2021-04-27 09:44:35 阅读:268 作者:小新 栏目:web开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

小编给大家分享一下微信小程序怎么实现吸顶效果,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

最开始的时候,在小程序中实现吸顶效果,开发工具看起来还挺好的,但是在真机上就会有问题了。 原因是我不停的去 setData 会导致操作反馈延迟严重,无法及时将操作处理结果及时传递到视图层。

微信小程序怎么实现吸顶效果

后面就对代码进行了调整,避免不停的去setData

效果图

吸顶前

微信小程序怎么实现吸顶效果

吸顶后

微信小程序怎么实现吸顶效果

代码部分

wxml

<view ></view>
<view ></view>


<view class="navbar-wrap">
 <view class="column {{isFixedTop?'fixed':''}}" id="navbar">
  <view class="block active">新品推荐</view>
  <view class="block">限时优惠</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 ></view>
</block>

wxss

view, text {
 box-sizing: border-box;
 -moz-box-sizing: border-box;
 -webkit-box-sizing: border-box;
}

.navbar-wrap {
 width100%;
}

.navbar-wrap .column {
 width100%;
 height80rpx;
 display: flex;
 flex-direction: row;
 align-items: center;
 justify-content: space-around;
 background#fff;
 border-bottom: solid 1px #eee;

 top0;
 left0;
 z-index100;
}

.navbar-wrap .column.fixed {
 position: fixed;
}

/* 以下的代码不重要 */

.navbar-wrap .column .block {
 width25%;
 height80rpx;
 line-height80rpx;
 text-align: center;
 font-size30rpx;
 color#333;
 letter-spacing1px;
 position: relative;
}

.navbar-wrap .column .block::after {
 content"";
 width60%;
 height3px;
 border-radius2px;
 position: absolute;
 bottom0;
 left50%;
 transformtranslateX(-50%);
 background: transparent;
}

.navbar-wrap .column .block.active {
 color#1490ce;
 font-weight: bold;
}

.navbar-wrap .column .block.active::after {
 backgroundlinear-gradient(160degrgba(82202300.710%rgba(01402550.790%);
}

js

Page({
 data: {
  navbarInitTop0//导航栏初始化距顶部的距离
  isFixedTopfalse//是否固定顶部
 },

 /**
  * 生命周期函数--监听页面加载
  */
 onLoadfunction(options) {

 },

 /**
  * 生命周期函数--监听页面显示
  */
 onShowfunction() {
  var that = this;

  if (that.data.navbarInitTop == 0) {

   //获取节点距离顶部的距离
   wx.createSelectorQuery().select('#navbar').boundingClientRect(function(rect) {
    if (rect && rect.top > 0) {
     var navbarInitTop = parseInt(rect.top);
     that.setData({
      navbarInitTop: navbarInitTop
     });
    }
   }).exec();

  }
 },

 /**
  * 监听页面滑动事件
  */
 onPageScrollfunction(e) {
  var that = this;
  var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度

  //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
  var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
  //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
  if (that.data.isFixedTop === isSatisfy) {
   return false;
  }

  that.setData({
   isFixedTop: isSatisfy
  });
 }


})

看完了这篇文章,相信你对“微信小程序怎么实现吸顶效果”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×