使用“css”显示屏幕中心创建透明的“淡入”和“淡出”弹出窗口

oo7oh9g9  于 2023-03-14  发布在  其他
关注(0)|答案(2)|浏览(129)

想创建一个透明的fade infade out弹出窗口使用css显示屏幕的中心。我已经创建了一个,但总是显示屏幕的左下角。有人能请建议?

尝试了组件方式的React,但转换不适用于弹出窗口

const Popup = () => {

    const style = {
      position: 'absolute',
      left: '50%',
      top: '50%',
      transform: 'translate(-50%,-50%)',
      width: '250px',
      height: '100px',
      borderRadius: '5px',
      backgroundColor: '#6d418c',
      opacity: 0.5,
      transition: 'all 250ms linear 2s'
    }
  
    return (
      <div>
        {createPortal(
          <div style={style} className={`alert alert-success ${ showLatestMailAlert ? 'alert-shown' : 'alert-hidden'}`}
            onTransitionEnd={() => setShowLatestMailAlert(false)}>
            <span class="popuptext" id="myPopup"><strong>Success!</strong> This child is placed in the document body.</span>
          </div>,
          document.body
        )}
      </div>
    ) 
  }

 return (
    <div id="App">
        <div className='parent'>
             <div className='adminSection'>
                <h1>Create a new blog</h1>
                <div className='row'>
                    <div className='createBlogSection'>

                        <div data-color-mode="dark" className='container'>
                            <MDEditor
                                className='reactEditorArea'
                                id="blogdetails"
                                name="blogdetails"
                                value={blogValue}
                                onChange={setBlogValue}
                            />
                            {/* <MDEditor.Markdown className='reactEditorOutput' source={blogValue} style={{ whiteSpace: 'pre-wrap' }} /> */}
                        </div>
                        <form>
                        <div className='row'>
                            <div className='tagsName'>
                                <label>Tags:</label>
                                <input
                                    type="text"
                                    id="tags"
                                    name="tags"
                                    placeholder="enter tags"
                                    value={tagValue}
                                    onChange={(e) => setTagValue(e.target.value)}     
                                />
                            </div>
                            {/* <span className="validationmsg">
                                {errors.tags && errors.tags.type === "required" && <span>Tag is required !</span>}
                                {errors.tags && <span>{errors.tags.message}</span>}
                            </span> */}
                        </div>
                        </form>                 
                        <label>
                         <span className="adminSuccessMsg">{helperText}</span>
                        </label>
                        <div className='row'>
                            <div className='submitSection'>
                                <input type="submit" onClick={onSubmit} className="submitBtn" />
                            </div>
                        </div>

                    </div>
                </div>
            </div>
            
        </div>
       <Popup/>
    </div> 
)
kmb7vmvb

kmb7vmvb1#

你可能需要从DOM流中删除弹出窗口,这取决于父元素。它们的CSS可能会影响子元素的位置。
为此,您可以使用portal

const Popup = () => {

  const style = {
    position: 'absolute',
    left: '50%',
    top: '50%',
    transform: 'translate(-50%,-50%)'
  }

  return (
    <div>
      <p>This child is placed in the parent div.</p>
      {createPortal(
        <p style={style}>This child is placed in the document body.</p>,
        document.body
      )}
    </div>
  ) 
}

nuypyhwy

nuypyhwy2#

将以下css属性添加到.alert类中

position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);

相关问题