| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <template>
- <view class="ruler-wrapper" v-show="visible">
- <scroll-view class="ruler-scroll" scroll-x :scroll-left="scrollLeft" :scroll-with-animation="useAnimation"
- @scroll="onScroll" @touchend="onTouchEnd" @touchcancel="onTouchEnd" scroll-into-view="">
- <view class="ruler-inner" :style="{ width: totalWidth + 'px' }">
- <view v-for="(g, idx) in gridList" :key="idx" class="grid-item" :class="{ long: g.isLongGrid }"
- :style="{ width: props.gutter + 'px' }">
- <view class="tick" :class="{ long: g.isLongGrid }"></view>
- <text v-if="g.showText" class="grid-num">{{ g.displayNum }}</text>
- </view>
- </view>
- </scroll-view>
- <view class="indicator">
- <view class="value-box"><text class="value-text">{{ currentValue }}</text></view>
- <view class="indicator-triangle"></view>
- <view class="indicator-line"></view>
- </view>
- </view>
- </template>
- <script setup lang="ts">
- import { ref, computed, watch, onMounted } from 'vue'
- import { getCurrentInstance } from 'vue'
- interface GridItem { num: number; displayNum: number | string; isLongGrid: boolean; showText: boolean }
- const props = defineProps({
- min: { type: Number, default: 0 },
- max: { type: Number, default: 100 },
- step: { type: Number, default: 1 },
- unit: { type: String, default: '' },
- initialValue: { type: Number, default: 0 },
- gutter: { type: Number, default: 10 },
- visible: { type: Boolean, default: true },
- useAnimation: { type: Boolean, default: true }
- })
- const emit = defineEmits(['update:value', 'change'])
- const instance = getCurrentInstance()
- const scrollLeft = ref(0)
- const actualScrollLeft = ref(0)
- const currentValue = ref(Math.round(props.initialValue || props.min))
- // Support decimal step: convert to integer space using scale factor
- function decimalPlaces(n: number) {
- const s = String(n)
- if (s.indexOf('e-') >= 0) {
- const m = s.match(/e-(\d+)$/)
- return m ? parseInt(m[1], 10) : 0
- }
- const idx = s.indexOf('.')
- return idx >= 0 ? s.length - idx - 1 : 0
- }
- const stepDecimals = decimalPlaces(props.step)
- const scaleFactor = Math.pow(10, stepDecimals)
- const intMin = Math.round(props.min * scaleFactor)
- const intMax = Math.round(props.max * scaleFactor)
- const intStep = Math.round(props.step * scaleFactor)
- const totalUnits = computed(() => Math.round((intMax - intMin) / intStep))
- const extraGridCount = Math.ceil((uni?.getSystemInfoSync?.().windowWidth || 375) / props.gutter / 2) * 2
- const gridList = ref<GridItem[]>([])
- const totalWidth = ref(0)
- const gridItemStyle = computed(() => ({ width: props.gutter + 'px', height: '24px' }))
- function buildGrid() {
- const count = totalUnits.value
- const arr: GridItem[] = []
- for (let i = 0; i <= count + extraGridCount * 1; i++) {
- const numIndex = i - extraGridCount / 2
- const intNum = intMin + numIndex * intStep
- const num = intNum / scaleFactor
- const displayNum = (Number.isInteger(num) ? num : Number(num.toFixed(stepDecimals)))
- // mark long grid every 10 steps in integer space (10 * intStep)
- const isLongGrid = ((intNum - intMin) % (intStep * 10) === 0)
- const showText = isLongGrid && num >= props.min && num <= props.max
- arr.push({ num, displayNum, isLongGrid, showText })
- }
- gridList.value = arr
- totalWidth.value = arr.length * props.gutter
- }
- buildGrid()
- const offsetScroll = computed(() => extraGridCount * props.gutter/2)
- const windowWidth = uni?.getSystemInfoSync?.().windowWidth || 375
- const halfWindow = windowWidth / 2
- onMounted(() => {
- // initialize using integer index space
- const initIndex = Math.round((Math.round((props.initialValue || props.min) * scaleFactor) - intMin) / intStep)
- scrollLeft.value = offsetScroll.value + initIndex * props.gutter + props.gutter / 2 - halfWindow
- const initVal = (intMin + initIndex * intStep) / scaleFactor
- currentValue.value = Number(stepDecimals ? initVal.toFixed(stepDecimals) : String(initVal))
- scrollLeft.value = offsetScroll.value + initIndex * props.gutter + props.gutter / 2 - halfWindow
- currentValue.value = Math.round(initIndex * props.step + props.min)
- })
- function onScroll(e: any) {
- const left = e?.detail?.scrollLeft ?? e?.detail?.scrollLeft ?? 0
- actualScrollLeft.value = left
- const numIndex = Math.round((left + halfWindow - offsetScroll.value - props.gutter / 2) / props.gutter)
- let intValue = intMin + numIndex * intStep
- if (intValue < intMin) intValue = intMin
- if (intValue > intMax) intValue = intMax
- const value = intValue / scaleFactor
- currentValue.value = Number(stepDecimals ? value.toFixed(stepDecimals) : String(value))
- emit('update:value', value)
- }
- function adjustScrollPosition() {
- const left = actualScrollLeft.value
- const numIndex = Math.round((left + halfWindow - offsetScroll.value - props.gutter / 2) / props.gutter)
- const targetLeft = offsetScroll.value + numIndex * props.gutter + props.gutter / 2 - halfWindow
- const maxScroll = Math.max(0, totalWidth.value - windowWidth)
- const clamped = Math.min(Math.max(targetLeft, 0), maxScroll)
- scrollLeft.value = clamped
- }
- function onTouchEnd() {
- adjustScrollPosition()
- const left = scrollLeft.value
- const numIndex = Math.round((left + halfWindow - offsetScroll.value - props.gutter / 2) / props.gutter)
- let intValue = intMin + numIndex * intStep
- if (intValue < intMin) intValue = intMin
- if (intValue > intMax) intValue = intMax
- const value = intValue / scaleFactor
- currentValue.value = Number(stepDecimals ? value.toFixed(stepDecimals) : String(value))
- emit('change', value)
- }
- </script>
- <style scoped>
- .ruler-wrapper {
- position: relative;
- height: 420rpx;
- display: flex;
- align-items: flex-start;
- }
- .ruler-scroll {
- margin-top: 210rpx;
- width: 100%;
- height: 180rpx;
- overflow-x: scroll;
- scrollbar-width: none; /* Firefox */
- -ms-overflow-style: none; /* IE and Edge */
- }
- .ruler-scroll::-webkit-scrollbar {
- display: none; /* Chrome, Safari, Opera */
- }
- .ruler-inner {
- display: flex;
- align-items: flex-start;
- }
- .grid-item {
- width: 10px;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: flex-start;
- padding-top: 8rpx;
- }
- .tick {
- width: 2px;
- margin-top: 10rpx;
- height: 60rpx;
- background: #d5d5d5;
- }
- .tick.long {
- width: 3px;
- margin-top: 0rpx;
- /* margin-left: 1px; */
- height: 80rpx;
- background: #414141;
- }
- .grid-num {
- font-size: 36rpx;
- color: #666;
- margin-block: 8rpx;
- }
- .indicator {
- position: absolute;
- left: 50%;
- top: 0;
- transform: translateX(-50%);
- width: 2px;
- height: 100%;
- display: flex;
- align-items: center;
- flex-direction: column;
- pointer-events: none;
- }
- .indicator-line {
- width: 4px;
- margin-top: -4px;
- /* margin-left: 1px; */
- height: 15px;
- background: #ff6a00;
- }
- .indicator-triangle {
- width: 0;
- height: 0;
- border-left: 8px solid transparent;
- border-right: 8px solid transparent;
- border-top: 10px solid #ff6a00;
- margin-top: 100px;
- }
- .value-box {
- margin-top: 70rpx;
- position: absolute;
- left: 50%;
- transform: translateX(-50%);
- padding: 6rpx 12rpx;
- border-radius: 6rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .value-text {
- color: #000000;
- font-size: 70rpx;
- font-weight: 900;
- line-height: 1;
- }
- </style>
|