js实现滑动进度条效果

作者:#麻辣小龙虾# 时间:2023-08-24 03:47:39 

本文实例为大家分享了js实现滑动进度条效果的具体代码,供大家参考,具体内容如下

js实现滑动进度条效果

进度条:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>js滑动进度条效果</title>
<style>
 *{margin:0;padding:0;user-select:none;}
 .progress-bar{position:relative;height:10px;width:400px;margin:200px auto;background:#ebeef5;border-radius:10px;}
 .progress-bar .pro-bar{position:absolute;left:0;height:10px;width:10px;background:#409eff;}
 .progress-bar .min-num{position:absolute;left:-20px;top:-5px;}
 .progress-bar .max-num{position:absolute;right:-40px;top:-5px;}
 .progress-bar .block {position:absolute;height:30px;width:10px;background:#ccc;top:-10px;border-radius:10px;}
 .progress-bar .block div{position:absolute;display:none;left:-20px;top:-45px;width:50px;height:30px;text-align:center;line-height:30px;background:#ccc;border-radius:20px;}
 .progress-bar .block:hover div{display:block;font-size:10%;color:#fff;background:#409eff;}
</style>
</head>
<body>
<div class="progress-bar">
<span class="min-num">0</span>
<span class="max-num">100</span>
<div class="pro-bar"></div>
<div class="block">
 <div>0</div>
</div>
</div>
</body>
<script>
(function(){
let moveBlock = document.querySelector('.block');
let proBar = document.querySelector('.pro-bar');
let flag = false;
let startX = null;
let moveMax = (400 - 10); // 背景条宽度减去滑块的宽度
moveBlock.onmousedown = function(e){
 startX = e.pageX;
 moveBlock.style.left ? moveBlock.style.left : moveBlock.style.left = '0px';
 let startLeft = parseInt(moveBlock.style.left);
 document.onmousemove = function(e){
 let moveX = (e.pageX - startX) > 0 ? true : false;
 let moveSection = startLeft + (e.pageX - startX);
 // 限定移动范围
 if (moveSection >= 0 && moveSection <= moveMax) {
  let percent = ((startLeft + (e.pageX - startX)) / moveMax).toFixed(4) * 100;
  percent.toString().length > 5 ? percent = percent.toString().subStr(0, 5) : percent = percent.toString();
  moveBlock.style.left = startLeft + (e.pageX - startX) + 'px';
  proBar.style.width = moveBlock.style.left;
  moveBlock.querySelector('div').innerText = percent + '%';
 }
 };
};
// 鼠标松开移除事件
moveBlock.onmouseup = function(){
 document.onmousemove = null;
};
})();
</script>
</html>

来源:https://blog.csdn.net/CodingNoob/article/details/102522168

标签:js,滑动,进度条
0
投稿

猜你喜欢

  • mysql中explain用法详解

    2024-01-13 16:28:25
  • Mysql锁内部实现机制之C源码解析

    2024-01-12 21:13:25
  • Golang实现WebSocket服务的项目实践

    2024-04-28 09:15:51
  • Git 命令行教程及实例教程(附github注册)

    2022-02-17 12:36:25
  • Django实现微信小程序的登录验证功能并维护登录态

    2022-03-14 22:56:48
  • 解决tensorflow模型压缩的问题_踩坑无数,总算搞定

    2021-12-18 03:09:23
  • Python的join函数的用法及实例

    2023-03-08 12:39:28
  • 教你用PyTorch部署模型的方法

    2023-07-23 17:52:30
  • python中matplotlib调整图例位置的方法实例

    2023-09-11 08:52:13
  • JavaScript继承模式粗探

    2024-04-23 09:25:20
  • Python 数据可视化之Matplotlib详解

    2022-12-30 05:33:21
  • Vue3.0中的monorepo管理模式的实现

    2024-04-28 09:24:51
  • Python 数据处理库 pandas进阶教程

    2022-04-18 01:17:13
  • YOLOv5在图片上显示统计出单一检测目标的个数实例代码

    2023-07-20 18:08:42
  • django3.02模板中的超链接配置实例代码

    2021-07-12 01:02:25
  • python的pstuil模块使用方法总结

    2022-10-09 22:47:16
  • Python 异步之如何保护任务免于取消详解

    2022-08-04 05:03:26
  • Javascript fso操作文件

    2008-07-30 12:51:00
  • golang gorm错误处理事务以及日志用法示例

    2024-04-25 13:18:50
  • Oracle 正则表达式实例详解

    2024-01-25 23:43:01
  • asp之家 网络编程 m.aspxhome.com