【问题标题】:Adding active class with styled component not working添加带有样式组件的活动类不起作用
【发布时间】:2019-03-27 20:22:04
【问题描述】:

所以我的功能是:

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 中的这个函数我可以看到它正在获取活动类。但是从我的样式组件中:

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

【问题讨论】:

  • 与您的问题无关,但您应该注意,使用 DOM 作为您的状态的源是违反 React 的工作方式(状态从父级流向子级,并且 DOM 由 React 处理)。

标签: javascript css reactjs styled-components gatsby


【解决方案1】:

您正在为:active pseudo-class 提供样式,该样式仅用于激活元素时(例如,在鼠标单击期间)。您可能希望在样式化组件 CSS 中从 &amp;:active 更改为 &amp;.active

另外,您应该注意,您通常会根据带有样式组件的道具更改样式。你可以做你正在做的事,但这种情况不太常见。

【讨论】:

    【解决方案2】:

    您可以通过在 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> 
        )
    } 
    

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多