【发布时间】:2021-04-16 13:43:42
【问题描述】:
我正在构建一个价格范围滑块,其中包含从最低价格到最高价格的 2 个输入和一条 SVG 线,该线将在滑块拇指内向左/向右动画。
我正在努力寻找该功能的数学方程式,因为如果有意义的话,左 SVG x1 值需要为 0 而不是最小输入值。
我留下了一个codeSandBox,所以你们可以看看我在这里想要实现的确切目标。
https://codesandbox.io/s/practical-bush-qq7dl?file=/src/App.js
import React, { useState } from "react";
export default function App() {
const [minRange, setMinRange] = useState("");
const [maxRange, setMaxRange] = useState("");
const [staticRange, setStaticRange] = useState({
min: 20,
max: 112
});
const rangeSplit = staticRange.min + staticRange.max / 2;
const handleChange = (input, e) => {
const value = e.target.value;
input === "min" ? setMinRange(value) : setMaxRange(value);
};
return (
<div className="slider">
<div className="slider__inputs">
<input
min={staticRange.min}
max={rangeSplit}
type="range"
value={minRange}
onChange={(e) => handleChange("min", e)}
/>
<input
min={rangeSplit}
max={staticRange.max}
type="range"
value={maxRange}
onChange={(e) => handleChange("max", e)}
/>
<svg width="100%" height="4">
<line
x1={minRange}
y1="0"
x2={maxRange}
y2="0"
stroke="#444"
stroke-width="12"
></line>
</svg>
</div>
<div className="slider__range">
<span>{`£${minRange}`}</span>
<span>{`£${maxRange}`}</span>
</div>
</div>
);
}
想要的效果:
【问题讨论】: