当前位置: 首页 > news >正文

封装一个滑块控制灯光组件

效果如下gif

只进行了基础的事件和布局,可优化的地方:luminance-box这个div加上后,由于和slider-run-way都是absolute定位,导致slider-run-way的点击事件无法设置值,只能通过滑块设置。暂时想不到咋处理,有想法可以讨论一下

html布局

<div class="light-slider"><divclass="slider-run-way"@click.stop="handleClickRunway($event)"ref="runwayRef"><div class="active-bar" :style="{ width: activeWidth + '%' }"></div><divclass="control-wrapper":style="{ left: activeWidth + '%' }"@mousedown="handleMousedown($event)"><div class="control-btn"><span></span><span></span></div></div></div><div class="luminance-box" @click="handleRight"><img src="../../../../assets/home/homepage_control_bright.png" /><div class="right-box"><span class="num">{{ activeWidth }}%</span><span class="l-title">luminance</span></div></div></div>

js部分

export default {name: "lightingControl",props: {isDisabled: {type: Boolean,default: true,},},data() {return {activeWidth: 13,runWayWidth: 0, // 滑道总宽度startX: 0,dragging: false,isClick: true,startPosition: 0,};},mounted() {this.$nextTick(() => {this.runWayWidth = this.$refs.runwayRef.clientWidth;});},methods: {handleClickRunway(e) {if (!this.isClick || this.isDisabled) return;console.log(e);const { runWayWidth } = this;const { offsetX } = e;const percent = parseInt((offsetX / runWayWidth) * 100);this.activeWidth = percent;},handleRight(e) {return false;},handleMousedown(e) {if (this.isDisabled) return;e.preventDefault();this.startX = e.clientX;this.onDragStart(e);window.addEventListener("mousemove", this.onDragging);window.addEventListener("mouseup", this.onDragEnd);},onDragStart(e) {this.dragging = true;this.isClick = true;this.startPosition = parseFloat(this.activeWidth);},onDragging(e) {e.stopPropagation();if (this.dragging) {this.isClick = false;let currentX = e.clientX;let diff = ((currentX - this.startX) / this.runWayWidth) * 100;this.newPosition = this.startPosition + diff;this.setPosition(this.newPosition);}},onDragEnd() {if (this.dragging) {setTimeout(() => {this.dragging = false;if (!this.isClick) {this.setPosition(this.newPosition);this.isClick = true;}}, 0);window.removeEventListener("mousemove", this.onDragging);window.removeEventListener("mouseup", this.onDragEnd);}},setPosition(newPosition) {if (newPosition < 0) {newPosition = 0;} else if (newPosition > 100) {newPosition = 100;}this.activeWidth = Math.round(newPosition);},},
};

css


.light-slider {margin-top: 16px;position: relative;.slider-run-way {width: 100%;position: relative;cursor: pointer;height: 70px;background: #d7eeff;border-radius: 4px 4px 4px 4px;.active-bar {height: 100%;position: absolute;left: 0;border-top-left-radius: 4px;border-bottom-left-radius: 4px;background: #36c1fd;}.control-wrapper {display: flex;align-items: center;justify-content: center;height: 30px;width: 22px;position: absolute;user-select: none;z-index: 1001;top: 20px;transform: translateX(-50%);background-color: transparent;.control-btn {width: 16px;height: 24px;background: #fff;box-shadow: 0px 0px 5px 0px rgba(76, 115, 184, 0.5);border-radius: 2px 2px 2px 2px;color: #36c1fd;display: flex;align-items: center;justify-content: space-evenly;span {width: 2px;height: 6px;background: #36c1fd;border-radius: 30px 30px 30px 30px;}}}}.luminance-box {display: flex;width: 100%;height: 70px;justify-content: space-between;align-items: center;position: absolute;left: 0;top: 0;img {width: 20px;height: 20px;margin-left: 14px;}.l-title {font-size: 14px;}.right-box {color: #2e2e48;display: flex;flex-direction: column;align-items: center;font-weight: bolder;margin-right: 10px;.num {font-size: 24px;}}}
}

http://www.lryc.cn/news/197444.html

相关文章:

  • flutter循环
  • 2.3 如何使用FlinkSQL读取写入到JDBC(MySQL)
  • Flink日志收集到数据库/kafka
  • Go项目踩坑:go get下载超时,goFrame框架下的go项目里将vue项目的dist同步打包发布,go项目打包并压缩
  • DataCon【签到题】挖矿流量检测
  • Vivado详细使用教程 | LED闪烁示例
  • 一些经典的神经网络(第17天)
  • Hadoop-HA-Hive-on-Spark 4台虚拟机安装配置文件
  • Hutool工具类参考文章
  • 【 Python ModuleNotFoundError: No module named ‘xxx‘可能的解决方案大全】
  • eclipse 配置selenium环境
  • 数据挖掘(6)聚类分析
  • 在启智平台上安装anconda
  • 棒球省队建设实施办法·棒球1号位
  • 架构案例2017(五十二)
  • 给四个点坐标计算两条直线的交点
  • 从入门到进阶 之 ElasticSearch SpringData 继承篇
  • 中文编程开发语言工具编程案例:计时计费管理系统软件连接灯控器编程案例
  • YOLOv7改进:动态蛇形卷积(Dynamic Snake Convolution),增强细微特征对小目标友好,实现涨点 | ICCV2023
  • 从文心大模型4.0与FuncGPT:用AI为开发者打开新视界
  • Nginx集群负载均衡配置完整流程
  • 如何生成SSH服务器的ed25519公钥SHA256指纹
  • 设计模式:抽象工厂模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
  • ocpp-远程启动(RemoteStartTransaction)、远程停止(RemoteStopTransaction)
  • 【网络安全】安全的系统配置
  • conda使用一般步骤
  • 如何做好需求收集?方法和步骤
  • SpringBoo整合WebSocket实战演练——Java入职十三天
  • 众佰诚:抖音小店的体验分什么时候更新
  • 详解cv2.addWeighted函数【使用 OpenCV 添加(混合)两个图像-Python版本】