2020/05/13 23:28
プラグイン不要!簡単コピペでモーダルウィンドウ実装方法
・HTML
・jQuery
・CSS
3種でサクっと実装できるシンプルなモーダルウィンドウです。
特にプラグイン等も不要なので軽量でカスタムしやすいと思います。
コピペでOK
【HTML】
<div class="popup-wrapper">
<div class="popup">
<div class="popup-area">
<div class="popup-content-box">
<img src="【画像URL】" alt="" class="popup-img">
<div class="popup-close-btn">×</i></div>
</div>
</div>
</div>
</div>
<script>
$('.popup-area').click(function(){
$('.popup-wrapper').addClass('active');
});
</script>
<style>
.popup-wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100vh;
text-align: center;
margin: 0 auto;
z-index: 99;
}
.popup-wrapper.active {
-webkit-animation: pop-close-anime 2s both 0s ease;
animation: pop-close-anime 2s both 0s ease;
}
@-webkit-keyframes pop-close-anime {
0% {
opacity: 1;
}
99% {
opacity: 0;
}
100% {
opacity: 0;
display: none;
z-index: -9999;
}
}
@keyframes pop-close-anime {
0% {
opacity: 1;
}
99% {
opacity: 0;
}
100% {
opacity: 0;
display: none;
z-index: -9999;
}
}
.popup {
position: relative;
display: block;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: auto;
height: 100vh;
overflow-y: auto;
text-align: center;
margin: 0 auto;
}
/* チェックボックスの初期設定 */
#popup-on {
display: none;
}
/* チェックされたらポップアップウインドウを開く */
#popup-on:checked + .popup {
display: block;
}
/* 閉じるアイコン(右上) */
.icon-close {
background: #000;
color: #fff;
font-size: 30px;
padding: 0 10px;
position: absolute;
right: 0;
}
/* 閉じるボタン */
.btn-close {
background: #000;
border-radius: 10px;
color: #fff;
padding: 10px;
cursor: pointer;
margin: 10px auto;
width: 95%;
text-align: center;
}
/* 開くボタン */
.btn-open {
background: #fff;
border-radius: 10px;
color: #fff;
padding: 10px;
cursor: pointer;
margin: 10px auto;
width: 95%;
text-align: center;
}
/* ポップアップの内容 */
.popup-area {
margin: 100px auto;
width: 100%;
}
.popup-img {
position: relative;
width: 580px;
height: auto;
text-align: center;
z-index: 999;
margin: 0 auto;
box-shadow: 3px 6px 5px rgba(0,0,0,0.15);
}
.popup-content-box {
position: relative;
width: 580px;
height: auto;
text-align: center;
z-index: 999;
margin: 0 auto;
}
.popup-close-btn {
font-size: 3rem;
color: #888;
position: absolute;
right: 0;
top: 0;
z-index: 999;
}
.popup-close-btn:hover {
cursor: pointer
}
@media only screen and (max-width: 767px) {
.popup-img,
.popup-content-box {
width: 100%;
}
}
</style>
スタイルはお好みで調整してください。
では現場から以上です!
4120