效果图:

实现过程:

  1. 设置默认的滚动条和滚动条背景

<view class="myscrollBlock"><view class="myscrollBg"></view><view class="myscroll" :style="`width:${scroll_width}rpx;left:${scroll_left}rpx;`"></view></view>
.myscrollBlock {position: relative;width: 100rpx;top: -30rpx;left: 50%;transform: translateX(-50%);.myscrollBg {width: 100rpx;height: 8rpx;background: rgba(197, 254, 255, 0.15);position: absolute;z-index: 2;border-radius: 10rpx;}.myscroll {height: 8rpx;background: #1CD5CD;border-radius: 10rpx;position: absolute;z-index: 3;}}

容器实际的总宽度(listWidth) = 容器内元素的(宽度+边距) * 数量

容器现有宽度( blockWidth) = 我的容器现有宽度为屏幕的宽度的百分之90

默认滚动条的宽度(滚动条) = 容器现有宽度 / 容器实际的总宽度 * 100

setMyScrooll() {this.scroll_width =0;this.scroll_left =0;let listWidth = (110+24) * this.list.lengthlet blockWidth = uni.getStorageSync('sysTemInfo').screenWidth * 0.9this.scroll_width = blockWidth / listWidth * 100
},
  1. 安装vuescroll插件

vuescroll 滚动条插件安装:

npm install vuescroll -S
  1. 根据插件的handleScroll时间,动态获取滚动的进度

handleScroll(vertical, horizontal, nativeEvent) {this.scroll_width = horizontal.barSize * 100this.scroll_left = (1 - horizontal.barSize) * horizontal.process * 100},

注:

1.为了隐藏IOS的默认滚动条,我设置了固定的外层高度,然后overflow: hidden;

2.代码中 vuescroll 组件的高度必须写在view内,因为组件会设置高度为100%,写在css优先级低会导致失效

页面完整代码

<template><view class="page" id="page"><image class="xinBj" :src="xinBj"></image><view class="menuBlock"><view class="menuItem" v-for="(item, index) in menus" :key="index" @click="chooseMenu(item, index)"><view class="titleBlock"><image v-if="item.show" class="menuBgIn" :src="menuIn"></image><image v-else class="menuBgNo" :src="menuNo"></image><view class="year" :class="item.show ? 'colorIn' : 'colorNo'" v-html="item.guideNameFull"></view></view><div v-if="item.show" class="offLineTable" style=""><vuescroll class="listBlock" style="height:302rpx;" scrollingY="false" @handle-scroll="handleScroll" :ops="ops"><view class="p_r" v-if="list.length>0"><view class="listItem p_l" v-for="(itemm, indexx) in list" :key="indexx"@click="chooseList(indexx, itemm)":style="listIdx === indexx ? `background-image:url(${itemBg}); background-repeat:no-repeat; background-size:100% 100%;-moz-background-size:100% 100%;` : ''"><image class="listIcon" :src="listIdx === indexx ? itemm.icon : itemm.icon"mode="heightFix"></image><!-- colorIn colorNo--><view class="txt" :class="listIdx === indexx ? '' : ''">{{ itemm.guideDetailName }}</view></view></view></vuescroll><view class="myscrollBlock"><view class="myscrollBg"></view><view class="myscroll" :style="`width:${scroll_width}rpx;left:${scroll_left}rpx;`"></view></view></div></view></view></view>
</template><script>import vuescroll from "vuescroll";export default {components: {vuescroll},data() {return {xinBj: this.$config.imageUrl + "xinBj.jpg",menuNo: this.$config.imageUrl + "fungusv2/menuNo.png?v=2",menuIn: this.$config.imageUrl + "fungusv2/menuIn.png?v=2",icon1_1: this.$config.imageUrl + "fungus/icon1_1.png",icon1: this.$config.imageUrl + "fungus/icon1.png",itemBg: this.$config.imageUrl + "fungusv2/itemBg.png",menus: [],menuIdx: 0,listIdx: 0,list: [],guideNameFull: "",scroll_process: 0,scroll_width: 0,scroll_left: 0,ops: {mode: "slide",locking: true,detectResize: true,sizeStrategy: 'percent',bar: {// rgba(197,254,255,0.15)// "#1CD5CD"background: "#1CD5CD", //滚动条的背景颜色opacity: 0,keepShow: true, // 一直显示滚动条size: '0',// color:"#1CD5CD"},rail: {background: 'rgba(197, 254, 255, 0.15)',opacity: 0,size: '0',},scrollPanel: {},}};},onLoad(option) {console.log("option.openid");if (option.openid) {console.log(option.openid);uni.setStorageSync("openid", option.openid);} else {uni.setStorageSync("openid", 123);}this.menuIdx = 0;this.$common.Init.call(this, "");// this.setData();this.getListFungus();console.log(location)},methods: {setMyScrooll() {this.scroll_width =0;this.scroll_left =0;let listWidth = this.list.length * 134let blockWidth = uni.getStorageSync('sysTemInfo').screenWidth * 0.9this.scroll_width = blockWidth / listWidth * 100},handleScroll(vertical, horizontal, nativeEvent) {this.scroll_width = horizontal.barSize * 100this.scroll_left = (1 - horizontal.barSize) * horizontal.process * 100},getText(str) {let words = str.replace(/<[^<>]+>/g, "").replace(/&nbsp;/gi, ""); //这里是去除标签return words.replace(/\s/g, ""); //这里是去除空格},getListFungus() {console.log(1);this.$request(this.$api.fungus.listFungus).then((res) => {console.log(res);res.data.infos.forEach((item, index) => {if (index == 0) {item.show = true;} else {item.show = false;}if (item.guideDetail) {item.guideDetail.forEach((itemm, indexx) => {itemm.show = false;});}});this.menus = res.data.infos;this.guideNameFull = this.menus[0].guideNameFull;let list = this.menus[0].guideDetail;this.list = list;this.setMyScrooll()});},chooseList(index, item) {this.listIdx = index;console.log(this.menus[index]);console.log(item);let params = {first_button: this.getText(this.guideNameFull),second_button: item.guideDetailName,};this.guidelines(params);let url = `/pages/fungusGuide/fungusDetail?guideDetailId=${item.guideDetailId}`;this.goOtherPages(url);},chooseMenu(item, index) {this.listIdx = 0;if (item.guideDetail) {this.menus.forEach((e) => {e.show = false;});item.show = true;let list = this.menus[index].guideDetail;this.list = list;this.setMyScrooll()this.guideNameFull = this.menus[index].guideNameFull;let params = {first_button: this.getText(this.guideNameFull),};this.guidelines(params);} else {uni.showToast({title: "内容开发中, 敬请期待!",icon: "none",});}},guidelines(params) {this.$request(this.$api.huoshi.ld_clinical_guidelines_fungus_button,params).then((res) => {console.log(res);});},},};
</script><style lang="scss">.page {}.myscrollBlock {position: relative;width: 100rpx;top: -30rpx;left: 50%;transform: translateX(-50%);.myscrollBg {width: 100rpx;height: 8rpx;background: rgba(197, 254, 255, 0.15);position: absolute;z-index: 2;border-radius: 10rpx;}.myscroll {height: 8rpx;background: #1CD5CD;border-radius: 10rpx;position: absolute;z-index: 3;}}.colorIn {color: #c5feff;}.colorNo {color: #1cd5cd;}.offLineTable {width: 90vw;margin-left: 5%;height: 290rpx;overflow: hidden;}.listBlock {width: 90vw;.listItem {display: flex;flex-shrink: 0;width: 228rpx;height: 228rpx;background: rgba(197, 254, 255, 0.1);justify-content: center;/*子元素水平居中*/align-items: center;/*子元素垂直居中*/display: -webkit-flex;text-align: center;margin-right: 20rpx;.listIcon {height: 74rpx;}}}.txt {font-size: 26rpx;margin-top: 20rpx;color: #1cd5cd;}.menuBlock {width: 100vw;position: relative;padding-top: 20rpx;.menuItem {margin-bottom: 30rpx;padding-bottom: 20rpx;width: 100vw;position: relative;font-size: 32rpx;background: rgba(197, 254, 255, 0.05);.titleBlock {display: flex;flex-direction: row;padding: 20rpx;padding-top: 50rpx;padding-bottom: 20rpx;}.year {// margin-top: -10rpx;margin-left: 20rpx;position: relative;z-index: 20;}.menuBgNo {width: 40rpx;height: 40rpx;z-index: 9;}.menuBgIn {width: 40rpx;height: 40rpx;z-index: 8;}}}
</style>

HTML自定义滚动条附效果图和完整源码相关推荐

  1. 大神论坛 逆向分析 Internet Download Manager 序列号算法 附IDM注册机完整源码

    1. 前言 idm version : 6.38 Build 23 2.算法逆向 IDM的序列号验证函数定位在: 下面是在IDA下的代码分析: .text:00510010 push ebp .tex ...

  2. 「 硬核教学」 ❤️ C语言编写扫雷游戏外挂❤️「 完整源码」

    目录 第一步:使用CE找出棋盘的基址 第二步:使用CE/OD查看该地址的数据 第三步:对比棋盘分析数据的含义 第四步:验证猜想 第五步:编写功能 有没有羡慕学霸们在机房里 潇洒如意地玩着扫雷无限通关, ...

  3. 大转盘完整源码附效果图,可以下载直接用

    本转盘实现功能,可以动态配置奖品和转盘相关的任何图片,可以灵活配置使用.是基于 uni-app 实现的.可以在小程序或者H5,各端兼容使用. 效果图:因为GIF图的掉帧,所以显示抽奖的转动速度慢,实际 ...

  4. C语言自定义实现string字符串的算法(附完整源码)

    C语言自定义实现string字符串的算法 string字符串的定义pstring.h(完整源码) string字符串的实现pstring.cpp(完整源码) string字符串的定义pstring.h ...

  5. H5 直播的疯狂点赞动画是如何实现的?(附完整源码)

    直播有一个很重要的互动:点赞. 为了烘托直播间的氛围,直播相对于普通视频或者文本内容,点赞通常有两个特殊需求: 点赞动作无限次,引导用户疯狂点赞 直播间的所有疯狂点赞,都需要在所有用户界面都动画展现出 ...

  6. H5 直播的疯狂点赞动画如何通过php直播平台源码实现(附完整源码)

    php直播平台源码H5 直播的疯狂点赞动画如何通过php直播平台源码实现(附完整源码) 直播有一个很重要的互动:点赞. 为了烘托直播间的氛围,直播相对于普通视频或者文本内容,点赞通常有两个特殊需求: ...

  7. A*寻路算法,循序渐进,附完整源码

    A*寻路算法-循序渐进(附完整源码) 用途 ​ A*寻路算法的一般用途即为灵活地寻找初始点到目标点的最短路径. 概述 ​ 灵活是A*算法更为注重的特性,可以任意添加障碍物,可以对不同地形的寻路损耗赋予 ...

  8. 微信小程序之授权登录(附完整源码)

    个人博客上已经同步更新了文章,有目录索引,阅读起来比较方便,欢迎大家移步个人博客上读阅~ 个人博客地址:http://zwd596257180.gitee.io/blog/2019/04/15/wec ...

  9. JavaScript实现唯一路径问题的动态编程方法的算法(附完整源码)

    JavaScript实现唯一路径问题的动态编程方法的算法(附完整源码) dpUniquePaths.js完整源代码 dpUniquePaths.test.js完整源代码 dpUniquePaths.j ...

最新文章

  1. sublime按ctrl+B不能运行
  2. OpenCV捕获格雷码模式
  3. Oracle入门(六)之用户操作
  4. Taro+react开发(77):taro项目目录介绍
  5. LeetCode 410. 分割数组的最大值(极小极大化 二分查找 / DP)
  6. 西部数码 php.ini,如何修改PHP的memory_limit限制
  7. java IO包装流如何关闭
  8. Sublime搭建Java程序运行环境
  9. React Native Modal组件 Android覆盖状态栏
  10. java中scanner类_Java里Scanner 类
  11. Android内核开发:在源码树中添加新的app应用
  12. OpenCV对矩形填充透明颜色
  13. Ubuntu下Vim的常用操作命令——vi编辑器常用命令
  14. zh-CN、zh-Hans区别
  15. 实时Linux之PREEMPT_RT篇
  16. shell中test命令用法详解
  17. 网络驱动器问题:指定的网络文件夹目前是以其他用户名和密码进行映射的
  18. Unity点击物体后,移动到物体所在位置
  19. 网络安全学习--008--SQL注入之Access数据库注入详解
  20. 广东移动UNT401H南传通过adb装当贝桌面

热门文章

  1. Pandas库的使用教程,每个程序都配图,两万字保姆教学轻松学会
  2. 云呐|固定资产计提折旧怎么算
  3. html图片转换特效,css3图片切换效果
  4. Excel数据分析常用函数④——日期函数(now,today,datedif,weekday,weeknum,text)
  5. 英雄联盟里,为什么要打一下,走一下
  6. 知道创宇CTO杨冀龙:网络安全人才决定行业格局
  7. oracle11g memory_target,oracle11g要求在操作系统层设定共享内存/dev/shm,且大于MEMORY_TARGET...
  8. 猿创征文 | 专做药品生产研发的程序员
  9. 计算机组成原理实验信号cpu,计算机组成原理实验43_实验报告
  10. gittrack_什么时候使用git branch –track(开始“看上游”的意思)?