uni-app 吸顶方案总结

效果

页面级 uni.pageScrollTo

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

原生头部导航

js 复制代码
uni.pageScrollTo({
	selector: '#tabs',
	duration: 300
});

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

js 复制代码
<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中未生效

不然可以写成下边的样子

js 复制代码
uni.pageScrollTo({
	offsetTop: uni.getMenuButtonBoundingClientRect().bottom,
	selector: '#tabs',
	duration: 300
});

组件级 scroll-view

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

(推荐)scroll-top

html 复制代码
<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 这个属性微信小程序无效。。抖音小程序生效

html 复制代码
<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>
相关推荐
平凡シンプル1 小时前
安卓 uniapp跨端开发
android·uni-app
艾小逗3 小时前
uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
小程序·uni-app·app·es6
鸭子嘎鹅子呱18 小时前
uniapp使用高德地图设置marker标记点,后续根据接口数据改变某个marker标记点,动态更新
uni-app·map·高德地图
计算机源码社1 天前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
Angus-zoe1 天前
uniapp+vue+微信小程序实现侧边导航
vue.js·微信小程序·uni-app
Pluto & Ethereal1 天前
uni-app尺寸单位、flex布局于背景图片
uni-app
天空下sky1 天前
uniapp+若依 开发租房小程序源码分享
uni-app
帅过二硕ฅ1 天前
uniapp点击跳转到对应位置
前端·javascript·uni-app
佩淇呢2 天前
uniapp vue3 梯形选项卡组件
前端·vue.js·uni-app
[廾匸]2 天前
uni-app获取设备唯一值、静态IP以及公网IP的方法
uni-app·ip·imei·唯一值