我想制作一个带有幻灯片的弹出窗口,但我有一个问题,我无法将导航箭头放置在图像容器内,因此当调整网站大小时,它们会保留在同一位置。
HTML 片段:
<div id="popup" class="popup hidden">
<div class="popup-content">
<span id="closePopup" class="close">×</span>
<button id="prevImage" class="nav-button"><</button>
<img id="popupImage" src="" alt="Project Image">
<button id="nextImage" class="nav-button">></button>
<div class="popup-text">
<p id="popupName"></p>
<p id="popupDescription"></p>
</div>
</div>
</div>
CSS 片段:
/* Popup */
.popup {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
overflow: auto;
background-color: rgba(0,0,0,0.5);
}
.popup-content {
background-color: #494949;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
padding-top: 30px;
padding-bottom: 30px;
padding-left: 30px;
border-radius: 20px;
width: 70vw;
max-width: 1600px;
height: auto;
max-height: 1000px;
text-align: left;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
position: relative;
}
#popupImage {
width: 55%;
height: auto;
border-radius: 20px;
margin-right: 8%;
position: relative;
}
#popupName {
font-size: 3vh;
font-weight: bold;
margin-bottom: 10px;
}
#popupDescription {
font-size: 2vh;
}
/* Navigation Buttons */
.nav-button {
background-color: transparent;
border: none;
color: #fff;
font-size: 2em;
cursor: pointer;
position: absolute;
top: 50%;
transform: translateY(-50%);
z-index: 1;
}
#prevImage {
left: 1.5%;
}
#nextImage {
right: 1.5%;
}
与将弹出窗口名称和描述放入包含
div
元素的方式类似,您可以将下一个/上一个按钮和图像放入另一个 div
容器中,并相对于 it 而不是整体内容绝对定位按钮容器。
示例:
<div id="popup" class="popup hidden">
<div class="popup-content">
<span id="closePopup" class="close">×</span>
<div id="image-container">
<button id="prevImage" class="nav-button"><</button>
<img
id="popupImage"
src="...."
alt="Project Image"
/>
<button id="nextImage" class="nav-button">></button>
</div>
<div class="popup-text">
<p id="popupName">....</p>
<p id="popupDescription">....</p>
</div>
</div>
</div>
...
.popup-content {
color: #dddddd;
background-color: #494949;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
padding-top: 30px;
padding-bottom: 30px;
padding-left: 30px;
border-radius: 20px;
width: 70vw;
max-width: 1600px;
height: auto;
max-height: 1000px;
text-align: left;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
position: relative;
gap: 8%; /* <-- add "padding" here */
}
...
#popupImage {
border-radius: 20px;
/* margin-right: 8%; remove */
position: relative;
}
...
#image-container {
height: auto;
position: relative;
}
...