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>
相关推荐
anyup2 小时前
🔥🔥 10 天 Star 破百!uView Pro 文档也开源啦:完全免费、无广告、高效上手
前端·vue.js·uni-app
fakaifa12 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_9159184118 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
梦里寻码1 天前
自行食用 uniapp 多端 手写签名组件
前端·uni-app
不如摸鱼去2 天前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程
小白_ysf2 天前
uniapp 开发微信小程序,获取经纬度并且转化详细地址(单独封装版本)
微信小程序·uni-app
iOS阿玮2 天前
三年期已满,你的产品不再更新将于90天后下架。
uni-app·app·apple
bug总结3 天前
深入理解 uni-app 的 uni.createSelectorQuery()
uni-app
真上帝的左手3 天前
25. 移动端-uni-app
uni-app
编程猪猪侠3 天前
基于Uni-app+vue3实现微信小程序地图固定中心点范围内拖拽选择位置功能(分步骤详解)
uni-app