【发布时间】:2019-04-12 03:19:19
【问题描述】:
这是我的实际输入范围:
这就是我想要的:
RangeComponent.tsx:
const active = '#64c3ef'
const inactive = '#dbdbdb'
export class RangeComponent extends React.Component<Props> {
inputRef: any = React.createRef()
state = { value: 10 }
handleChange = (min: number, max: number) => (event: any) => {
const value = event.target.value
const progress = (value / max) * 100 + '%'
this.setState({ value: value })
const newBackgroundStyle = `linear-gradient(90deg, ${active} 0% ${progress}%, ${inactive} ${progress}% 100%)`
this.inputRef.current.style.background = newBackgroundStyle
}
render() {
const minValue = 10
const maxValue = 300
const progress = (this.state.value / maxValue) * 100 + '%'
const styleInput = {
background: `linear-gradient(90deg, ${active} 0% ${progress}%, ${inactive} ${progress}% 100%)`,
}
return (
<div>
<input
ref={this.inputRef}
id="sliderId"
className="inputR"
name="sliderName"
type="range"
min={minValue}
max={maxValue}
value={this.state.value}
onChange={this.handleChange(minValue, maxValue)}
style={styleInput}
/>
<div className="label">
{this.state.value}
</div>
</div>
)
}
}
style.css:
.inputR::-webkit-slider-thumb {
background: rgba(255, 208, 255, 0.75);
}
如果可能,我想不在 style.css 中而是在 RangeComponent.tsx 中控制拇指颜色。
我不明白为什么 inputR 类对输入样式没有影响。
如何使用 javascript 对象 (styleInput) 中的 CSS 更改样式?
【问题讨论】:
-
能不能上传到github或者codepen,我们看看?还请包括“滑块”类的 css。
-
@Francois 当然!我添加了一个 Codepen 链接:codepen.io/mistochi/pen/YMQZKV
-
谢谢,我去看看。我目前正在使用移动设备。
标签: javascript reactjs typescript input