如何禁用Material UI Slider点击轨道更改数值的功能
Material-UI 多滑块Slider禁用轨道点击调整方案
当Slider组件存在多个滑块(thumb)时,无法确定用户点击轨道时想要移动哪个滑块,因此需要禁用点击轨道调整Slider数值的能力。
解决方案:多滑块场景下仅允许通过拖动滑块的方式控制数值,实现代码如下:
import React from 'react'; import PropTypes from 'prop-types'; import { withStyles, makeStyles } from '@material-ui/core/styles'; import Slider from '@material-ui/core/Slider'; import Typography from '@material-ui/core/Typography'; import Tooltip from '@material-ui/core/Tooltip'; const useStyles = makeStyles((theme) => ({ root: { width: 300 + theme.spacing(3) * 2, }, margin: { height: theme.spacing(3), }, })); const AirbnbSlider = withStyles({ root: { color: '#3a8589', height: 3, padding: '13px 0', }, thumb: { height: 27, width: 27, backgroundColor: '#fff', border: '1px solid currentColor', marginTop: -12, marginLeft: -13, boxShadow: '#ebebeb 0 2px 2px', '&:focus, &:hover, &$active': { boxShadow: '#ccc 0 2px 3px 1px', }, '& .bar': { // display: inline-block !important; height: 9, width: 1, backgroundColor: 'currentColor', marginLeft: 1, marginRight: 1, }, }, active: {}, track: { height: 3, }, rail: { color: '#d8d8d8', opacity: 1, height: 3, }, })(Slider); function AirbnbThumbComponent(props) { return ( <span {...props}> <span className="bar" /> <span className="bar" /> <span className="bar" /> </span> ); }
示例效果

内容的提问来源于stack exchange,提问作者Bear Bile Farming is Torture
相关产品推荐
相关产品推荐

