我制作了一个滑动条,左边应该是蓝色的,右边是灰色的。而且蓝色部分应该比灰色部分更宽。
我有两个问题需要解决:
- 如果拇指块有一个透明的切口,那么滑块的蓝色部分不会完全延伸到图像可见区域的左侧。
- 我希望滑块的边缘是圆形的,特别是蓝色边缘(可能不需要灰色)。
以下是代码本身
import React from "react";
import styled from "styled-components";
const image = "https://i.imgur.com/XMfYtkv.png";
const thumb = {
width: 25,
height: 26
};
const height = 36;
const trackHeight = 4;
// colours
const lowerBackground = `linear-gradient(to bottom, gray, gray) 100% 50% / 100% ${
trackHeight / 2.5
}px no-repeat transparent`;
const upperBackground = `linear-gradient(to bottom, blue, blue) 100% 50% / 100% ${
trackHeight / 2.5
}px no-repeat transparent`;
const makeLongShadow = (color, size) => {
// Colors the slider to the right of the thumbpiece
let i = 1;
let shadow = `-${i}px 0 0 ${size} ${color}`;
for (; i < 1950; i++) {
//If i is a small number, like 720, then when the slider gets set to its minimum, the end of the slider is the color for the left side of the slider, when it should be the color for the right side
shadow = `${shadow}, -${i}px 0 0 ${size} ${color}`;
}
return shadow;
};
const StyledSlider = styled.input`
overflow: hidden;
appearance: none;
height: ${height}px;
cursor: pointer;
marginbottom: 0;
&:focus {
outline: none;
}
&::-webkit-slider-runnable-track {
width: 100%;
height: ${height}px;
background: ${lowerBackground};
}
&::-webkit-slider-thumb {
position: relative;
appearance: none;
height: ${thumb.height}px;
width: ${thumb.width}px;
background: url(${image});
background-size: cover;
border: 0;
top: 50%;
transform: translateY(-50%);
box-shadow: ${makeLongShadow("blue", "-11px")};
transition: background-color 150ms;
}
&::-moz-range-progress {
background: ${lowerBackground};
}
&::-moz-range-track,
&::-moz-range-progress {
width: 100%;
height: ${height};
background: ${upperBackground};
}
&::-moz-range-thumb {
appearance: none;
margin: 0;
height: ${thumb.height}px;
width: ${thumb.height}px;
background: url(${image});
border: 0;
transition: background-color 150ms;
}
&::-ms-track {
width: 100%;
height: ${height}px;
border: 0;
/* color needed to hide track marks */
color: transparent;
background: transparent;
}
&::-ms-fill-lower {
background: ${lowerBackground};
}
&::-ms-fill-upper {
background: ${upperBackground};
}
&::-ms-thumb {
appearance: none;
height: ${thumb.height}px;
width: ${thumb.height}px;
background: url(${image});
border-radius: 100%;
border: 0;
transition: background-color 150ms;
/* IE Edge thinks it can support -webkit prefixes */
top: 0;
margin: 0;
box-shadow: none;
}
`;
//!Black line is too thick when then margin at the bottom is 11 or 10 ? I don't know what would cause this
export default (props) => <StyledSlider type="range" {...props} />;