经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序实现简单的吸顶效果
来源:jb51  时间:2022/5/23 10:36:44  对本文有异议

本文实例为大家分享了微信小程序实现简单吸顶效果的具体代码,供大家参考,具体内容如下

需求:进入页面后首先看到banner布局,然后是tab切换栏以及页面内容,当页面滚动到一定距离后,tab切换栏始终固定在顶部

wxml部分代码如下:

  1. <!--pages/test/test.wxml-->
  2. <view style="padding-bottom:30rpx;position:fixed;top:0;width:100%;background:#2ea7e0;">
  3. ?? ?<view class="wehx-top_input weui-flex">
  4. ?? ??? ?<!-- 搜索框 -->
  5. ?? ??? ?<view class="weui-flex__item">
  6. ?? ??? ??? ?<view class="wehx-search__form">
  7. ?? ??? ??? ??? ?<view class="wehx-search__box">
  8. ?? ??? ??? ??? ??? ?<icon class="weui-icon-search_in-box" type="search" size="17" color="#0C98C5"></icon>
  9. ?? ??? ??? ??? ??? ?<input type="text" class="wehx-search__input" placeholder-class="wehx-input_placeholder" placeholder="关键字搜索,空格隔开" confirm-type="search" bindconfirm="search" value="{{search}}" />
  10. ?? ??? ??? ??? ??? ?<view class="weui-icon-clear" wx:if="{{search}}" bindtap="clearSearch">
  11. ?? ??? ??? ??? ??? ??? ?<icon type="clear" size="14" color="#2ea7e0"></icon>
  12. ?? ??? ??? ??? ??? ?</view>
  13. ?? ??? ??? ??? ?</view>
  14. ?? ??? ??? ?</view>
  15. ?? ??? ?</view>
  16. ?? ??? ?<view bindtap='toSend' style="display:flex;flex-direction:column;justify-content:space-between;align-items:center;padding:0 20rpx;color:#ffffff;">
  17. ?? ??? ??? ?<text class="iconfont icon-fabu-copy" style="font-size:40rpx;line-height:40rpx;"></text>
  18. ?? ??? ??? ?<view style="color:#ffffff;font-size:20rpx;padding-top:6rpx;">发单</view>
  19. ?? ??? ?</view>
  20. ?? ?</view>
  21. </view>
  22. <!-- 用于吸顶后 占位用的 -->
  23. <view style="height:92rpx;width:100%;"></view>
  24. <view style="width: 90%; height: 300rpx; background: red; margin: 30rpx auto;"></view>
  25. <view class="navbar-wrap">
  26. ?? ?<view class="column {{isFixedTop?'fixed':''}}" id="navbar">
  27. ?? ??? ?<view class="block active">新品推荐</view>
  28. ?? ??? ?<view class="block">限时优惠</view>
  29. ?? ??? ?<view class="block">火爆热搜</view>
  30. ?? ??? ?<view class="block">猜你喜欢</view>
  31. ?? ?</view>
  32. </view>
  33. <block wx:for="{{20}}" wx:key="list">
  34. ?? ?<view style="width: 100%; height: 120rpx; background: #f0f0f0; margin: 20rpx auto;"></view>
  35. </block>

