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

相关文章

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

idea设置快捷键风格方式

《idea设置快捷键风格方式》在IntelliJIDEA中设置快捷键风格,打开IDEA,进入设置页面,选择Keymap,从Keymaps下拉列表中选择或复制想要的快捷键风格,点击Apply和OK即可使... 目录idea设www.chinasem.cn置快捷键风格按照以下步骤进行总结idea设置快捷键pyth

MyBatis配置文件中最常用的设置

《MyBatis配置文件中最常用的设置》文章主要介绍了MyBatis配置的优化方法,包括引用外部的properties配置文件、配置外置以实现环境解耦、配置文件中最常用的6个核心设置以及三种常用的Ma... 目录MyBATis配置优化mybatis的配置中引用外部的propertis配置文件⚠️ 注意事项X

idea粘贴空格时显示NBSP的问题及解决方案

《idea粘贴空格时显示NBSP的问题及解决方案》在IDEA中粘贴代码时出现大量空格占位符NBSP,可以通过取消勾选AdvancedSettings中的相应选项来解决... 目录1、背景介绍2、解决办法3、处理完成总结1、背景介绍python在idehttp://www.chinasem.cna粘贴代码,出

SpringBoot返回文件让前端下载的几种方式

《SpringBoot返回文件让前端下载的几种方式》文章介绍了开发中文件下载的两种常见解决方案,并详细描述了通过后端进行下载的原理和步骤,包括一次性读取到内存和分块写入响应输出流两种方法,此外,还提供... 目录01 背景02 一次性读取到内存,通过响应输出流输出到前端02 将文件流通过循环写入到响应输出流

SpringBoot+Vue3整合SSE实现实时消息推送功能

《SpringBoot+Vue3整合SSE实现实时消息推送功能》在日常开发中,我们经常需要实现实时消息推送的功能,这篇文章将基于SpringBoot和Vue3来简单实现一个入门级的例子,下面小编就和大... 目录前言先大概介绍下SSE后端实现(SpringBoot)前端实现(vue3)1. 数据类型定义2.

JavaWeb 中的 Filter组件详解

《JavaWeb中的Filter组件详解》本文详细介绍了JavaWeb中的Filter组件,包括其基本概念、工作原理、核心接口和类、配置方式以及常见应用示例,Filter可以实现请求预处理、响应后... 目录JavaWeb 中的 Filter 详解1. Filter 基本概念1.1 什么是 Filter1.

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

前端Visual Studio Code安装配置教程之下载、汉化、常用组件及基本操作

《前端VisualStudioCode安装配置教程之下载、汉化、常用组件及基本操作》VisualStudioCode是微软推出的一个强大的代码编辑器,功能强大,操作简单便捷,还有着良好的用户界面,... 目录一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2

Java JAR 启动内存参数配置指南(从基础设置到性能优化)

《JavaJAR启动内存参数配置指南(从基础设置到性能优化)》在启动Java可执行JAR文件时,合理配置JVM内存参数是保障应用稳定性和性能的关键,本文将系统讲解如何通过命令行参数、环境变量等方式... 目录一、核心内存参数详解1.1 堆内存配置1.2 元空间配置(MetASPace)1.3 线程栈配置1.