import { ScrollView, View, Text, Image } from "@tarojs/components"; import './SlidngScale.scss' import { useEffect, useRef, useState } from "react"; import Taro from "@tarojs/taro"; export default function Component(props: { step: number, min: number, max: number, default_value: number, changed: Function, unit: string themeColor: string }) { const scrollViewRef = useRef(); const minNum: number = props.min; const maxNum: number = props.max; const stepNum: number = props.step * 10; const jingdu: number = props.step < 1 ? 10 : 1; const list: any[] = []; const slidngWidth = 10 const [current, setCurrent] = useState(props.default_value) const [left, setLeft] = useState((props.default_value - props.min) * slidngWidth / props.step + 1); for (var i: number = props.min; i <= props.max; i += props.step) { var value: number = parseFloat(i.toFixed(1)); var isBig: boolean = Math.round((value - minNum) * jingdu) % Math.round(stepNum * jingdu) == 0; var isMiddle: boolean = (Math.round((value - minNum) * jingdu) * 2) % Math.round(stepNum * jingdu) == 0; list.push({ value: props.step < 1 ? value : Math.round(value), showBig: isBig && i >= minNum && i <= maxNum, showMiddle: isMiddle && !isBig && i >= minNum && i <= maxNum }) } useEffect(() => { // var aa = current-props.min; // var bb = aa/props.step*slidngWidth; // setLeft(bb/ratio) }, []) // const dpr = Taro.pxTransform//Taro.getSystemInfoSync().pixelRatio; const handleScroll = (e) => { const { scrollLeft } = e.detail; var count = scrollLeft / slidngWidth; var strValue = (props.min + Math.round(count) * props.step).toFixed(1); if ((strValue as any) < props.min) { strValue = props.min as any; } if ((strValue as any) > props.max) { strValue = props.max as any; } if (parseFloat(strValue) != current) { var data = strValue as any; if (props.step < 1) { data = parseFloat(strValue).toFixed(1); } else { data = Math.round(data); } setCurrent(data); props.changed(data); } }; return {current} {props.unit} { list.map((item, index) => { return { item.showBig ? {item.value} : null } }) } {/* */} {/* */} }