当前位置: 首页 > news >正文

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><viewid="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.bottompageScrollTop.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-viewscroll-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-viewscroll-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 = nullnextTick(() => {scrollIntoViewTarget.value = 'demo2'})
}
</script>
http://www.lryc.cn/news/424273.html

相关文章:

  • 【C#】知识汇总
  • 1、Unity【基础】3D数学
  • 虚拟机ubuntu22的扩容记录
  • Docker 常用配置
  • 通过示例了解 .NET Core 中的依赖注入
  • fetch、FormData上传多张图片
  • C++STL详解(五)——list类的具体实现
  • 鸿蒙(API 12 Beta3版)【使用投播组件】案例应用
  • 【STM32项目】在FreeRtos背景下的实战项目的实现过程(一)
  • C#垃圾处理机制相关笔记
  • C语言memcmp函数
  • 低代码: 组件库测试之Vue环境下的测试工具以及测试环境搭建
  • 【Vue3】高颜值后台管理模板推荐
  • 详细介绍Pytorch中torchvision的相关使用
  • AI部署——主流模型推理部署框架
  • PyTorch之loading fbgemm.dll异常的解决办法
  • Vscode——如何实现 Ctrl+鼠标左键 跳转函数内部的方法
  • 力扣热题100_回溯_78_子集
  • 浏览器如何工作(一)进程架构
  • 【LeetCode】两数之和
  • UE5学习笔记11-为拿取武器添加动画
  • 68. 文本左右对齐【 力扣(LeetCode) 】
  • 【中等】 猿人学web第一届 第6题 js混淆-回溯
  • 低、中、高频率段具体在不同应用中的范围是多少
  • Oxford Model600 Model400低温氦压缩机cryogenic helium compressor手侧
  • Golang面试题四(并发编程)
  • 计算机学生高效记录并整理编程学习笔记的方法
  • 【书生大模型实战】L2-LMDeploy 量化部署实践闯关任务
  • 《编程学习笔记之道:构建知识宝库的秘诀》
  • DETR论文,基于transformer的目标检测网络 DETR:End-to-End Object Detection with Transformers