JavaScriptでモダールウィンドウを作成

今回はJavaScriptでモダールウィンドウを作成する方法を紹介します。

サムネイルをクリックすると画像がモダールウィンドウで表示されます。モダールかクローズボタンをクリックするとモダールウィンドウが閉じます。

サムネイルをクリックすると画像がモダールウィンドウで表示

モダールかクローズボタンをクリックするとモダールウィンドウが閉じる

コード

<div id="modal-column1">
	<ul class="jazz-gallery">
 		<li class="modal-button"><img src="images/jazz002.jpg" alt""/></li>
 		<li class="modal-button"><img src="images/jazz003.jpg" alt""/></li>
 		<li class="modal-button"><img src="images/jazz004.jpg" alt""/></li>
	</ul>
</div>
#modal-column1 {
    display: grid;
	align-content: center;
	align-items: center;
	height: 100vh;
	width: 100vw;
	background-image: url("../images/jazz001.jpg");
	background-repeat: no-repeat;
	background-position: center;
	background-size: cover;
	position: relative;
	overflow: hidden;
}
.jazz-gallery{
	display: flex;
	margin: auto;
}
.jazz-gallery li{
	width: 20vw;
	height: 20vw;
	max-width: 200px;
	max-height: 200px;
	overflow: hidden;
	cursor: pointer;
}
.jazz-gallery li img{
	width: 100%;
	height: 100%;
	object-fit: cover;
}
.modal {
	width: 100vw;
	height: 100vh;
	position: fixed;
	top: 0;
	left: 0;
	background-color: rgba(0, 0, 0, 0.5);
	display: flex;
	justify-content: center;
	align-items: center;
	z-index: 20;
	cursor: pointer;
}
.inner {
	top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
	min-width: 350px;
	position: fixed;
	text-align: center;
	max-width: 1200px;
	height: auto;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	animation: fadeInAnimation 800ms ease-out;
	z-index: 30;
	aspect-ratio: 4 / 3;
	overflow: hidden;
}
.inner img {
    width: 100%;
   	height: 100%;
    object-fit: cover;
}
.closemodal{
	width: 50px;
	height: 50px;
	/*background: #000;*/
	position: fixed;
	top: 0;
	right: 0;
	z-index: 200;
	cursor: pointer;
}
.closemodal::after, .closemodal::before{
	content: '';
	width: 30px;
	height:1px;
	top: 50%;
	left: 50%;
	background: #fff;
	position: absolute;
}
.closemodal::after{
	transform: translate(-50%,-50%) rotate(45deg);
}
.closemodal::before{
	transform: translate(-50%,-50%) rotate(-45deg);
}
@keyframes fadeInAnimation {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
const modalButton = document.querySelectorAll('.modal-button');

modalButton.forEach((image)=>{
  image.addEventListener('click', (event) => {
	 const moalImageSrc =  event.target.src;
	 const modalElement = document.createElement('div');
	//modalクラスを付与する
	modalElement.classList.add('modal');
	
	//モダールウィンドウの内部要素を生成する
	const innerElement = document.createElement('div');
	innerElement.classList.add('inner');
	innerElement.innerHTML = `
		<img src="${moalImageSrc}" alt""/>
	`;
	  
	 //クローズボタンを生成する
	const closeElement = document.createElement('div');
	closeElement.classList.add('closemodal');
	  
	//body要素にモダールウィンドウを配置する
	document.body.appendChild(modalElement);
	document.body.appendChild(innerElement);
	document.body.appendChild(closeElement);
	//内部要素をクリックしたらモダールウィンドウを削除する処理
	modalElement.addEventListener('click', () => {
		
		modalElement.animate(
			{
				opacity: [1, 0]
			},
			{
				duration: 400,
				easing: 'ease',
				fill: 'forwards'
			}
		);
		innerElement.animate(
			{
				opacity: [1, 0]
			},
			{
				duration: 400,
				easing: 'ease',
				fill: 'forwards'
			}
		);
		closeElement.animate(
			{
				opacity: [1, 0]
			},
			{
				duration: 400,
				easing: 'ease',
				fill: 'forwards'
			}
		);
		closeElement.animate(
			{
				transform: ['translate(-50%,-50%) rotate(45deg)','translate(-50%,-50%) rotate(0deg)']
			},
			{
				duration: 400,
				pseudoElement: "::after",
				easing: 'ease',
			}
		);
		closeElement.animate(
			{
				transform: ['translate(-50%,-50%) rotate(-45deg)','translate(-50%,-50%) rotate(-90deg)']
			},
			{
				duration: 400,
				pseudoElement: "::before",
				easing: 'ease',
			}
		);
		setTimeout( ()=> {
			closeModalWindow(modalElement);
			closeModalWindow(innerElement);
			closeModalWindow(closeElement);
		}, 400)
	});
	//内部要素をクリックしたらモダールウィンドウを削除する処理
	closeElement.addEventListener('click', () => {
		
		modalElement.animate(
			{
				opacity: [1, 0]
			},
			{
				duration: 400,
				easing: 'ease',
				fill: 'forwards'
			}
		);
		innerElement.animate(
			{
				opacity: [1, 0]
			},
			{
				duration: 400,
				easing: 'ease',
				fill: 'forwards'
			}
		);
		closeElement.animate(
			{
				opacity: [1, 0]
			},
			{
				duration: 400,
				easing: 'ease',
				fill: 'forwards'
			}
		);
		closeElement.animate(
			{
				transform: ['translate(-50%,-50%) rotate(45deg)','translate(-50%,-50%) rotate(0deg)']
			},
			{
				duration: 400,
				pseudoElement: "::after",
				easing: 'ease',
			}
		);
		closeElement.animate(
			{
				transform: ['translate(-50%,-50%) rotate(-45deg)','translate(-50%,-50%) rotate(-90deg)']
			},
			{
				duration: 400,
				pseudoElement: "::before",
				easing: 'ease',
			}
		);
		setTimeout( ()=> {
			closeModalWindow(modalElement);
			closeModalWindow(innerElement);
			closeModalWindow(closeElement);
		}, 400)
	});
	  
  });
});
function displayModalWindow() {
	//モダールウィンドウを生成する
}

function closeModalWindow(modalElement) {
	document.body.removeChild(modalElement)
}

RECOMMEND

CONTACT

Webサイト作成は、是非
CXG DESIGNへご検討ください。