uniapp 自界说的滑杆(支持一个屏幕2个滑杆同时滑动) ...

打印 上一主题 下一主题

主题 1605|帖子 1605|积分 4815

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <template>
  2.         <view class="slider-container" :style="containerStyle" @touchmove.prevent>
  3.                 <view ref="sliderHandle" class="slider-handle" :style="handleStyle" @touchstart="onTouchStart"
  4.                         @touchmove.stop="onTouchMove" @touchend="onTouchEnd"></view>
  5.         </view>
  6. </template>
  7. <script>
  8.         export default {
  9.                 name: 'SliderControl',
  10.                 props: {
  11.                         mode: {
  12.                                 type: String,
  13.                                 default: 'horizontal'
  14.                         },
  15.                         width: {
  16.                                 type: [Number, String],
  17.                                 default: 220
  18.                         },
  19.                         height: {
  20.                                 type: [Number, String],
  21.                                 default: 60
  22.                         }
  23.                 },
  24.                 data() {
  25.                         return {
  26.                                 position: 70,
  27.                                 isDragging: false,
  28.                                 startTouch: null,
  29.                                 startPosition: null,
  30.                                 lastUpdate: 0,
  31.                                 lastEmittedValue: 0,
  32.                                 currentDelta: 0,
  33.                                 animationTimer: null
  34.                         }
  35.                 },
  36.                 methods: {
  37.                         onTouchStart(e) {
  38.                                 uni.vibrateShort({
  39.                                         success: () => {
  40.                                                 console.log('短振动成功');
  41.                                         },
  42.                                         fail: (err) => {
  43.                                                 console.error('短振动失败:', err);
  44.                                         }
  45.                                 });
  46.                                 e.stopPropagation()
  47.                                 if (this.animationTimer) {
  48.                                         clearTimeout(this.animationTimer)
  49.                                         this.animationTimer = null
  50.                                 }
  51.                                 const touch = e.changedTouches[0]
  52.                                 if (!touch) return
  53.                                 this.isDragging = true
  54.                                 this.startTouch = {
  55.                                         x: touch.pageX,
  56.                                         y: touch.pageY
  57.                                 }
  58.                                 this.startPosition = this.position
  59.                                 this.lastUpdate = Date.now()
  60.                                 this.currentDelta = 0
  61.                         },
  62.                         onTouchMove(e) {
  63.                                 if (!this.isDragging || !this.startTouch) return
  64.                                 e.stopPropagation()
  65.                                 const now = Date.now()
  66.                                 const timeDiff = now - this.lastUpdate
  67.                                 if (timeDiff < 16) return
  68.                                 this.lastUpdate = now
  69.                                 const touch = e.changedTouches[0]
  70.                                 if (!touch) return
  71.                                 const delta = this.mode === 'horizontal' ?
  72.                                         touch.pageX - this.startTouch.x :
  73.                                         touch.pageY - this.startTouch.y
  74.                                 this.currentDelta = this.currentDelta + (delta - this.currentDelta) * 0.9
  75.                                 let targetPos = this.startPosition + this.currentDelta * 1
  76.                                 targetPos = Math.max(0, Math.min(targetPos, 180))
  77.                                 if (Math.abs(targetPos - this.position) > 0.05) {
  78.                                         const smoothFactor = Math.min(0.08, timeDiff / 250)
  79.                                         this.position = this.position + (targetPos - this.position) * smoothFactor
  80.                                 }
  81.                                 const value = this.mode === 'horizontal' ?
  82.                                         (this.position - 70) / 70 :
  83.                                         (70 - this.position) / 70
  84.                                 const roundedValue = Math.round(value * 100) / 100
  85.                                 if (Math.abs(roundedValue - this.lastEmittedValue) >= 0.01) {
  86.                                         this.lastEmittedValue = roundedValue
  87.                                         this.$emit('change', {
  88.                                                 value: Math.max(-1, Math.min(1, roundedValue))
  89.                                         })
  90.                                 }
  91.                         },
  92.                         onTouchEnd(e) {
  93.                                 if (!this.isDragging) return
  94.                                 e.stopPropagation()
  95.                                 this.isDragging = false
  96.                                 this.startTouch = null
  97.                                 this.startPosition = null
  98.                                 this.currentDelta = 0
  99.                                 const startPos = this.position
  100.                                 const startTime = Date.now()
  101.                                 const duration = 500
  102.                                 const easeOutQuart = t => 1 - Math.pow(1 - t, 4)
  103.                                 const animate = () => {
  104.                                         const elapsed = Date.now() - startTime
  105.                                         if (elapsed >= duration) {
  106.                                                 this.position = 70
  107.                                                 this.$emit('change', {
  108.                                                         value: 0
  109.                                                 })
  110.                                                 this.animationTimer = null
  111.                                                 return
  112.                                         }
  113.                                         const progress = elapsed / duration
  114.                                         const easeProgress = easeOutQuart(progress)
  115.                                         this.position = startPos + (70 - startPos) * easeProgress
  116.                                         this.animationTimer = setTimeout(animate, 16)
  117.                                 }
  118.                                 animate()
  119.                         }
  120.                 },
  121.                 beforeDestroy() {
  122.                         if (this.animationTimer) {
  123.                                 clearTimeout(this.animationTimer)
  124.                         }
  125.                 },
  126.                 computed: {
  127.                         containerStyle() {
  128.                                 return {
  129.                                         width: typeof this.width === 'number' ? `${this.width}px` : this.width,
  130.                                         height: typeof this.height === 'number' ? `${this.height}px` : this.height,
  131.                                         backgroundColor: '#000000',
  132.                                         borderRadius: '30px',
  133.                                         position: 'relative'
  134.                                 }
  135.                         },
  136.                         trackStyle() {
  137.                                 if (this.mode === 'horizontal') {
  138.                                         return {
  139.                                                 position: 'absolute',
  140.                                                 left: '20px',
  141.                                                 right: '20px',
  142.                                                 height: '2px',
  143.                                                 top: '30%',
  144.                                                 backgroundColor: 'rgba(255, 255, 255, 0.3)',
  145.                                                 transform: 'translateY(-1px)'
  146.                                         }
  147.                                 } else {
  148.                                         return {
  149.                                                 position: 'absolute',
  150.                                                 top: '20px',
  151.                                                 bottom: '20px',
  152.                                                 width: '2px',
  153.                                                 left: '40%',
  154.                                                 backgroundColor: 'rgba(255, 255, 255, 0.3)',
  155.                                                 transform: 'translateX(-1px)'
  156.                                         }
  157.                                 }
  158.                         },
  159.                         handleStyle() {
  160.                                 const baseStyle = {
  161.                                         position: 'absolute',
  162.                                         width: '50px',
  163.                                         height: '50px',
  164.                                         backgroundColor: '#FFFFFF',
  165.                                         borderRadius: '30px'
  166.                                 }
  167.                                 if (this.mode === 'horizontal') {
  168.                                         return {
  169.                                                 ...baseStyle,
  170.                                                 transform: `translateX(${this.position}px)`,
  171.                                                 top: '0px'
  172.                                         }
  173.                                 } else {
  174.                                         return {
  175.                                                 ...baseStyle,
  176.                                                 transform: `translateY(${this.position}px)`,
  177.                                                 left: '0px'
  178.                                         }
  179.                                 }
  180.                         }
  181.                 }
  182.         }
  183. </script>
  184. <style>
  185.         .slider-container {
  186.                 flex: 1;
  187.                 position: relative;
  188.                 background-color: #000000;
  189.                 border-radius: 30px;
  190.         }
  191.         .slider-track {
  192.                 position: absolute;
  193.                 background-color: rgba(255, 255, 255, 0.3);
  194.         }
  195.         .slider-handle {
  196.                 position: absolute;
  197.                 background-color: #FFFFFF;
  198.         }
  199. </style>
复制代码
为这个代码创建一个文件SlidePlugin.vue
然后再其他页面进行调用:
  1. <template>
  2. <view>
  3.     <SlidePlugin
  4.                                   mode="vertical"
  5.                                   :width="50"
  6.                                   :height="180"
  7.                                   @change="sliderChange_ver"
  8.                                 />
  9. </view>
  10. </template>
  11. <script>
  12. import SlidePlugin from '@/components/SlidePlugin.vue'
  13. export default {
  14. components: {
  15.                        
  16.                         SlidePlugin
  17.                 }
  18. }
  19. </script>
复制代码
官方提供的无法在一个页面同时滑动2个,所以自己做了一个,这个滑动后会自动返回中心位置,假如中心位置偏离,自己调整下

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

反转基因福娃

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表