|
@@ -22,6 +22,7 @@
|
|
|
<script setup lang="ts">
|
|
<script setup lang="ts">
|
|
|
import { ref, computed, onMounted } from 'vue'
|
|
import { ref, computed, onMounted } from 'vue'
|
|
|
import { getCurrentInstance } from 'vue'
|
|
import { getCurrentInstance } from 'vue'
|
|
|
|
|
+import { getWindowWidth } from '@/utils/platform'
|
|
|
|
|
|
|
|
interface GridItem { num: number; displayNum: number | string; isLongGrid: boolean; showText: boolean }
|
|
interface GridItem { num: number; displayNum: number | string; isLongGrid: boolean; showText: boolean }
|
|
|
|
|
|
|
@@ -65,8 +66,7 @@ const intStep = Math.round(props.step * scaleFactor)
|
|
|
const totalUnits = computed(() => Math.round((intMax - intMin) / intStep))
|
|
const totalUnits = computed(() => Math.round((intMax - intMin) / intStep))
|
|
|
|
|
|
|
|
|
|
|
|
|
-const extraGridCount = Math.ceil((uni?.getSystemInfoSync?.().windowWidth || 375) / props.gutter / 2) * 2
|
|
|
|
|
-
|
|
|
|
|
|
|
+const extraGridCount = ref(0)
|
|
|
const gridList = ref<GridItem[]>([])
|
|
const gridList = ref<GridItem[]>([])
|
|
|
const totalWidth = ref(0)
|
|
const totalWidth = ref(0)
|
|
|
|
|
|
|
@@ -75,8 +75,9 @@ const gridItemStyle = computed(() => ({ width: props.gutter + 'px', height: '24p
|
|
|
function buildGrid() {
|
|
function buildGrid() {
|
|
|
const count = totalUnits.value
|
|
const count = totalUnits.value
|
|
|
const arr: GridItem[] = []
|
|
const arr: GridItem[] = []
|
|
|
- for (let i = 0; i <= count + extraGridCount * 1; i++) {
|
|
|
|
|
- const numIndex = i - extraGridCount / 2
|
|
|
|
|
|
|
+ const extra = extraGridCount.value || 0
|
|
|
|
|
+ for (let i = 0; i <= count + extra * 1; i++) {
|
|
|
|
|
+ const numIndex = i - extra / 2
|
|
|
const intNum = intMin + numIndex * intStep
|
|
const intNum = intMin + numIndex * intStep
|
|
|
const num = intNum / scaleFactor
|
|
const num = intNum / scaleFactor
|
|
|
const displayNum = (Number.isInteger(num) ? num : Number(num.toFixed(stepDecimals)))
|
|
const displayNum = (Number.isInteger(num) ? num : Number(num.toFixed(stepDecimals)))
|
|
@@ -89,23 +90,27 @@ function buildGrid() {
|
|
|
totalWidth.value = arr.length * props.gutter
|
|
totalWidth.value = arr.length * props.gutter
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-buildGrid()
|
|
|
|
|
-
|
|
|
|
|
-const offsetScroll = computed(() => extraGridCount * props.gutter/2)
|
|
|
|
|
|
|
+const offsetScroll = computed(() => (extraGridCount.value || 0) * props.gutter / 2)
|
|
|
|
|
|
|
|
-const windowWidth = uni?.getSystemInfoSync?.().windowWidth || 375
|
|
|
|
|
-const halfWindow = windowWidth / 2
|
|
|
|
|
|
|
+const windowWidthRef = ref(375)
|
|
|
|
|
+const halfWindow = computed(() => windowWidthRef.value / 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
-onMounted(() => {
|
|
|
|
|
|
|
+onMounted(async () => {
|
|
|
|
|
+ // 获取宽度并初始化相关计算
|
|
|
|
|
+ const w = await getWindowWidth().catch(() => 375)
|
|
|
|
|
+ windowWidthRef.value = w
|
|
|
|
|
+ extraGridCount.value = Math.ceil((w || 375) / props.gutter / 2) * 2
|
|
|
|
|
+ // 重建 grid
|
|
|
|
|
+ buildGrid()
|
|
|
|
|
|
|
|
// initialize using integer index space
|
|
// initialize using integer index space
|
|
|
const initIndex = Math.round((Math.round((props.initialValue || props.min) * scaleFactor) - intMin) / intStep)
|
|
const initIndex = Math.round((Math.round((props.initialValue || props.min) * scaleFactor) - intMin) / intStep)
|
|
|
- scrollLeft.value = offsetScroll.value + initIndex * props.gutter + props.gutter / 2 - halfWindow
|
|
|
|
|
|
|
+ scrollLeft.value = offsetScroll.value + initIndex * props.gutter + props.gutter / 2 - halfWindow.value
|
|
|
const initVal = (intMin + initIndex * intStep) / scaleFactor
|
|
const initVal = (intMin + initIndex * intStep) / scaleFactor
|
|
|
currentValue.value = Number(stepDecimals ? initVal.toFixed(stepDecimals) : String(initVal))
|
|
currentValue.value = Number(stepDecimals ? initVal.toFixed(stepDecimals) : String(initVal))
|
|
|
|
|
|
|
|
- scrollLeft.value = offsetScroll.value + initIndex * props.gutter + props.gutter / 2 - halfWindow
|
|
|
|
|
|
|
+ scrollLeft.value = offsetScroll.value + initIndex * props.gutter + props.gutter / 2 - halfWindow.value
|
|
|
|
|
|
|
|
currentValue.value = Math.round(initIndex * props.step + props.min)
|
|
currentValue.value = Math.round(initIndex * props.step + props.min)
|
|
|
})
|
|
})
|
|
@@ -114,7 +119,7 @@ function onScroll(e: any) {
|
|
|
const left = e?.detail?.scrollLeft ?? e?.detail?.scrollLeft ?? 0
|
|
const left = e?.detail?.scrollLeft ?? e?.detail?.scrollLeft ?? 0
|
|
|
actualScrollLeft.value = left
|
|
actualScrollLeft.value = left
|
|
|
|
|
|
|
|
- const numIndex = Math.round((left + halfWindow - offsetScroll.value - props.gutter / 2) / props.gutter)
|
|
|
|
|
|
|
+ const numIndex = Math.round((left + halfWindow.value - offsetScroll.value - props.gutter / 2) / props.gutter)
|
|
|
let intValue = intMin + numIndex * intStep
|
|
let intValue = intMin + numIndex * intStep
|
|
|
if (intValue < intMin) intValue = intMin
|
|
if (intValue < intMin) intValue = intMin
|
|
|
if (intValue > intMax) intValue = intMax
|
|
if (intValue > intMax) intValue = intMax
|
|
@@ -127,11 +132,11 @@ function adjustScrollPosition() {
|
|
|
|
|
|
|
|
const left = actualScrollLeft.value
|
|
const left = actualScrollLeft.value
|
|
|
|
|
|
|
|
- const numIndex = Math.round((left + halfWindow - offsetScroll.value - props.gutter / 2) / props.gutter)
|
|
|
|
|
|
|
+ const numIndex = Math.round((left + halfWindow.value - offsetScroll.value - props.gutter / 2) / props.gutter)
|
|
|
|
|
|
|
|
- const targetLeft = offsetScroll.value + numIndex * props.gutter + props.gutter / 2 - halfWindow
|
|
|
|
|
|
|
+ const targetLeft = offsetScroll.value + numIndex * props.gutter + props.gutter / 2 - halfWindow.value
|
|
|
|
|
|
|
|
- const maxScroll = Math.max(0, totalWidth.value - windowWidth)
|
|
|
|
|
|
|
+ const maxScroll = Math.max(0, totalWidth.value - windowWidthRef.value)
|
|
|
const clamped = Math.min(Math.max(targetLeft, 0), maxScroll)
|
|
const clamped = Math.min(Math.max(targetLeft, 0), maxScroll)
|
|
|
scrollLeft.value = clamped
|
|
scrollLeft.value = clamped
|
|
|
}
|
|
}
|
|
@@ -141,7 +146,7 @@ function onTouchEnd() {
|
|
|
adjustScrollPosition()
|
|
adjustScrollPosition()
|
|
|
|
|
|
|
|
const left = scrollLeft.value
|
|
const left = scrollLeft.value
|
|
|
- const numIndex = Math.round((left + halfWindow - offsetScroll.value - props.gutter / 2) / props.gutter)
|
|
|
|
|
|
|
+ const numIndex = Math.round((left + halfWindow.value - offsetScroll.value - props.gutter / 2) / props.gutter)
|
|
|
let intValue = intMin + numIndex * intStep
|
|
let intValue = intMin + numIndex * intStep
|
|
|
if (intValue < intMin) intValue = intMin
|
|
if (intValue < intMin) intValue = intMin
|
|
|
if (intValue > intMax) intValue = intMax
|
|
if (intValue > intMax) intValue = intMax
|