【发布时间】:2020-07-18 23:22:33
【问题描述】:
我想在其固定定位的父 div 之外呈现子组件。但是,子组件总是在其固定位置的父组件中呈现。以代码和图像为例,如何将“Test 2”渲染在与“Test 1”相同的位置,而不是在其固定位置的div内?我想基于浏览器视口而不是其固定的父 div 来渲染它。提前致谢!
import React,{useState} from 'react'
function Testing() {
const [isCLicked, setIsCLicked] = useState(false)
return (
<div style={{
position:'relative',
width:'100vw',
height:'100vh',
backgroundColor:'lightblue',
}}>
<p style={{
position:'absolute',
top:'10%',
left:'10%',
backgroundColor:'black',
color:'white'
}}>Test 1</p>
<div style={{
position:'fixed',
top:0,
left:'50%',
height:'100vh',
width:'10vw',
backgroundColor:'green'
}}>
<button onClick={()=>setIsCLicked(true)}>create new text box</button>
{isCLicked &&
<p style={{
position:'absolute',
top:'10%',
left:'10%',
backgroundColor:'black',
color:'white'
}}>Test 2</p>
}
</div>
</div>
)
}
export default Testing
【问题讨论】:
标签: javascript css reactjs css-position fixed