我有一个模式,它使用 JavaScript 显示/隐藏。
在模式中,将使用 JavaScript 插入图像。同样在图像上将存在一个 div 元素,它将模拟裁剪(获取图像的坐标)。
我在使图像保持在模态裁剪下方时遇到问题。 modal-crop 并且图像需要位于 modal-area 的中心。
我不能使用 grid 或 flex,因为我需要支持 IE9。
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
display: none;
overflow: hidden;
outline: 0;
}
.modal-area {
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
transform: translate(-50%, -50%);
margin: 0 auto;
padding: 30px;
background-color: blueviolet;
border-radius: 4px;
box-shadow: 0 0 50px black;
overflow: hidden;
}
.modal-area img {
margin-left: auto;
margin-right: auto;
}
.modal-crop {
position: relative;
background-color: aliceblue;
left: 0;
right: 0;
top: 0;
margin-left: auto;
margin-right: auto;
width: 200px;
height: 200px;
opacity: 0.2;
z-index: 2;
}
<div class="modal">
<div class="modal-area">
<div class="modal-crop"></div>
#img will be inserted here using Javascript#
</div>
</div>
最佳答案
你的图片需要绝对像这样定位:
.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1050;
overflow: hidden;
outline: 0;
min-height: 300px;
}
.modal-area {
position: absolute;
top: 50%;
left: 50%;
width: 50%;
height: 50%;
transform: translate(-50%, -50%);
margin: 0 auto;
padding: 30px;
background-color: blueviolet;
border-radius: 4px;
box-shadow: 0 0 50px black;
overflow: hidden;
}
.modal-area img {
margin-left: auto;
margin-right: auto;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
.modal-crop {
position: relative;
background-color: aliceblue;
left: 0;
right: 0;
top: 0;
margin-left: auto;
margin-right: auto;
height: 100%;
opacity: 0.2;
z-index: 2;
}
<div class="modal">
<div class="modal-area">
<div class="modal-crop"></div>
<img src="https://lorempixel.com/500/500/">
</div>
</div>
关于css - z-index 和相对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46970221/