uni-app 小程序 sticky 固定定位失效

文章讲述了作者在uni-app小程序项目中遇到的my-search组件使用sticky定位失效的问题,发现原因是设置top:0而非默认的left:0,解决后提示开发者在使用sticky定位时优先设置top以避免未知问题。
摘要由CSDN通过智能技术生成

今天在继续复习某马优购的uni-app小程序项目的时候,又踩到了第一次次固定定位的坑,给搜索框组件添加了sticky定位也不起作用,查找了半天百度,主要都说设置固定元素的高度超过了父容器的高度,或者没有给定位之一,再者就是父容器设置了overflow属性,我按照他们的建议试了后还是不起作用,代码片段如下:

此为首页引用my-search组件的代码:

<template>
  <view>
    <my-search></my-search>
    <view class="swipwerContainer">
      <!-- 给轮播图设置宽高必须设置在swiper上,设置在swiperItem上有奇怪问题 -->
     <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true" class="swiperFath">
       <swiper-item v-for="(item,index) in swiperList" :key="index" class="swiperItem">
         <navigator  :url="`/subpkg/goods_detail/goods_detail?${item.navigator_url.split('?')[1]}`">
             <image :src="item.image_src" ></image>
           </navigator>
       </swiper-item>
      </swiper>
    </view>
    <!-- 分类导航 -->
    <view class="navListContainer"> 
      <view class="navList">
        <view class="navItem" v-for="(item,index) in navList" :key="index" @click="switchPage(item)">
          <image :src="item.image_src" mode=""></image>
        </view> 
      </view>
    </view>
    <!-- 楼层导航区域 -->
    <view class="floorContainer">
      <view class="floorList">
        <view class="flooItem" v-for="(item,index) in floorList" :key="index">
          <view class="floorTitle">
            <image :src="item.floor_title.image_src"></image>
          </view>
          <view class="floorContent">
            <navigator class="floorContentLeft" :url="item.product_list[0].navigator_url">
              <image :src="item.product_list[0].image_src" ></image>
            </navigator>
            <view class="floorContentRight">
              <navigator class="floorContentRightItem" v-for="(item2,index2) in item.product_list" v-if="index2!==0" :url="item2.navigator_url" :key="index2">
                <image :src="item2.image_src"  ></image>
              </navigator>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

此为my-search组件中的代码:

<template>
  <view class="searchContainer">
    <view class="search-input" @click="gotoSearch">
      <uni-icons type="search"></uni-icons>
      <text>搜索</text>
    </view>
  </view>
</template>
<style lang="scss">
.searchContainer{
  position: sticky;
  left: 0;
  // bottom: 0;
  z-index: 999;
  background-color: #f8f3ec;
  height: 60px;
  // border: 1px solid red;
  display: flex;
  justify-content: center;
  align-items: center;
  .search-input{
   width: 90%;
   height: 40px;
   border-radius:20px;
   line-height: 40px;
   text-align: center;
   background-color: white;
   & uni-icons{
     margin-right: 3px;
   }
  }
}
</style>

可以看到,我在my-search组件的样式里已经设置了sticky定位,并且也给定了定位四值之一left:0,且在home页面里引用my-search组件也是在view的根节点下,不存在说有单独设置overflow:hidden这种情况,但是就是不行,第一次搞这个的时候也是卡了很久,当时莫名其妙就好了,没搞清楚,结果又卡在这里了

滑动时搜索框被拉上去运行截图:

解决办法

后面试了半天,修改了my-search组件样式里的定位属性值为top:0,就没问题了,并且我又反复单独测试了其他三个定位值right,bottom,和一开始的left,设置后sticky依然无效,但只要加上top:0位置设定后,就起作用了,真机调试也没有异常,这个我暂时也想不通为什么,不过算是解决了一半,有知道的伙伴可以分享一下。

修改后的my-search组件中的代码如下:(就只是将.searchContainer样式的left:0替换为了top:0)

<template>
  <view class="searchContainer">
    <view class="search-input" @click="gotoSearch">
      <uni-icons type="search"></uni-icons>
      <text>搜索</text>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-search",
    data() {
      return {
      };
    },
    methods:{
     gotoSearch(){
       uni.navigateTo({
         url:'/subpkg/search/search'
       })
     }
    },
  }
</script>

<style lang="scss">
.searchContainer{
  position: sticky;
  top: 0;
  z-index: 999;
  background-color: #f8f3ec;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  .search-input{
   width: 90%;
   height: 40px;
   border-radius:20px;
   line-height: 40px;
   text-align: center;
   background-color: white;
   & uni-icons{
     margin-right: 3px;
   }
  }
}
</style>

sticky定位修复后搜索框正常固定截图:
在这里插入图片描述

总结

后续使用sticky定位设置相对位置时,还是应该优先设置top,这样可以避免出现这种莫名的问题

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值