如何为 Slider 添加线性渐变颜色?

IT技术 css reactjs material-ui
2021-05-19 00:08:29

我想将线性渐变添加到 Material-UI Slider 作为颜色。是否可以?我尝试一切。

color: 'linear-gradient(180deg, #29ABE2 0%, #00EAA6 100%)'
1个回答

linear-gradient 创建图像而不是颜色。因此,您需要在指定图像的 CSS 中使用它(例如background-image)。

下面是一个Slider使用渐变的例子

import React from "react";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import Slider from "@material-ui/core/Slider";

const useStyles = makeStyles({
  root: {
    width: 200
  }
});

const CustomSlider = withStyles({
  rail: {
    backgroundImage: "linear-gradient(.25turn, #f00, #00f)"
  },
  track: {
    backgroundImage: "linear-gradient(.25turn, #f00, #00f)"
  }
})(Slider);

export default function ContinuousSlider() {
  const classes = useStyles();
  const [value, setValue] = React.useState(30);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  return (
    <div className={classes.root}>
      <CustomSlider
        value={value}
        onChange={handleChange}
        aria-labelledby="continuous-slider"
      />
    </div>
  );
}

编辑渐变滑块