uniapp 实现自定义缩略滚动条

embedded/2024/9/24 17:33:15/

<template><view class="container-scroll"><!-- 文字导航 --><scroll-view class="scroll-view-text" scroll-x="true" v-if="type === 1"><navigator:url="item.url"class="scroll-view-item":style="`width:${itemWidth}px`"v-for="(item, index) in tarbarList":key="index"><!-- 必须得多包一层 --><view class="text-container" :style="`width:${itemWidth}px`"><view class="text-nav">{{ item.title }}</view></view></navigator></scroll-view><!-- 图文导航 --><scroll-viewclass="img-scroll-view"scroll-x="true"@scroll="handleScroll"ref="scrollView"v-if="type === 2"><view class="scroll-view-container"><navigator:url="item.url"class="scroll-view-item":style="`width:${itemWidth}px`"v-for="(item, index) in tarbarList":key="index"><view class="nav-item"><view class="image-container" :style="imageStyle"><imageclass="image":src="item.image"mode="widthFix"lazy-load="false":style="imageStyle"></image></view><view class="text-title">{{ item.title }}</view></view></navigator></view></scroll-view></view><!-- 图文导航自定义滚动条 --><viewclass="custom-scroll-container"v-if="type === 2 && showNumber < tarbarList.length"><view class="custom-scroll"><viewclass="scroll":style="`transform:translateX(${translateXValue}px)`"/></view></view>
</template><script setup name="imageTextNavigator">
import { onMounted, ref, getCurrentInstance, computed } from "vue";
const props = defineProps({type: {// 1 图文导航 2 文字导航type: Number,default: 2,},// 首屏展示数量showNumber: {type: Number,default: 4,}, // 一屏显示的数量tarbarList: {type: Array,default: () => [{title: "导航导航导航导航",image: "",url: "",},{title: "导航导航导航导航",image: "",url: "",},{title: "导航导航导航导航",image: "",url: "",},{title: "导航导航导航导航",image: "",url: "",},{title: "导航5",image: "",url: "",},{title: "导航6",image: "",url: "",},],},
});const instance = getCurrentInstance();// 活动距离
const translateXValue = ref(0);
const itemWidth = ref(0);// 计算滚动距离
function handleScroll(event) {// 30 滑块父盒子的宽度// 8 滑块的宽度// scrollLeft 是 scroll-view  滚动的距离const scrollLeft = event.detail.scrollLeft; // 获取scroll-view滚动的距离const scrollWidth = event.detail.scrollWidth; // 获取scroll-view内容的总宽度const viewWidth = 375; // scroll-view的视口宽度// 滑块父容器的宽度为30,滑块的宽度为8,计算出滑块可移动的最大距离const maxTranslateX = 30 - 8; // 最大滑动距离 22// 计算滚动条的移动比例,将内容滚动的比例映射到滑块的移动范围内const moveRatio = maxTranslateX / (scrollWidth - viewWidth); // 滑块能够移动距离 比上 大容器能够滑动的距离,映射出 比率,最高就是 100% 嘛,等比换算// 计算滑块的实际位置,确保不会超过最大移动距离translateXValue.value = Math.min(scrollLeft * moveRatio, maxTranslateX);
}// 图片样式
const imageStyle = computed(() => {return {width: props.showNumber === 4 ? `44px` : `36px`,height: props.showNumber === 4 ? `44px` : `36px`,};
});function getElementInfo(id, context) {return new Promise((resolve, reject) => {let query = uni.createSelectorQuery().in(context);query.select(id).boundingClientRect((rect) => {if (rect) {// 获取的元素都是 px 需要乘以 2resolve(rect);} else {reject();}}).exec();});
}async function setNavItemWidth() {const eleInfo = await getElementInfo(".container-scroll", instance);const phoneWidth = eleInfo.width;itemWidth.value = phoneWidth / props.showNumber;
}onMounted(async () => {await setNavItemWidth();
});
</script><style lang="scss" scoped>
.container-scroll {width: 750rpx;position: relative;.scroll-view-text {white-space: nowrap;width: 750rpx;display: flex;.scroll-view-item {display: inline-block;background: #ffffff;.text-container {width: 150rpx;height: 100%;display: flex;justify-content: center;align-items: center;.text-nav {width: 112rpx;font-weight: 400;font-size: 28rpx;color: #b2945e;line-height: 28rpx;font-style: normal;text-transform: none;white-space: break-spaces;}}}}.img-scroll-view {display: flex;flex-wrap: nowrap;white-space: nowrap;box-sizing: border-box;width: 750rpx;.scroll-view-container {//   padding-left: 64rpx;display: inline-block;.scroll-view-item {display: inline-block;min-height: 154rpx;background: #ffffff;text-align: center;.nav-item {width: 100%;height: 100%;display: flex;justify-content: center;align-items: center;flex-direction: column;.text-title {width: 112rpx;font-weight: 400;font-size: 28rpx;color: #333333;line-height: 28rpx;white-space: break-spaces;margin-top: 24rpx;}.image-container {display: flex;justify-content: center;align-items: center;.image {background: #f3f3f3;border-radius: 0px 0px 0px 0px;}}}}}}
}
// 两种方案 绝对定位 \ 使用translateX
.custom-scroll-container {width: 100%;display: flex;justify-content: center;align-items: center;padding-top: 24rpx;.custom-scroll {width: 60rpx;overflow: hidden;height: 6rpx;background: #f3f3f3;border-radius: 4px 4px 4px 4px;position: relative;.scroll {width: 16rpx;height: 6rpx;background: #b2945e;border-radius: 4px 4px 4px 4px;/* 初始位置 */transform: translateX(0rpx);}}
}
</style>


http://www.ppmy.cn/embedded/92674.html

相关文章

CesskeCRM基于若依平台二次开发的CRM系统

CesskeCRM基于若依平台二次开发的开源CRM系统&#xff0c;以SaaS方向实现多租户功能&#xff0c;动态创建数据库&#xff0c;独立数据库安全级别&#xff0c;不是按字段隔离。 多租户功能&#xff0c;物理数据库隔离。前端采用Vue、Element UI。后端采用Spring Boot、Spring S…

VSCode 使用总结

VSCode 使用总结 一&#xff1a;c/c 代码格式化二&#xff1a;忽略文件三&#xff1a;VSCode ssh remote linux file四&#xff1a;vscode 大纲预览找不到文件符号五&#xff1a;vscode 实用插件六&#xff1a;预定义变量列表七&#xff1a;VSCode快捷键 一&#xff1a;c/c 代码…

扩展Conda的宇宙:使用conda config --append channels命令

扩展Conda的宇宙&#xff1a;使用conda config --append channels命令 Conda不仅是Python编程语言的包管理器&#xff0c;它还是一个强大的环境管理工具&#xff0c;用于安装和管理不同编程语言的软件包。Conda的灵活性在很大程度上得益于其channels系统&#xff0c;它允许用户…

【Linux】常用指令集合

目录 1.who&#xff1a;查看使用云服务器的账号 2.pwd&#xff1a;显示当前所处的工作目录 3.ls&#xff1a;列出当前目录中的文件和子目录 ls 查看目录中的文件和子目录 ls -l 或者 ll 显示文件和目录的详细信息 ls -a 列出全部文件&#xff0c;包含隐藏文件 4.cd&#xf…

【SpringMVC】详细介绍SpringMVC的执行流程

目录 1. 概念 2.SpringMVC工作原理 3. springMVC的简单使用 1.在pom.xml中导入相关依赖 2.在web.xml中配置dispatcherServlet 3.创建springMVC.xml核心配置文件 1. 概念 什么是MVC&#xff1f; MVC是下面三个组件的简写&#xff0c;模型&#xff08;Model&#xff09;、视图…

免费【2024】springboot 房地产销售管理系统的设计与实现

博主介绍&#xff1a;✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌ 技术范围&#xff1a;SpringBoot、Vue、SSM、HTML、Jsp、PHP、Nodejs、Python、爬虫、数据可视化…

68 类的定义与使用

Python 使用 class 关键字来定义类&#xff0c;class 关键字之后是一个空格&#xff0c;接下来是类的名字&#xff0c;如果派生自其他基类则需要把所有基类放到一对括号中并使用逗号分隔&#xff0c;然后是一个冒号&#xff0c;最后换行并定义类的内部实现。类名的首字母一般要…

B+树与B*树:扩展B树的高级应用

B树与B*树&#xff1a;扩展B树的高级应用 1. 引言 B树和B树是B树的扩展形式&#xff0c;它们在数据库系统、文件系统以及索引结构中广泛应用。B树和B树提供了更高效的数据查找、插入和删除操作&#xff0c;同时优化了存储空间的利用率。本篇文章将详细介绍B树和B*树的概念、特…