0
1

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 3 years have passed since last update.

【jQuery】モーダルの作成方法

Posted at

完成形

  • SCSSを使用
  • jQueryを使用

See the Pen MWpXJmm by c-koch0514 (@c-koch0514) on CodePen.

HTML

html
<!-------------------  modal ------------------------->
<button id="modal-btn">modelを開く</button>
<div class="modal">
  <div class="modal-bg modal-close"></div>
  <div class="modal-content">
    <h1>モーダルタイトル</h1>
    <p>モーダルテキストモーダルテキストモーダルテキストモーダルテキストモーダルテキストモーダルテキストモーダルテキストモーダルテキストモーダルテキストモーダルテキストモーダルテキスト</p>

    <button class="modal-close">閉じる</button>
  </div>
</div>
<!-------------------  end of modal ------------------>

SCSS

scss
/***********************************************************************/
/**********                modalの設定                       *************/
/***********************************************************************/
# modal-btn {
  background-color: #000000;
  border-radius: 10px;
  font-size:15px;
  color: #fff;
  width: 130px;
  height: 50px;
}
.modal {
  display: none;
  height: 100%;
  position: fixed;
  top: 0;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 1;

  .modal-bg {
    background: rgba(0, 0, 0, 0.6);
    height: 100vh;
    width: 100%;
  }
  .modal-content{
    width: 300px;
    height: 200px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    padding: 10px 30px;
    
    .modal-close{
      position: absolute;
      top: 0;
      right: 0;
    }
  }
}
/*********************  ena of modal    **********************************/
/***********************************************************************/

jQuery

jQuery
$(function(){  

  /***************** modal モーダル **********************/
  // ウィンドウを開く
  $('#modal-btn').on('click', function() {
       $('.modal').fadeIn( 300 );
       return false;
  });

  // ウィンドウを閉じる
  $( '.modal-close' ).on( 'click', function() {
  $( '.modal' ).fadeOut( 300 );
  return false;
  });
  /*********************************************************/

});
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?