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

Auto Save

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

              
                
<body>
<div class="change-img">
  <div class="change-img__main js-change-img-main">
    <img src="https://images.unsplash.com/photo-1734426668495-670bba0a8d79?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3Mzc2NjU0MDJ8&ixlib=rb-4.0.3&q=8" alt="キャストの画像" />
  </div>
  <div class="change-img__img-sub">
    <ul class="change-img__list">
      <button class="change-img__item js-change-img-button">
        <img src="https://images.unsplash.com/photo-1734426668495-670bba0a8d79?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3Mzc2NjU0MDJ8&ixlib=rb-4.0.3&q=8" alt="キャストのサブ画像" />
      </button>
      <button class="change-img__item js-change-img-button">
        <img src="https://images.unsplash.com/photo-1735669356374-8ea7506cd1d2?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3Mzc2NjU0MDJ8&ixlib=rb-4.0.3&q=85" alt="キャストのサブ画像" />
      </button>
      <button class="change-img__item js-change-img-button">
        <img src="https://images.unsplash.com/photo-1737047938856-ea38cdb28dea?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3Mzc2NjU0MDJ8&ixlib=rb-4.0.3&q=85" alt="キャストのサブ画像" />
      </button>
        <button class="change-img__item js-change-img-button">
        <img src="https://images.unsplash.com/photo-1737047938856-ea38cdb28dea?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3Mzc2NjU0MDJ8&ixlib=rb-4.0.3&q=85" alt="キャストのサブ画像" />
      </button>
    </ul>
  </div>
</div>
</body>
              
            
!

CSS

              
                body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}

.change-img {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px;
  max-width: 300px;
  margin: 0 auto; 

}

.change-img__main {
  width: 100%;
  text-align: center;
}

.change-img__main img {
  aspect-ratio: 2/1;
  width: 100%;
  height: auto;
  border-radius: 8px;
  object-fit: cover;
}

.change-img__img-sub {
  width: 100%;
}

.change-img__list {
    width: 100%;
  display: flex;
  gap: 10px;
  list-style: none;
  padding: 0;
  margin: 0;
overflow-x: scroll;
}

.change-img__item {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  min-width: 80px;
}

.change-img__item img {
  aspect-ratio: 1;
  width: 100%;
  object-fit: cover;
  border-radius: 4px;
}



              
            
!

JS

              
                // メイン画像を取得
const $mainImage = $('.js-change-img-main img');
// サブ画像ボタンをすべて取得
const $buttons = $('.js-change-img-button');

// 各ボタンにクリックイベントを追加
$buttons.on('click', function() {
  // ボタン内の画像要素を取得
  const newImageSrc = $(this).find('img').attr('src');
  // メイン画像のsrcを更新
  $mainImage.attr('src', newImageSrc);
});
              
            
!
999px

Console