Vue封装一个刻度尺组件,可滑动,显示刻度设置默认刻度

本文主要是介绍Vue封装一个刻度尺组件,可滑动,显示刻度设置默认刻度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Vue封装一个刻度尺组件,可滑动,显示刻度设置默认刻度


html代码

 <div ref="Hello" class="c-slide f-inline-block" @mousemove="mousemove" @click="a"><div class="slide-runway"  ref="name"  ><div class="slide-bar" ref="bar" :style="{width: x}"></div><div class="c-slide-button slide-btn"  ref="tu" :style="{left:x}"  @mousedown="move"><div class="c-slide-tooltip tooltip" >{{title}}</div></div></div><div class="slide-rule f-cl" style="display:flex"><span class="rule f-fl" style="width: 10%;">130以下</span><span class="rule f-fl rule-bg" style="width: 10%;" v-for="item in data" :key="item">{{item}}</span>               <span class="rule f-fl " style="width: 10%;">210以上</span></div>   </div>

实现效果
滑动显示刻度,设置默认位置
js部分

//设置默认数据Initial() {//初始化获取元素宽度let TotalWidth = this.$refs.name.clientWidth;//将数据划分let PerWidth = TotalWidth / 100;//调用默认身高this.linearAnimation(PerWidth, PerWidth * 40);},//初始化自动移动linearAnimation(step, target) {let intervalID = null;let begin = 0,step_len = step || 0;clearInterval(intervalID);intervalID = setInterval(() => {begin += step_len;if (begin >= target) {begin = target;clearInterval(intervalID);}// box.style.marginLeft = begin + "px";this.$refs.bar.style.width = begin + "px";this.$refs.tu.style.left = begin + "px";this.CalculatedWidth(begin);}, 50);},//计算实际所占比(显示框)CalculatedWidth(w) {//初始化获取元素宽度let TotalWidth = this.$refs.name.clientWidth;console.log("初始化h", TotalWidth);//将数据划分let PerWidth = TotalWidth / 100;//小小格子let smallWidth = PerWidth / 10;// 实际占比 实占多少格let WWidth = parseInt(w / PerWidth);//判断区间if (w <= PerWidth * 10) {this.title = "小于130";return;} else if (w >= PerWidth * 90) {this.title = "大于210";return;} else if (WWidth >= WWidth + PerWidth / 2) {WWidth = WWidth + 1;this.title = WWidth + 120 + "cm";return;} else if (WWidth <= WWidth + PerWidth / 2) {WWidth = WWidth;this.title = WWidth + 120 + "cm";return;} else if (WWidth == WWidth) {this.title = WWidth + 120 + "cm";return;}},move(event) {let odiv = event.target; //获取目标元素let fdiv = odiv.parentNode;let xdiv = fdiv.firstChild;let tdiv = this.$refs.tu;let disX = event.clientX - odiv.offsetLeft;document.onmousemove = e => {//鼠标按下并移动的事件//用鼠标的位置减去鼠标相对元素的位置,得到元素的位置let left = e.clientX - disX;//获取实际宽度let OriginalWidth = this.$refs.name.clientWidth;if (left <= OriginalWidth && left >= 0) {this.CalculatedWidth(left);let bfb = left / OriginalWidth + "%";let a = this.mousemove(event);let z = a + "%";this.x = z;//移动当前元素xdiv.style.width = left + "px";tdiv.style.left = left + "px";}};document.onmouseup = () => {document.onmousemove = null;document.onmouseup = null;};},a(event) {//获取let a = this.mousemove(event);if (event.offsetX == a) {event.offsetX == event.offsetX;}this.CalculatedWidth(event.offsetX);let z = a + "%";this.x = z;},mousemove(event) {//获取实际宽度let OriginalWidth = this.$refs.name.clientWidth;let a = event.offsetX;let c = a / OriginalWidth;let d = (c * 100).toFixed(4);let realVal = parseFloat(d).toFixed(4);if (realVal >= 100) return realVal == 100;return realVal;}

css样式

* {box-sizing: border-box;
}
.f-inline-block {display: inline-block;
}
.a {height: 16px;padding: 10px;background-color: #4d4d4d;
}.f-cl:after,
.f-cl:before {display: table;content: "";
}
.f-cl:after {clear: both;
}
.f-cl:before {clear: both;
}
.f-fl {float: left;
}
.c-slide {user-select: none;background-color: #f4f4f4;border-radius: 4px;width: 510px;height: 100px;cursor: pointer;padding: 14px 14px 0;
}
.c-slide .slide-runway {width: 100%;height: 6px;position: relative;background-color: #d8d8d8;
}
.c-slide .slide-bar {height: 6px;position: absolute;left: 0;top: 0;background-color: #9463f7;z-index: 1;
}
.c-slide .slide-btn {position: absolute;top: -4px;transform: translateX(-50%);z-index: 2;
}
.c-slide .rule {display: block;float: left;color: #333333;font-size: 11px;padding: 2px 0 0 3px;line-height: 14px;
}
.c-slide .rule:last-child {position: relative;
}
.c-slide .rule:last-child::before {content: "";position: absolute;display: block;width: 0;height: 10px;border-left: 1px solid #4d4d4d;top: 0;left: 0;
}
.c-slide .rule-bg {background: url(../assets/images/rule.079996f.png) no-repeat;
}
.c-slide-button {border: 3px solid #9463f7;width: 14px;height: 14px;border-radius: 50%;background-color: #fff;position: relative;
}
.c-slide-button .tooltip {transform: translate(-46%, -150%);
}
.c-slide-tooltip {padding: 5px;box-shadow: 0px 0px 4px 0px rgba(163, 163, 163, 0.36);border-radius: 4px;background-color: #fff;display: inline-block;color: #333333;white-space: nowrap;font-size: 12px;position: absolute;
}
.c-slide-tooltip:before {content: "";position: absolute;width: 0;height: 0;border-width: 5px;border-style: solid;border-color: transparent;border-left-color: #fff;border-bottom-color: #fff;transform: rotate(-45deg) translateX(-50%);bottom: 0px;box-shadow: -1px 1px 4px 0px rgba(163, 163, 163, 0.36);left: 50%;
}

《程序员》曾陪伴了无数开发者成长。《新程序员》全新归来,推荐给大家!
结语:

不管多么险峻的高山,总是为不畏艰难的人留下一条攀登的路。

这篇关于Vue封装一个刻度尺组件,可滑动,显示刻度设置默认刻度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/340485

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

Python用Flask封装API及调用详解

《Python用Flask封装API及调用详解》本文介绍Flask的优势(轻量、灵活、易扩展),对比GET/POST表单/JSON请求方式,涵盖错误处理、开发建议及生产环境部署注意事项... 目录一、Flask的优势一、基础设置二、GET请求方式服务端代码客户端调用三、POST表单方式服务端代码客户端调用四

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统