이미지의 오른쪽 상단 모서리에 닫기 버튼 (x)을 배치하려고합니다.
내 페이지 바닥 글 바로 앞 :
<div class="modal" id="modal">
<span class="close" id="close">✖</span>
<img class="modal-content" id="modal-content">
</div>
모달 표시 방법 :
function showModal(name) {
modal.style.display = "block";
modalImg.src = document.getElementById(name).src;
}
그리고 여기 내 스타일이 있습니다.
/* Background when image is shown */
#modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(119, 119, 119); /* Fallback color */
background-color: rgba(119, 119, 119, 0.9); /* Black w/ opacity */
}
/* Image */
#modal-content {
/* Horizontally center image */
margin: auto;
/* Sizing */
display: block;
max-width: 90%;
max-height: calc(100vh * 0.5);
width: auto;
/* Zoom (image gets bigger) on open */
animation-name: zoom;
animation-duration: 0.6s;
/* Style */
border: 10px solid #ffffff;
box-shadow: rgba(0, 0, 0, 0.54) 0px 0px 7px 4px;
/* Vertically center image */
position: relative;
top: 40%;
transform: translateY(-40%);
}
@keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
/* Close Button */
#close {
/* Position */
position: absolute;
top: 10px;
right: 10px;
/* Style */
color: #ffffff;
font-size: 20px;
font-weight: bold;
transition: 0.3s;
background-color: #000000;
border: 3px solid #ffffff;
box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 25px 3px;
/* Makes the Button round */
border-radius: 50%;
padding-left: 7px;
padding-right: 7px;
padding-bottom: 3px;
}
/* Change cursor when pointing on button */
#close:hover,
#close:focus {
text-decoration: none;
cursor: pointer;
}
/* 100% image width on smaller screens */
@media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}
문제는
position: absolute;
top: 10px;
right: 10px;
닫기 버튼을 이미지의 오른쪽 상단이 아닌 페이지의 오른쪽 상단에 배치합니다.
어떻게 고칠 수 있습니까?
close
가장 가까운 위치에있는 조상을 기준으로 위치를 지정 하는 요소 (원하는대로) modal
대신 div modal-content
가 있습니다.
다음 modal-content
과 close
같이 (상대적) div에서 및 "버튼" 을 래핑하는 것이 좋습니다 .
<div class="modal" id="modal">
<div id='modal-content' class='modal-content">
<span class="close" id="close">✖</span>
<img> //make img height/width to 100%
</div>
</div>
그러면 닫기 버튼이 절대 div의 오른쪽 상단 위치와이 div 내부의 이미지에 배치됩니다.
닫기 버튼이 나타나지 않으면 z-index
이미지보다 높은지 확인하십시오 .
코드 펜을 만들었습니다 : https://codepen.io/jpaulet/pen/RwGxpxy
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다