简体   繁体   English

添加带有样式化组件的活动类不起作用

[英]Adding active class with styled component not working

So My function is: 所以我的功能是:

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')
  }
}

From this function in DOM I can see that it's getting the active class. 从DOM中的此函数,我可以看到它正在获得活动类。 But from my styled component: 但是从我的样式组件中:

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;
  }
`

It is not getting the style of &:active 它没有得到&:active的样式

My div element is: 我的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>

Can anyone please help? 谁能帮忙吗? Thank you in advance :D 预先谢谢你:D

You can do by passing a prop active in your FirstDivElement and update your onClick method. 您可以通过在FirstDivElement传递activeFirstDivElement并更新onClick方法来完成。

here an example : 这是一个例子:

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> 
    )
} 

You're providing styles for the :active pseudo-class , which is only used for when an element is being activated (eg during a mouse click). 您正在为:active伪类提供样式,该样式仅用于元素被激活时(例如,在单击鼠标时)。 You probably want to change from &:active to &.active in your styled component CSS. 您可能希望在样式化组件CSS &.active &:active更改为&.active

Also, you should note that you usually would alter styles based on props with Styled Components. 另外,您还应该注意,通常会使用带有样式化组件的道具来更改样式。 You can do what you're doing, but it's less common. 可以做自己在做的事情,但这并不常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM