1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

中身を切り替えできるお手軽モーダルウィンドウの作り方(HTML/SCSS)

Last updated at Posted at 2019-10-11

はじめに

中身を切り替えできるお手軽モーダルウィンドウの作り方。

使用言語

  • HTML
  • SCSS(CSSでも可)

コード

See the Pen JjjYvOG by k1-web (@k1-web) on CodePen.

やってること

ラジオボタンを判定にしてチェックされているかどうかで表示非表示を切り替えている。
「#close(閉じる)」か「#overlay(背景)」を押すなどのチェックしている状態だと、モーダルウィンドウを非表示にする。
「#handle-1」にチェックが付いていたら閉じる・背景・コンテンツを表示させる。
表示させるコンテンツは「#handle-1」に隣接する次の要素を表示させる。

xxx.html
	<input type="radio" name="modal" id="close">
	<input type="radio" name="modal" id="overlay" checked>

	<input type="radio" name="modal" id="handle-1">
		<div id="modal-1" class="wrap wrap-1"></div>


xxx.css
	#handle-1 + #modal-1 {
		display: none;
	}
	#handle-1:checked + #modal-1 {
		display: block;
	}

背景がスクロールするのがイヤな人向け追加jQuery

PCとSPとで少し記述は違いますが、以下のjQueryを追加することで、背景スクロールも制御できます。

PC用

pc.js
	$('input[name="modal"], label.handle').on('change blur click focus', function(){
		if ( $('input#close').prop('checked') || $('input#overlay').prop('checked') ){
			$('html, body').removeAttr('style');
		} else {
			$('html, body').css({'overflow':'hidden'});
		}
	});

SP用

sp.js
	$('input[name="modal"], label.handle').on('change blur click focus', function(){
		if ( $('input#close').prop('checked') || $('input#overlay').prop('checked') ){
			$('html, body').removeAttr('style');
			$('body').off('.noScroll');
		} else {
			$('html, body').css({'overflow':'hidden'});
			$('body').on('touchmove.noScroll', function(e) {
				e.preventDefault();
			});
		}
	});
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?