만들기 방법:CSS/JS 모달
CSS와 JavaScript를 사용하여 모달을 만들어 보세요.
모달 만들기 방법
모달은 현재 페이지의 상단에 표시되는 대화 상자/팝업 창입니다:
第一步 - 추가 HTML:
<!-- 트리거/모달 열기 --> <button id="myBtn">모달 열기</button> <!-- 모달 --> <div id="myModal" class="modal"> <!-- 모달 내용 --> <div class="modal-content"> <span class="close">×</span> <p>모달에 있는 일부 텍스트.</p> </div> </div>
第二步 - 추가 CSS:
/* 模态框(背景) */ .modal { display: none; /* 默认隐藏 */ position: fixed; /* 保持位置固定 */ z-index: 1; /* 位于顶层 */ left: 0; top: 0; width: 100%; /* 全宽 */ height: 100%; /* 全高 */ overflow: auto; /* 如有需要,启用滚动 */ background-color: rgb(0,0,0); /* 备用颜色 */ background-color: rgba(0,0,0,0.4); /* 带不透明度的黑色 */ } /* 模态框内容/框 */ .modal-content { background-color: #fefefe; margin: 15% auto; /* 距顶部 15%,并居中 */ padding: 20px; border: 1px solid #888; width: 80%; /* 可能因屏幕尺寸而异 */ } /* 关闭按钮 */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; }
第三步 - 添加 JavaScript:
// 获取模态 var modal = document.getElementById("myModal"); // 获取打开模态框的按钮 var btn = document.getElementById("myBtn"); // 获取关闭模态框的 <span> 元素 var span = document.getElementsByClassName("close")[0]; // 当用户单击按钮时,打开模态框 btn.onclick = function() { modal.style.display = "block"; } // 当用户单击 <span> (x) 时,关闭模态框 span.onclick = function() { modal.style.display = "none"; } // 当用户单击模态框之外的任何位置时,将其关闭 window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } }
상단과 하단 추가
모달 상단, 본문, 하단에 클래스를 추가하세요:
<!-- 모달 내용 --> <div class="modal-content"> <div class="modal-header"> <span class="close">×</span> <h2>Modal Header</h2> </div> <div class="modal-body"> <p>모달 본문에 있는 텍스트</p> <p>기타 텍스트...</p> </div> <div class="modal-footer"> <h3>Modal Footer</h3> </div> </div>
모달 상단, 본문, 하단의 스타일을 설정하고 애니메이션을 추가하여 (모달 창이 슬라이딩으로 진입):
/* 모달 창 상단 */ .modal-header { padding: 2px 16px; background-color: #5cb85c; color: white; } /* 모달 창 본문 */ .modal-body {padding: 2px 16px;} /* 모달 창 하단 */ .modal-footer { padding: 2px 16px; background-color: #5cb85c; color: white; } /* 모달 창 내용 */ .modal-content { position: relative; background-color: #fefefe; margin: auto; padding: 0; border: 1px solid #888; width: 80%; box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); animation-name: animatetop; animation-duration: 0.4s } /* 애니메이션 추가 */ @keyframes animatetop { from {top: -300px; opacity: 0} to {top: 0; opacity: 1} }
하단 모달 창
하단으로 슬라이딩하는 전체 너비 모달 창을 만들어 보세요:
예제 - 하단 모달 창
相关页面
教程:如何创建模态图像
教程:如何创建灯箱