2018/04/11

簡単なモーダルウィンドウのサンプル






動作サンプル

See the Pen モーダルウィンドウのサンプル by takapen (@takapen) on CodePen.






btn

中身

btn2

中身2





/*とりあえずのリセット*/
div,p{margin: 0;padding: 0;}

/*ここからモーダル用*/
.js-modal__bg {
  width: 100%;
  height: 100%;
  display: none;
  background-color: rgba(0,0,0,0.6);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
}
.js-modal__main {
  width: 260px;
  height: 400px;
  margin-left:-150px;
  padding: 20px;
  top: 15%;
  left: 50%;
  background: #fff;
  border-radius:5px;
  position: absolute;
}
.js-modal__btn {
  cursor: pointer
}
.js-modal__btn--close {
  width: 30px;
  height: 30px;
  background: #333;
  border-radius: 50%;
  position: absolute;
  top: -12px;
  right: -12px;
  z-index: 101;
  cursor:pointer;
}
.js-modal__btn--close:before{
  content:"";
  width: 20px;
  height: 20px;
  border-right:2px solid #fff;
  transform: rotate(-45deg);
  position: absolute;
  top:12px;
  left:-3px;
}
.js-modal__btn--close:after{
  content:"";
  width: 20px;
  height: 20px;
  border-right:2px solid #fff;
  transform: rotate(45deg);
  position: absolute;
  top:-2px;
  left:-3px;
}




$(function(){

  var modalBtn = $('.js-modal__btn');
  var modalBtnClose = $('.js-modal__btn--close');
  var modalBg = $('.js-modal__bg');
  var modalMain = $('.js-modal__main');

  modalBtn.on('click', function (e) {
    $(this).next(modalBg).fadeToggle();
  });

  modalBtnClose.on('click', function (e) {
    modalBg.fadeOut();
  });

  modalMain.on('click', function (e) {
    e.stopPropagation();
  });

  modalBg.on('click', function () {
    $(this).fadeOut();
  });

});