当前位置:   article > 正文

微信小程序Skyline搜索框吸顶到navtab胶囊位置,丝滑Q弹动画

微信小程序Skyline搜索框吸顶到navtab胶囊位置,丝滑Q弹动画

进入下面小程序可以体验效果

基于官方原版demo加入了回弹动画

WXML:

  1. <scroll-view
  2. class="scroll-area"
  3. type="custom"
  4. scroll-y
  5. show-scrollbar="{{false}}"
  6. worklet:onscrollend="handleScrollEnd"
  7. worklet:onscrollupdate="handleScrollUpdate"
  8. >
  9. <view class="fake-nav-bar" />
  10. <sticky-section push-pinned-header="{{false}}">
  11. <sticky-header>
  12. <view class="search-container">
  13. <view class="search">
  14. <view class="search-icon-wrp">
  15. <image class="search-icon" src="/assets/image/search.png" />
  16. </view>
  17. <view class="search-text">搜索</view>
  18. <view class="search-btn">搜索</view>
  19. </view>
  20. </view>
  21. </sticky-header>
  22. <view>1211111111111111111111111111111111</view>
  23. <view>1211111111111111111111111111111111</view>
  24. </sticky-section>
  25. </scroll-view>

js:

  1. const app = getApp() //这段代码需要放在page页面JS的最顶部
  2. const systemInfo = wx.getSystemInfoSync()
  3. const { shared, Easing,spring ,timing,sequence,delay} = wx.worklet
  4. const lerp = function (begin, end, t) {
  5. 'worklet'
  6. return begin + (end - begin) * t
  7. }
  8. const clamp = function (cur, lowerBound, upperBound) {
  9. 'worklet'
  10. if (cur > upperBound) return upperBound
  11. if (cur < lowerBound) return lowerBound
  12. return cur
  13. }
  14. Page({
  15. /**
  16. * 页面的初始数据
  17. */
  18. data: {
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad(options) {
  24. this.saftPadding = shared(0)
  25. this.searchBarheight = shared(0)
  26. this.searchBarWidth = shared(100)
  27. this.searchBarLeft = shared(0)
  28. this.navBarOpactiy = shared(1)
  29. this.isTop = shared(false) //是否触顶了
  30. this.isBottom = shared(false) //是否触底了
  31. this.isExc = shared(false) //是否执行动画了
  32. this.isScrolling = shared(false) //是否正在滑动
  33. },
  34. /**
  35. * 生命周期函数--监听页面初次渲染完成
  36. */
  37. onReady() {
  38. this.applyAnimatedStyle('.nav-bar', () => {
  39. 'worklet'
  40. return {
  41. opacity: this.navBarOpactiy.value
  42. }
  43. })
  44. this.applyAnimatedStyle('.search', () => {
  45. 'worklet'
  46. return {
  47. width: `${this.searchBarWidth.value}%`,
  48. left: `${this.searchBarLeft.value}%`,
  49. height: `${this.searchBarheight.value}px`
  50. }
  51. })
  52. this.applyAnimatedStyle('.search-container', () => {
  53. 'worklet'
  54. return {
  55. backgroundColor: (this.navBarOpactiy.value > 0) ? 'transparent' : '#fff',
  56. paddingTop: `${this.saftPadding.value}px`
  57. }
  58. })
  59. wx.nextTick(()=>{
  60. //胶囊顶部位置(安全位置)
  61. this.saftPadding.value = app.globalData.menu.top
  62. // //胶囊高度
  63. this.searchBarheight.value = app.globalData.menu.height
  64. })
  65. },
  66. handleScrollUpdate(evt) {
  67. 'worklet'
  68. const maxDistance = 60
  69. const scrollTop = clamp(evt.detail.scrollTop, 0, maxDistance)
  70. const progress = scrollTop / maxDistance
  71. const EasingFn = Easing.cubicBezier(0.4, 0.0, 0.2, 1.0)
  72. this.searchBarLeft.value = lerp(0, 25, EasingFn(progress))
  73. this.navBarOpactiy.value = lerp(1, 0, progress)
  74. const t1= lerp(100, 45, EasingFn(progress))
  75. if(t1>45&&t1<100){
  76. this.isScrolling.value=true
  77. }
  78. if((t1>45&&t1<100)&&(!this.isExc.value||this.isScrolling.value)){
  79. this.searchBarWidth.value = t1-1.5
  80. }
  81. if(t1===45&&evt.detail.scrollTop>60&&!this.isTop.value){
  82. //靠顶了
  83. this.isTop.value=true
  84. this.isBottom.value=false
  85. this.searchBarWidth.value = t1-2
  86. this.searchBarWidth.value = spring(t1)
  87. this.isExc.value=true
  88. this.isScrolling.value=false
  89. }else if(t1===100&&evt.detail.scrollTop<2&&!this.isBottom.value){
  90. //靠底了
  91. this.isBottom.value=true
  92. this.isTop.value=false
  93. this.searchBarWidth.value = t1-2
  94. this.searchBarWidth.value = spring(t1)
  95. this.isExc.value=true
  96. this.isScrolling.value=false
  97. }
  98. },
  99. handleScrollEnd(evt){
  100. 'worklet'
  101. this.isScrolling.value=false
  102. this.isExc.value=false
  103. },
  104. /**
  105. * 生命周期函数--监听页面显示
  106. */
  107. onShow(){
  108. },
  109. /**
  110. * 生命周期函数--监听页面隐藏
  111. */
  112. onHide() {
  113. },
  114. /**
  115. * 生命周期函数--监听页面卸载
  116. */
  117. onUnload() {
  118. },
  119. /**
  120. * 页面相关事件处理函数--监听用户下拉动作
  121. */
  122. onPullDownRefresh() {
  123. },
  124. /**
  125. * 页面上拉触底事件的处理函数
  126. */
  127. onReachBottom() {
  128. },
  129. /**
  130. * 用户点击右上角分享
  131. */
  132. onShareAppMessage() {
  133. }
  134. })

WXSS: 

  1. .fake-nav-bar {
  2. height: 60px;
  3. }
  4. .search-container {
  5. padding: 0 16px 10px 16px;
  6. }
  7. .search {
  8. display: flex;
  9. flex-direction: row;
  10. box-sizing: border-box;
  11. width: 100%;
  12. height: 40px;
  13. border-radius: 20px;
  14. border: 2px solid #07c160;
  15. position: relative;
  16. align-items: center;
  17. background-color: #fff;
  18. }
  19. .search-text {
  20. color: #8f8888;
  21. font-size: 14px;
  22. }
  23. .search-icon-wrp {
  24. display: flex;
  25. width: 30px;
  26. height: 100%;
  27. flex-direction: row;
  28. align-items: center;
  29. justify-content: center;
  30. }
  31. .search-icon {
  32. width: 16px;
  33. height: 16px;
  34. }
  35. .search-btn {
  36. position: absolute;
  37. right: 0;
  38. width: 60px;
  39. height: 100%;
  40. border-radius: 20px;
  41. background-color: #07c160;
  42. display: flex;
  43. align-items: center;
  44. justify-content: center;
  45. color: #FFF;
  46. font-size: 16px;
  47. /* font-weight: bold; */
  48. }
  49. .nav-bar {
  50. background-color: #fff;
  51. position: absolute;
  52. }
  53. .nav-left {
  54. display: flex;
  55. flex-direction: row;
  56. align-items: center;
  57. }
  58. .nav-logo {
  59. width: 40px;
  60. height: 40px;
  61. border-radius: 50%;
  62. }
  63. .nav-title {
  64. margin-left: 2px;
  65. font-size: 20px;
  66. color: #3f3e3e;
  67. /* font-weight: bold; */
  68. }
  69. .scroll-area {
  70. height: 100vh;
  71. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/308029
推荐阅读
相关标签
  

闽ICP备14008679号