Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- 
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>画像のモーダルウィンドウ</title>

  <link rel="stylesheet" href="style.css">
</head>
 -->
<body>
  <!-- 
モーダルウィンドウに直接関係があるのは、class属性"js-modalWindow"が付いている
a要素とその子要素のimg要素です。
その他のコードはレイアウトや装飾目的で記述しました。
-->
  <div class="l-container">
    <h1 class="m-pageTitle">画像のモーダルウィンドウ</h1>
    <div class="l-spacer --xlTop">
      <ul class="l-grid">
        <li class="l-grid__item">
          <a class="m-modalLink js-modalWindow" href="https://drive.google.com/uc?export=view&id=15Dom_6c0greOYOl_ADjkXf0wgwpiHfNP">

            <img class="m-modalImage" src="https://drive.google.com/uc?export=view&id=18q6XsxqI_GPdlOyr52D1t5WQwz5pJvoY" alt="ぶどう">
          </a>
        </li>
        <li class="l-grid__item">
          <a class="m-modalLink js-modalWindow" href="https://drive.google.com/uc?export=view&id=1UTcL-GB9tCDnpeV3hDZRwNYQjtn-Noid">
            <img class="m-modalImage" src="https://drive.google.com/uc?export=view&id=1lQ2hmVgtqLXxHS9YrNDd6BcL5h8IOeS5" alt="りんご">
          </a>
        </li>
        <li class="l-grid__item">
          <a class="m-modalLink js-modalWindow" href="https://drive.google.com/uc?export=view&id=1ajfs_9vegmJ2XRlGG_UqDpKiTjXU1cFR">
            <img class="m-modalImage" src="https://drive.google.com/uc?export=view&id=1kAB3UkRy0n3Vr_SijY7dDBPnrfZ0eVPj" alt="レモン">
          </a>
        </li>
      </ul>
    </div>
    <!-- /.l-spacer -->
  </div>
  <!-- /.l-container -->

  <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
  <script src="app.js"></script>
</body>
<!-- </html> -->
              
            
!

CSS

              
                /* ===== デフォルトのCSSリセット =====*/
* {
  margin: 0;
  padding: 0;
}

body {
  padding: 0 1em;
  overflow-x: hidden;
}

li {
  list-style-type: none;
}

img {
  vertical-align: bottom;
}

/* ===== ここから =====*/
/* JavaScript操作用 */

/* overlay */
.overlay {
  display: none;
  /* 手順⑤ */
  position: fixed;
  inset:0 auto 0 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: rgba(0, 0, 0, 0.5);
}

/* modalContent */
.modalContent {
  position: absolute;
  inset:0;
  max-width: 80%;
  max-height: 80%;
  margin: auto;
}

/* is-active */
.is-active {
  overflow: hidden;
}

/* ===== ここまで =====*/

/* 以下、今回関係なし */
/* ===== layout =====*/
.l-container {
  max-width: 100%;
  width: 960px;
  margin: 2.5em auto;
}

.l-spacer.--xlTop {
  margin-top: clamp(2em, 4vw, 5em);
}

.l-grid {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  justify-content: center;
  gap: 2em;
}

@media screen and (min-width: 768px) {
  .l-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media screen and (min-width: 1200px) {
  .l-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

.l-grid__item {
  text-align: center;
}

/* module */
.m-pageTitle {
  font-size: clamp(1em, 3vw, 2.5em);
  font-weight: bold;
  text-align: center;
}

.m-modalLink {
  display: inline-block;
  border: 5px solid rgba(0, 0, 0, 0.5);
  transition: border 0.3s ease-in-out;
}
@media (hover: hover) and (pointer: fine) {
  .m-modalLink:hover {
    border-color: transparent;
  }
}

.m-modalImage {
  max-width: 100%;
  object-fit: cover;
}

@media screen and (min-width: 1200px) {
  .m-modalImage {
    width: 100%;
  }
}

              
            
!

JS

              
                $(function () {
  //クリックイベントの作成
  $(".js-modalWindow").click(function () {
    //body要素の末尾にoverlayクラスの付いた空divを作成
    $("body").append("<div class=overlay></div>");
    //overlayをフェードインして表示
    $(".overlay").fadeIn(300);
    //クリックしたa要素のhref属性を取得
    var target = $(this).attr("href");
    //ポップアップ用のimg要素を変数にまとめる
    var largeImage = "<img class='modalContent' src='" + target + "'>";
    //ポップアップ用のimg要素を空divの中に追加
    $(".overlay").append(largeImage);
    //body要素にclass属性を追加
    $("body").addClass("is-active");
    //a要素のページ遷移機能を無効化
    return false;
  });

  //非表示用イベントの作成
  $("body").on("click", ".overlay", function () {
    //オーバーレイをフェードアウトさせる
    $(this).fadeOut(300, function () {
      //フェードアウトが終了した後に空div要素を削除
      $(this).remove();

      //body要素に追加したclass属性を削除
      $("body").removeClass("is-active");
    });
  });
});

              
            
!
999px

Console