wxss部分代码如下:

  1. /* pages/test/test.wxss */
  2. view,
  3. text {
  4. ? box-sizing: border-box;
  5. ? -moz-box-sizing: border-box;
  6. ? -webkit-box-sizing: border-box;
  7. }
  8.  
  9. .navbar-wrap {
  10. ? width: 100%;
  11. }
  12.  
  13. .navbar-wrap .column {
  14. ? width: 100%;
  15. ? height: 82rpx;
  16. ? display: flex;
  17. ? flex-direction: row;
  18. ? align-items: center;
  19. ? justify-content: space-around;
  20. ? background: #fff;
  21. ? border-bottom: solid 1px #eee;
  22. ? /* top: 0; */
  23. ? left: 0;
  24. ? z-index: 100;
  25. }
  26.  
  27. .navbar-wrap .column.fixed {
  28. ? position: fixed;
  29. ? top: 92rpx;
  30. }
  31.  
  32. /* 以下的代码不重要 */
  33. .navbar-wrap .column .block {
  34. ? width: 25%;
  35. ? height: 80rpx;
  36. ? line-height: 80rpx;
  37. ? text-align: center;
  38. ? font-size: 30rpx;
  39. ? color: #333;
  40. ? letter-spacing: 1px;
  41. ? position: relative;
  42. }
  43.  
  44. .navbar-wrap .column .block::after {
  45. ? content: "";
  46. ? width: 60%;
  47. ? height: 3px;
  48. ? border-radius: 2px;
  49. ? position: absolute;
  50. ? bottom: 0;
  51. ? left: 50%;
  52. ? transform: translateX(-50%);
  53. ? background: transparent;
  54. }
  55.  
  56. .navbar-wrap .column .block.active {
  57. ? color: #1490ce;
  58. ? font-weight: bold;
  59. }
  60.  
  61. .navbar-wrap .column .block.active::after {
  62. ? background: linear-gradient(160deg, rgba(8, 220, 230, 0.7) 10%, rgba(0, 140, 255, 0.7) 90%);
  63. }
  64.  
  65. /* 顶部栏目布局 */
  66. .wehx-search__form {
  67. ? overflow: hidden;
  68. ? background: #fff;
  69. ? border-radius: 30rpx;
  70. }
  71.  
  72. .wehx-top_input {
  73. ? height: 62rpx;
  74. ? padding-left: 20rpx;
  75. ? background: #2ea7e0;
  76. }
  77.  
  78. .wehx-search__box {
  79. ? position: relative;
  80. ? padding-left: 68rpx;
  81. }
  82.  
  83. .wehx-search__input {
  84. ? height: 62rpx;
  85. ? font-size: 28rpx;
  86. ? border-radius: 32.5rpx;
  87. ? margin-right: 50px;
  88. }

js部分代码如下:

  1. Page({
  2. ? data: {
  3. ? ? navbarInitTop: 0, //导航栏初始化距顶部的距离
  4. ? ? isFixedTop: false, //是否固定顶部
  5. ? },
  6. ? /**
  7. ? ?* 生命周期函数--监听页面加载
  8. ? ?*/
  9. ? onLoad: function (options) {
  10.  
  11. ? },
  12. ? /**
  13. ? ?* 生命周期函数--监听页面显示
  14. ? ?*/
  15. ? onShow: function () {
  16. ? ? var that = this;
  17. ? ? if (that.data.navbarInitTop == 0) {
  18. ? ? ? console.log(that.data.navbarInitTop)
  19. ? ? ? //获取节点距离顶部的距离
  20. ? ? ? wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
  21. ? ? ? ? if (rect && rect.top > 0) {
  22. ? ? ? ? ? console.log(rect.top - 92)
  23. ? ? ? ? ? //92是页面搜索框一栏整体的高度 ?若无搜索栏一项时可以不加此项目
  24. ? ? ? ? ? var navbarInitTop = parseInt(rect.top - 92);
  25. ? ? ? ? ? that.setData({
  26. ? ? ? ? ? ? navbarInitTop: navbarInitTop
  27. ? ? ? ? ? });
  28. ? ? ? ? }
  29. ? ? ? }).exec();
  30. ? ? }
  31. ? },
  32. ? /**
  33. ? ?* 监听页面滑动事件
  34. ? ?*/
  35. ? onPageScroll: function (e) {
  36. ? ? var that = this;
  37. ? ? var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
  38. ? ? // console.log(e.scrollTop) //滚动149rpx ?//滚动条距离顶部高度
  39. ? ? //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
  40. ? ? var isSatisfy = scrollTop >= (that.data.navbarInitTop) ? true : false;
  41. ? ? //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
  42. ? ? if (that.data.isFixedTop === isSatisfy) {
  43. ? ? ? return false;
  44. ? ? }
  45. ? ? that.setData({
  46. ? ? ? isFixedTop: isSatisfy
  47. ? ? });
  48. ? }
  49. })

json部分代码如下:

  1. {
  2. ? "navigationBarTitleText": "测试",
  3. ? "usingComponents": {}
  4. }

最终效果:

未吸顶:

吸顶后:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号