使用伪元素的样式化组件 :checked
Styled components using pseudo element :checked
我正在制作带样式组件的切换开关组件,切换时我想更改背景颜色。我在这里尝试 select 并更改它 ${SwitchInputUI}:checked + ${SwitchSliderUI}{ background-color:#737A9B;}
但它不起作用。我 select 做对了吗?这里满了code.Please帮帮我
import React from 'react';
import styled from 'styled-components'
const SwitchInputUI = styled.input.attrs({ type: 'checkbox' })`
opacity: 0;
`
const SwitchUI =styled.label`
position: relative;
display: inline-block;
width: 22px;
height: 12px;
margin-bottom: 0;
vertical-align: middle;
${SwitchInputUI}:checked + ${SwitchSliderUI}{
background-color: #737A9B;
}
${SwitchInputUI}:checked + ${SwitchSliderUI}:before{
transform: translateX(10px);
}
`
const SwitchSliderUI= styled.span`
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color:#BBCDD9;
transition: .4s;
border-radius: 10px;
&:before {
content: "";
position: absolute;
width: 10px;
height: 10px;
left: 1px;
bottom: 1px;
background-color: #FFF;
transition: .4s;
border-radius: 50%;
}
`
const Switch = () => {
return (
<SwitchUI>
<SwitchInputUI/>
<SwitchSliderUI/>
</SwitchUI>
);
};
export default Switch;
试试是否有帮助:
${SwitchInputUI} + ${SwitchSliderUI}{
'&:checked':{background-color: #737A9B; }
}
问题是您在 SwitchUI
中使用 SwitchSliderUI
之前定义它。更改顺序即可。
import React from 'react';
import styled from 'styled-components'
const SwitchInputUI = styled.input.attrs({ type: 'checkbox' })`
opacity: 0;
`
const SwitchSliderUI= styled.span`
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color:#BBCDD9;
transition: .4s;
border-radius: 10px;
&:before {
content: "";
position: absolute;
width: 10px;
height: 10px;
left: 1px;
bottom: 1px;
background-color: #FFF;
transition: .4s;
border-radius: 50%;
}
`
const SwitchUI =styled.label`
position: relative;
display: inline-block;
width: 22px;
height: 12px;
margin-bottom: 0;
vertical-align: middle;
${SwitchInputUI}:checked + ${SwitchSliderUI}{
background-color: #737A9B;
}
${SwitchInputUI}:checked + ${SwitchSliderUI}:before{
transform: translateX(10px);
}
`
const Switch = () => {
return (
<SwitchUI>
<SwitchInputUI/>
<SwitchSliderUI/>
</SwitchUI>
);
};
export default Switch;
我正在制作带样式组件的切换开关组件,切换时我想更改背景颜色。我在这里尝试 select 并更改它 ${SwitchInputUI}:checked + ${SwitchSliderUI}{ background-color:#737A9B;}
但它不起作用。我 select 做对了吗?这里满了code.Please帮帮我
import React from 'react';
import styled from 'styled-components'
const SwitchInputUI = styled.input.attrs({ type: 'checkbox' })`
opacity: 0;
`
const SwitchUI =styled.label`
position: relative;
display: inline-block;
width: 22px;
height: 12px;
margin-bottom: 0;
vertical-align: middle;
${SwitchInputUI}:checked + ${SwitchSliderUI}{
background-color: #737A9B;
}
${SwitchInputUI}:checked + ${SwitchSliderUI}:before{
transform: translateX(10px);
}
`
const SwitchSliderUI= styled.span`
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color:#BBCDD9;
transition: .4s;
border-radius: 10px;
&:before {
content: "";
position: absolute;
width: 10px;
height: 10px;
left: 1px;
bottom: 1px;
background-color: #FFF;
transition: .4s;
border-radius: 50%;
}
`
const Switch = () => {
return (
<SwitchUI>
<SwitchInputUI/>
<SwitchSliderUI/>
</SwitchUI>
);
};
export default Switch;
试试是否有帮助:
${SwitchInputUI} + ${SwitchSliderUI}{
'&:checked':{background-color: #737A9B; }
}
问题是您在 SwitchUI
中使用 SwitchSliderUI
之前定义它。更改顺序即可。
import React from 'react';
import styled from 'styled-components'
const SwitchInputUI = styled.input.attrs({ type: 'checkbox' })`
opacity: 0;
`
const SwitchSliderUI= styled.span`
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color:#BBCDD9;
transition: .4s;
border-radius: 10px;
&:before {
content: "";
position: absolute;
width: 10px;
height: 10px;
left: 1px;
bottom: 1px;
background-color: #FFF;
transition: .4s;
border-radius: 50%;
}
`
const SwitchUI =styled.label`
position: relative;
display: inline-block;
width: 22px;
height: 12px;
margin-bottom: 0;
vertical-align: middle;
${SwitchInputUI}:checked + ${SwitchSliderUI}{
background-color: #737A9B;
}
${SwitchInputUI}:checked + ${SwitchSliderUI}:before{
transform: translateX(10px);
}
`
const Switch = () => {
return (
<SwitchUI>
<SwitchInputUI/>
<SwitchSliderUI/>
</SwitchUI>
);
};
export default Switch;