123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="overlay" class="overlay"></div>
<div id="modal-content" class="modal-content">
	<ul id="pager" class="pager">
		<li class="prev"><button class="prev-btn"><</button></li>
		<li class="next"><button class="next-btn">></button></li>
	</ul>
	<button id="close-btn" class="close-btn">☓</button>
	<div id="enlarged-images" class="enlarged-images"></div>
</div>
<div id="thumbnails" class="thumbnails">
	
</div>
            
          
!
            
              @mixin translate($hVal, $vVal) {
	transform: translate($hVal, $vVal);
}

@mixin tl($pVal, $tVal, $lVal) {
	position: $pVal;
	top: $tVal;
	left: $lVal;
}

@mixin tr($pVal, $tVal, $rVal) {
	position: $pVal;
	top: $tVal;
	right: $rVal;
}

%inactive {
	opacity: 0;
	visibility: hidden;
}

* {
	box-sizing: border-box;
}

body {
	background: #333;
}

.thumbnails {
	> div {
		background: #ccc;
		border: 5px solid #fff;
		cursor: pointer;
		display: block;
		float: left;
		overflow: hidden;
		position: relative;
		padding-bottom: 40%;
		width: 50%;
		@media (min-width: 800px) {
			padding-bottom: 15%;
			width: 25%;
		}
	}
	img {
		user-select: none;
			-webkit-user-select: none;
			-webkit-user-drag: none;
		display: block;
		max-height: 200%;
		@include tl(absolute, 0, 0);
		width: 100%;
	}
}

.overlay {
	@extend %inactive;
	background: #000;
	height: 100%;
	@include tl(fixed, 0, 0);
	transition: 0.5s;
	width: 100%;
	z-index: 1000;
	&.active {
		opacity: 0.6;
		visibility: visible;
	}
}

.modal-content {
	@extend %inactive;
	background: #ccc;
	height: 50vw;
	overflow: hidden;
	@include tl(fixed, 50%, 50%);
	@include translate(-50%, -50%);
	transition: 0.5s;
	width: 50%;
	z-index: 2000;
	&.active {
		opacity: 1;
		visibility: visible;
	}
	img {
		//@extend %inactive;
		@include tl(absolute, 50%, 50%);
		@include translate(-50%, -50%);
		transition: 0.5s;
		width: 100%;
		/*
		&.active {
			opacity: 1;
			visibility: visible;
		}
		*/
	}
	@media (min-width: 800px) {
		height: 400px;
		width: 500px;
	}
}

.close-btn {
	background: #333;
	border: none;
	color: #fff;
	cursor: pointer;
	height: 8vw;
	line-height: 8vw;
	@include tr(absolute, 0, 0);
	text-align: center;
	width: 8vw;
	z-index: 4000;
	@media (min-width: 800px) {
		line-height: 44px;
		height: 44px;
		width: 44px;
	}
}

.pager {
	height: 8vw;
	list-style: none;
	margin: 0;
	padding: 0;
	@include tl(absolute, 50%, 0);
	@include translate(0, -50%);
	width: 100%;
	z-index: 4000;
	@media (min-width: 800px) {
		height: 44px;
	}
	> .prev,
	> .next {
		position: absolute;
		top: 0;
		> button {
			background: #333;
			border: none;
			color: #fff;
			cursor: pointer;
			height: 8vw;
			line-height: 8vw;
			text-align: center;
			width: 8vw;
			z-index: 5000;
			@media (min-width: 800px) {
				height: 44px;
				line-height: 44px;
				width: 44px;
			}
		}
	}
	> .prev {
		left: 0;
	}
	> .next {
		right: 0;
	}
}

            
          
