uni-app 吸顶方案总结

效果

在这里插入图片描述

页面级 uni.pageScrollTo

官方文档:https://uniapp.dcloud.net.cn/api/ui/scroll.html#pagescrollto

原生头部导航

uni.pageScrollTo({
	selector: '#tabs',
	duration: 300
});

(推荐)需要兼容自定义头部导航

在这里插入图片描述

<template>
  <view id="demo1" :style="{ height: '30vh', backgroundColor: 'red' }">
    A
  </view>
  <view
    id="demo2"
    :style="{
      height: '50vh',
      backgroundColor: 'yellow'
    }"
  >
    <button
      @click="onTop"
      :style="{
        position: 'sticky',
        top: safeBottom + 'px'
      }"
    >
      吸顶
    </button>
  </view>
  <view id="demo3" :style="{ height: '180vh', backgroundColor: 'green' }">
    C
  </view>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'

const pageScrollTop = ref(0)
const safeBottom = ref(0)
onMounted(() => {
  const query = uni.createSelectorQuery()
  query
    .select('#demo2')
    .boundingClientRect((data) => {
      const clientRect = uni.getMenuButtonBoundingClientRect()
      safeBottom.value = clientRect.bottom
      pageScrollTop.value = Math.floor(data.top) - clientRect.bottom
    })
    .exec()
})

function onTop() {
  uni.pageScrollTo({
    scrollTop: pageScrollTop.value, //滚动的距离
    duration: 10 //过渡时间
  })
}
</script>

</script>



微信是支持offsetTop配置的,但是不知道为什么uni中未生效
不然可以写成下边的样子

uni.pageScrollTo({
	offsetTop: uni.getMenuButtonBoundingClientRect().bottom,
	selector: '#tabs',
	duration: 300
});

组件级 scroll-view

官方文档:https://uniapp.dcloud.net.cn/component/scroll-view.html

(推荐)scroll-top

<template>
  <scroll-view
    scroll-y="true"
    :style="{ height: '100vh' }"
    :scroll-top="scrollTop"
  >
    <view id="demo1" :style="{ height: '30vh', backgroundColor: 'red' }">
      A
    </view>
    <view id="demo2" :style="{ height: '50vh', backgroundColor: 'yellow' }">
      <button @click="onTop">吸顶</button>
    </view>
    <view id="demo3" :style="{ height: '180vh', backgroundColor: 'green' }">
      C
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
const scrollTop = ref(0)
const initScrollTop = ref(0)

onMounted(() => {
  const query = uni.createSelectorQuery()
  query
    .select('#demo2')
    .boundingClientRect((data) => {
      if (!data) {
        return
      }
      const top = Math.floor(data.top)
      initScrollTop.value = top + 1 // 解决吸顶缝隙-模拟器有缝隙,真机没,保险起见
    })
    .exec()
})
function onTop() {
  if (scrollTop.value === initScrollTop.value) {
    scrollTop.value = initScrollTop.value + 0.1 // scrollTop新旧值的改变触发scroll-view的更新,否则不更新,
  } else {
    scrollTop.value = initScrollTop.value
  }
}
</script>

scroll-into-view

scroll-into-view 这个属性微信小程序无效。。抖音小程序生效

<template>
  <scroll-view
    scroll-y="true"
    :style="{ height: '100vh' }"
    :scroll-into-view="scrollIntoViewTarget"
  >
    <view id="demo1" :style="{ height: '30vh', backgroundColor: 'red' }"
      >A</view
    >
    <view id="demo2" :style="{ height: '50vh', backgroundColor: 'yellow' }">
      <button @click="onTop">吸顶</button>
    </view>
    <view id="demo3" :style="{ height: '180vh', backgroundColor: 'green' }"
      >C</view
    >
  </scroll-view>
</template>

<script setup >
import { ref, nextTick  } from 'vue'

const scrollIntoViewTarget = ref(null)
function onTop() {
  scrollIntoViewTarget.value = null
  nextTick(() => {
    scrollIntoViewTarget.value = 'demo2'
  })
}
</script>
  • 21
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_pengliang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值