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

华为快应用中自定义Slider效果

文章目录

  • 一、前言
  • 二、实现代码
  • 三、参考链接

一、前言

在华为快应用中官方提供了<slider>控件,但是这个控件的限制比较多,比如滑块无法自定义,所以这里进行下自定义,自己修改样式。

二、实现代码

整体效果如下:
在这里插入图片描述
源码如下:
custom_slider.ux

<template><div id="container"><div id="progress-background"></div><div id="progress-second" style="width: {{progressSecondWidth}}px"></div><div id="slider" style="left: {{left}}px" ontouchmove="handleTouchMove"></div></div>
</template><script>import prompt from '@system.prompt'export default {props: ['progress','min','max','step'],public: {progressSecondWidth: 0,left: 0,right: 0,//右侧可以滑动到的位置containerLeft: 0,containerRight: 0,stepWidth: 1, // 每一步滑动的距离,这个根据step值进行计算minDuration: 5, //滑动最小间距,小于这个间距不算滑动},onInit: function () {this.$on('onShow', this.onShow)this.$watch('progress', 'progressChange')},progressChange(newValue, oldValue) {console.log('progressChange:' + JSON.stringify(newValue))if(newValue === oldValue){return}this.sliderOtherProgress(newValue)},handleTouchMove(e) {let touchX = e.touches[0].clientX - this.containerLeft; // 获取触摸点的全局X坐标if (touchX - this.progressSecondWidth < -this.minDuration) {this.leftSlide()}if (touchX - this.progressSecondWidth > this.minDuration) {this.rightSlide()}this.emitChange()},onShow(e) {let that = thisthis.$element('container').getBoundingClientRect({success: function (data) {const { top, bottom, left, right, width, height } = data;prompt.showToast({message: `getBoundingClientRect结果: width:${width}, height:${height},top:${top}, bottom:${bottom}, left:${left}, right:${right}`})that.containerLeft = leftthat.containerRight = rightthat.right = right - left - 40 //40是滑块的宽度that.computerStepWidth()},fail: (errorData, errorCode) => {prompt.showToast({message: `错误原因:${JSON.stringify(errorData)}, 错误代码:${errorCode}`})},complete: function () {console.info('complete')}})},//向左滑动leftSlide() {let nextWidth = this.progressSecondWidth - this.stepWidthif (nextWidth <= 0) {nextWidth = 0}this.progressSecondWidth = nextWidththis.left = nextWidth;},//向右滑动rightSlide() {let nextWidth = this.progressSecondWidth + this.stepWidthif (nextWidth >= this.right) {nextWidth = this.right}this.progressSecondWidth = nextWidththis.left = nextWidth;},computerStepWidth() {let progressMax = 100;let progressMin = 0;let progressStep = 1;let currentProgress = 0;if (this.max) {progressMax = this.max}if (this.min) {progressMin = this.min}if (this.step) {progressStep = this.step}if (this.progress) {currentProgress = this.progress}this.stepWidth = this.right / (progressMax - progressMin) * progressStep //获取每一步滑动的距离let currentProgressWidth = this.stepWidth * currentProgressthis.progressSecondWidth = currentProgressWidth},sliderOtherProgress(progress){let nextWidth = progress + this.stepWidthif (nextWidth >= this.right) {nextWidth = this.right}if (nextWidth <= 0) {nextWidth = 0}this.progressSecondWidth = nextWidththis.left = nextWidth;this.emitChange()},emitChange() {let progress = Math.ceil(this.progressSecondWidth / this.stepWidth)this.$emit('change', {progress})}}
</script><style lang="less">#container {flex-grow: 1;height: 24px;display: flex;}#progress-background {flex-grow: 1;height: 4px;background-color: red;margin-top: 10px;border-radius: 4px;}#progress-second {height: 4px;background-color: pink;margin-top: 10px;border-radius: 4px;left: 0px;position: absolute;}#slider {width: 40px;height: 24px;background-color: #3b2204;position: absolute;border-radius: 12px;}
</style>

使用如下:

<import name="custom-slider" src="./custom_slider"></import>
...
<custom-slider onchange="progressChange" progress="{{customSliderProgress}}"></custom-slider>
<script>import prompt from '@system.prompt'export default {public: {customSliderProgress: 30,},progressChange(evt){console.log("--->当前进度",evt.detail.progress);},}
</script> ...

三、参考链接

  1. touch
http://www.lryc.cn/news/253185.html

相关文章:

  • C语言每日一题(43)旋转链表
  • CCF计算机软件能力认证考试—202209-1如此编码
  • Ubuntu18.04安装Ipopt-3.12.8流程
  • linux 内核同步互斥技术之信号量
  • 交通强国添力量 无人机巡航为何备受期待?
  • 【PID学习笔记 6 】控制系统的性能指标之二
  • ZLMediakit-method ANNOUNCE failed: 401 Unauthorized(ffmpeg、obs推流rtmp到ZLM发现的问题)
  • 聊聊logback的ThrowableProxyConverter
  • Kubernetes(k8s)访问不了Pod服务
  • python-学生管理|汉罗塔
  • python 堆与栈
  • 园区规划技术要点
  • 深入浅出 Linux 中的 ARM IOMMU SMMU III
  • Linux系统---图书管理中的同步问题
  • Vue学习笔记-activated和deactivated生命周期
  • 102.套接字-Socket网络编程4(TCP通信流程)
  • spring boot 2 升级到 spring boot 3 后文件上传失败
  • Java Stream API 提供了一种非常方便的方式来比较两个 List 的差异,并取出不同的对象
  • C语言还会存在多久
  • 手持式安卓主板_PDA安卓板_智能手持终端方案
  • LeetCode103. Binary Tree Zigzag Level Order Traversal
  • PHP 判断给定两个时间是否在同一周,月,年
  • 单机无锁线程安全队列-Disruptor
  • 好工具知多少:国内外最常用的SCADA软件
  • SQL Server 2016(创建数据库)
  • Vue学习计划--Vue2(一)简单了解vue
  • 微信小程序生成二维码并保存到本地方法
  • shell_exec 和 exec区别
  • WPF创建进度条
  • 全网最新最全面的Appium自动化:Appium常用操作之混合应用webview页面操作--待补充!