!
            
              (() => {
	class Asset {
		constructor() {
			this.urls = [
				'https://tsukulog.net/wp-content/uploads/2015/06/HIRA86_MBAkey-thumb-1000xauto-17206.jpg',
				'https://tsukulog.net/wp-content/uploads/2015/06/GAK88_nekokafeneko-thumb-1000xauto-16206.jpg',
				'https://tsukulog.net/wp-content/uploads/2015/06/N825_benchinouenoshironeko-thumb-autox1000-14809.jpg',
				'https://tsukulog.net/wp-content/uploads/2015/06/https-www.pakutaso.com-assets_c-2015-05-26NJ_retuwotukuruahirucyan-thumb-1000xauto-14005.jpg'
			];
			
			this.images = [];
		}
		
		createImage() {
			for(let i = 0; i < this.urls.length; i++) {
				const image = new Image();
				image.src = this.urls[i];
				this.images[i] = image;
			}
		}
	}
	
	class Thumbnail {
		constructor() {
			//サムネイル一覧
			this.elem = document.getElementById('thumbnails');
			this._image = this.elem.getElementsByTagName('img');
		}
		
		setupListener(modalContent, overlay) {
			const thumbnail = this._image;
		
			for(let i = 0; i < thumbnail.length; i++) {
				thumbnail[i].addEventListener('click', (e) => {
					e.preventDefault();

					//クリックしたサムネイルを取得
					const target = e.target;

					const clone = target.cloneNode(true);
					
					//クリックしたサムネイルの番号
					const currentIndex = [].slice.call(thumbnail).indexOf(target);
					//console.log(currentIndex);
					
					//console.log(thumbnail[currentIndex-2]);
					
					//オーバーレイをフェードイン
					overlay.show();

					//モーダルウィンドウをフェードイン
					modalContent.show();
					modalContent.removeImage();
					modalContent.addImage(clone);
					modalContent.currentIndex = currentIndex;
				});
			}
		}
		
		get image() {
			return this._image;
		}
		
		insertImages(images) {
			images.forEach((image) => this.elem.appendChild(image));
		}
		
		wrapImages(images) {
			images.forEach((image) => image.outerHTML = `<div class="thumbnail">${image.outerHTML}</div>`);
		}
	}
	
	//モーダルウィンドウに関するクラス
	class ModalContent {
		constructor() {
			
			//モーダルウィンドウ
			this.elem = document.getElementById('modal-content');
			
			//拡大画像を子に持つ親要素
			this.enlargedImages = document.getElementById('enlarged-images');
			
			this._currentIndex = 0;
		}
		
		get currentIndex() {
			return this._currentIndex;
		}
		
		set currentIndex(value) {
			if(typeof value === 'number') {
				this._currentIndex = value;
			}
		}
		
		//モーダルウィンドウを表示するメソッド
		show() {
			this.elem.classList.add('active');
		}
		
		//モーダルウィンドウを非表示にするメソッド
		hide() {
			this.elem.classList.remove('active');
		}
		
		addImage(image) {
			this.enlargedImages.appendChild(image);
		}
		
		removeImage() {
			if(this.enlargedImages.firstChild) {
				this.enlargedImages.removeChild(this.enlargedImages.firstChild);
			}
		}
		
		slide(index, image) {

			//番号の最大値
			const max = image.length - 1;

			//渡された番号が0未満であればmaxに変更
			if(index < 0) {
				index = max;
			}

			//渡された番号がmax超過であれば0に変更
			if(index > max) {
				index = 0;
			}

			//渡された番号の拡大画像を表示
			const clone = image[index].cloneNode(true);
			
			this.removeImage();
			this.addImage(clone);
			
			//現在の番号を更新
			this.currentIndex = index;
		}
	}
	
	//オーバーレイに関するクラス
	class Overlay {
		constructor() {
			this.elem = document.getElementById('overlay');
		}
		
		setupListener(modalContent) {
			this.elem.addEventListener('click', (e) => {
				this.hide();
				modalContent.hide();
			});
		}
		
		show() {
			this.elem.classList.add('active');
		}
		
		hide() {
			this.elem.classList.remove('active');
		}
	}
	
	//ページャーに関するクラス
	class Pager {
		constructor() {
			this.elem = document.getElementById('pager');
			this.btn = this.elem.getElementsByTagName('button');
		}
		
		setupListener(modalContent, thumbnail) {
			const pagerBtn = this.btn;
		
			//.prev-btnをクリック→一つ前の画像に切り替え
			//.next-btnをクリック→一つ後の画像に切り替え
			for(let i = 0; i < pagerBtn.length; i++) {
				pagerBtn[i].addEventListener('click', (e) => {
					if(pagerBtn[i].classList.contains('prev-btn')) {
						modalContent.currentIndex = modalContent.currentIndex - 1;
						console.log(modalContent.currentIndex);
						//console.log(modalWindow.image[modalWindow.currentIndex]);
						//modalWindow.slide(modalWindow.currentIndex, modalWindow.image);
					}else{
						modalContent.currentIndex = modalContent.currentIndex + 1;
						console.log(modalContent.currentIndex);
						//modalWindow.slide(currentIndex + 1, modalWindow.image);
					}
					
					modalContent.slide(modalContent.currentIndex, thumbnail.image);
				});
			}
		}
	}
	
	//閉じるボタンに関するクラス
	class CloseBtn {
		constructor() {
			this.elem = document.getElementById('close-btn');
		}
		
		//.close-btnをクリックできる状態にするメソッド
		setupListener(overlay, modalContent) {
			this.elem.addEventListener('click', (e) => {
				overlay.hide();
				modalContent.hide();
			});
		}
	}
	
	//インスタンス化
	const asset = new Asset();
	const thumbnail = new Thumbnail();
	const modalContent = new ModalContent();
	const overlay = new Overlay();
	const pager = new Pager();
	const closeBtn = new CloseBtn();
	
	//全体を初期化する関数
	function init() {
		asset.createImage();
		thumbnail.insertImages(asset.images);
		thumbnail.wrapImages(asset.images);
		setupListeners();
	}
	
	//モーダルウィンドウを操作できる状態にする関数
	function setupListeners() {
		thumbnail.setupListener(modalContent, overlay);
		overlay.setupListener(modalContent);
		pager.setupListener(modalContent, thumbnail);
		closeBtn.setupListener(overlay, modalContent);
	}
	
	init();
})();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console