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

uniapp滚动导航 - unique

下面是效果图

QQ2025108-134329

下面是实现代码

<template><view class="container"><!-- 顶部导航 --><view class="navbar"><viewv-for="(item, index) in navList":key="index"class="nav-item":class="{ active: currentIndex === index }"@tap="scrollTo(index)">{{ item.title }}</view></view><!-- 内容区 --><scroll-viewclass="scroll-content"scroll-yscroll-with-animation:scroll-into-view="scrollId"@scroll="onScroll"><view v-for="(item, index) in navList" :key="index" class="section" :id="'sec' + index"><view class="section-title">{{ item.title }}</view><view class="section-content">{{ item.content }}</view></view></scroll-view></view>
</template><script setup>
import { ref, onMounted } from 'vue'const navList = ref([{ title: '首页', content: '这里是首页内容' },{ title: '功能', content: '这里是功能介绍内容' },{ title: '关于', content: '这里是关于我们内容' },
])const currentIndex = ref(0)
const scrollId = ref('')
const sectionTops = ref([])// 点击导航滚动
function scrollTo(index) {scrollId.value = 'sec' + indexcurrentIndex.value = index
}// 监听滚动事件,高亮当前section
function onScroll(e) {const scrollTop = e.detail.scrollTopfor (let i = 0; i < sectionTops.value.length; i++) {const top = sectionTops.value[i]const nextTop = sectionTops.value[i + 1] || Infinityif (scrollTop >= top - 60 && scrollTop < nextTop - 60) {currentIndex.value = ibreak}}
}onMounted(() => {// 获取每个section的位置
  const query = uni.createSelectorQuery().in(getCurrentInstance())query.selectAll('.section').boundingClientRect((data) => {sectionTops.value = data.map((item) => item.top)}).exec()
})
</script><style scoped>
.container {display: flex;flex-direction: column;height: 100vh;
}/* 顶部导航栏 */
.navbar {display: flex;justify-content: space-around;align-items: center;height: 50px;background-color: #fff;border-bottom: 1px solid #eee;position: sticky;top: 0;z-index: 10;
}.nav-item {color: #666;padding: 6px 10px;font-size: 14px;
}.nav-item.active {color: #fff;background-color: #409eff;border-radius: 4px;
}/* 滚动内容区域 */
.scroll-content {flex: 1;
}.section {height: 1200rpx;padding: 20rpx;box-sizing: border-box;
}.section:nth-child(odd) {background-color: #f6f6f6;
}
.section:nth-child(even) {background-color: #e8f1ff;
}.section-title {font-size: 18px;font-weight: bold;margin-bottom: 10rpx;
}
</style>

 

http://www.jsqmd.com/news/10107/

相关文章:

  • 滚动导航 - unique
  • windows剪切板工具
  • C#基础:启用线程池执行并行任务
  • P1545 [USACO04DEC] Dividing the Path G 题解
  • AJ-Report - 实践
  • 视频采集程序
  • java作业2
  • 关于PPT的课后作业
  • RK 系列 GPU 驱动检查方法
  • Day-15【选择与循环】选择结构-if语句 - 实践
  • 咕乡
  • opencv应用经典bug
  • Linux随记(十八) - 详解
  • 通信算法之281:大疆DJI无人机ID-DJI DroneID开源工程-相关问题-协议信息问题 - 实践
  • Linux 文件 MD5 校验完全指南
  • week2课后作业
  • Java 语言程序设计(第二讲 方法)动手动脑与课后实验问题整理文档 - 20243867孙堃2405
  • 算法第一章
  • 课后作业1
  • mac打开app提示文件损坏解决方案
  • 深入解析:RDMA简介3之四种子协议对比
  • QBXT2025S刷题 Day7题
  • 大语言模型中的注意力机制详解 - 教程
  • 无需重新训练即可更新语音识别词汇
  • 深入解析:vscode中无法使用npm node
  • 第一次算法作业
  • AI元人文:新的评价与启示
  • Why is English commonly used in scientific literature?
  • 第二次课程
  • 详细介绍:《云原生安全攻防》-- K8s网络策略:通过NetworkPolicy实现微隔离