添加带有样式组件的活动 class 不起作用
Adding active class with styled component not working
所以我的功能是:
function onClickChange(props) {
let MyDiv = document.getElementById('myDIV')
let InterfaceDesign = document.getElementById('interfaceDesign')
if (MyDiv.style.display === 'none') {
MyDiv.style.display = ''
InterfaceDesign.classList.add('active')
} else {
MyDiv.style.display = 'none'
InterfaceDesign.classList.remove('active')
}
}
从 DOM 中的这个函数我可以看到它正在激活 class。但是从我的样式组件:
const FirstDivElement = styled.div`
position: relative;
overflow: hidden;
width: 100%;
background-color: #000;
&:hover {
background-color: #e6007e;
transform: scale(1.05);
}
&:active{
background-color: #e6007e;
}
`
它没有获取 &:active 的样式
我的 div 元素是:
<div id="interfaceDesign">
<div onClick={onClickChange}>
<ListItems
headingText="Interface Design"
backgroundImage={props.secondBackgroundImage}
>
<Img
style={{
height: '50px',
width: '50px',
}}
sizes={props.interfacedesign}
/>
</ListItems>
</div>
</div>
<div id="myDIV" style={{ display: 'none' }}>
<InterfaceDesign
secondBackgroundImage={props.secondBackgroundImage}
interfacedesignMagenta={props.interfacedesignMagenta}
/>
</div>
</div>
有人可以帮忙吗?提前谢谢你:D
您可以通过在 FirstDivElement
中传递道具 active
并更新 onClick
方法来完成。
这里有一个例子:
const FirstDivElement = styled.div`
position: relative;
overflow: hidden;
width: 100%;
background-color: ({ active }) => active ? #e6007e :#000;
&:hover {
background-color: #e6007e;
transform: scale(1.05);
}
&:active{
background-color: #e6007e;
}
`
onClickChange = () => {
let MyDiv = document.getElementById('myDIV')
this.setState({
active: MyDiv.style.display === 'none'
})
}
render(){
const { active } = this.state
return(
<div>
<div onClick={onClickChange}>
{/* content */}
</div>
<FirstDivElement active={active}>
{/* content */}
<div id="myDIV" style={{ display: 'none' }}/>
</FirstDivElement>
<button onClick={this.onClickChange}> Click </button>
</div>
)
}
您正在为 :active pseudo-class 提供样式,它仅在元素被激活时使用(例如在鼠标单击期间)。您可能希望在您的样式组件 CSS.
中将 &:active
更改为 &.active
此外,您应该注意,您通常会使用 Styled Components 根据道具更改样式。您可以 做您正在做的事情,但这种情况不太常见。
所以我的功能是:
function onClickChange(props) {
let MyDiv = document.getElementById('myDIV')
let InterfaceDesign = document.getElementById('interfaceDesign')
if (MyDiv.style.display === 'none') {
MyDiv.style.display = ''
InterfaceDesign.classList.add('active')
} else {
MyDiv.style.display = 'none'
InterfaceDesign.classList.remove('active')
}
}
从 DOM 中的这个函数我可以看到它正在激活 class。但是从我的样式组件:
const FirstDivElement = styled.div`
position: relative;
overflow: hidden;
width: 100%;
background-color: #000;
&:hover {
background-color: #e6007e;
transform: scale(1.05);
}
&:active{
background-color: #e6007e;
}
`
它没有获取 &:active 的样式
我的 div 元素是:
<div id="interfaceDesign">
<div onClick={onClickChange}>
<ListItems
headingText="Interface Design"
backgroundImage={props.secondBackgroundImage}
>
<Img
style={{
height: '50px',
width: '50px',
}}
sizes={props.interfacedesign}
/>
</ListItems>
</div>
</div>
<div id="myDIV" style={{ display: 'none' }}>
<InterfaceDesign
secondBackgroundImage={props.secondBackgroundImage}
interfacedesignMagenta={props.interfacedesignMagenta}
/>
</div>
</div>
有人可以帮忙吗?提前谢谢你:D
您可以通过在 FirstDivElement
中传递道具 active
并更新 onClick
方法来完成。
这里有一个例子:
const FirstDivElement = styled.div`
position: relative;
overflow: hidden;
width: 100%;
background-color: ({ active }) => active ? #e6007e :#000;
&:hover {
background-color: #e6007e;
transform: scale(1.05);
}
&:active{
background-color: #e6007e;
}
`
onClickChange = () => {
let MyDiv = document.getElementById('myDIV')
this.setState({
active: MyDiv.style.display === 'none'
})
}
render(){
const { active } = this.state
return(
<div>
<div onClick={onClickChange}>
{/* content */}
</div>
<FirstDivElement active={active}>
{/* content */}
<div id="myDIV" style={{ display: 'none' }}/>
</FirstDivElement>
<button onClick={this.onClickChange}> Click </button>
</div>
)
}
您正在为 :active pseudo-class 提供样式,它仅在元素被激活时使用(例如在鼠标单击期间)。您可能希望在您的样式组件 CSS.
中将&:active
更改为 &.active
此外,您应该注意,您通常会使用 Styled Components 根据道具更改样式。您可以 做您正在做的事情,但这种情况不太常见。