【问题标题】:Fixed positions acts like relative, in React在 React 中,固定位置的行为类似于相对
【发布时间】:2022-01-02 07:42:12
【问题描述】:

我想将一些动画弹出窗口插入到我的 React Web 应用程序中。 它应该是固定位置,这意味着它将直接插入到页面中,而不考虑弹出窗口在模板中的实际位置。

但是代码的结果是popup是相对插入的,并且依赖于它的parents。

这是图像,您可以看到它位于它所在的子组件内。

我做错了什么?

我的弹出窗口:

export function animatedBoundInDownHoc(WrappedComponent) {

return function () {
    const [amm, setAmm] = useState(true);

    return (
        <div>
            <div className="page-area" onClick={()=> {
                setAmm(!amm);
            }}>
                <div className={  amm? "animate__animated  animate__bounceInDown animated-hoc1" : "animate__animated animate__bounceOutUp animated-hoc1" }>
                    <WrappedComponent
                    />
                </div>

            </div>
        </div>
    );
}}

它显示的视图:

  <div>
            {/*<Try1Use/>*/}
            <h1 tabIndex={1} onBlur={() => {
                alert("blur pop up")
            }}

            >Blur?? </h1>
            <p>SHow animated? {this.state.globalConfiguration.showAnimatedPopup + ""}</p>

            <GlobalConfigurationContext.Provider value={this.state.globalConfiguration}>
                <GlobalConfigurationContext.Consumer>
                    {({showAnimatedPopup})=>{
                        if(showAnimatedPopup){
                            return(<Try1Use/>)
                        }
                    }}

                </GlobalConfigurationContext.Consumer>

CSS:

.page-area{
  position: fixed;
  z-index: 5;
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;/*define the horizontally centering*/
  align-items: center;/*define the vertically centering*/
  background: rgba(0,0,0, 0.5);


}

.animated-hoc1{
  width: auto;
}

用户界面结果:

检查结果:

【问题讨论】:

  • rgba(0,0,0,.5) 似乎覆盖了整个视口而不是居中。你的意思是黄金模态?您似乎没有为此显示 css。
  • @admcfajn 不是,我的意思是整个弹出窗口。黄金模态没有居中(它只有填充)。

标签: css reactjs sass webui


【解决方案1】:

只有当你给它 x,y 坐标时,固定才会考虑,组合 top,left,right,bottom

尝试以下方法:

.page-area{
  position: fixed;
  z-index: 5;
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;/*define the horizontally centering*/
  align-items: center;/*define the vertically centering*/
  background: rgba(0,0,0, 0.5);

  //positioning:
  top: 0;
  left: 0;
}

【讨论】:

  • 酷,tnx,它解决了。你能扩展/给出原因的来源吗?
  • 原因,如果你不给top,left赋值,它们的默认值是auto,所以它们将根据默认位置计算,就像它们是相对的一样
猜你喜欢
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 2012-01-20
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
相关资源
最近更新 更多