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

              
                <div class="main">
  <div class="carousel05">
    <ul class="carousel05__list">
      <li class="carousel05__item">
        <figure class="carousel05__img-wrapper">
          <img class="carousel05__img" id="js-carousel05Img-01" src="https://www.igmocho.com/sample/img/img_sample_640x480_01.jpg" alt="">
        </figure>
      </li><!--/.carousel05__item-->
      <li class="carousel05__item">
        <figure class="carousel05__img-wrapper">
          <img class="carousel05__img" id="js-carousel05Img-02" src="https://www.igmocho.com/sample/img/img_sample_640x480_02.jpg" alt="">
        </figure>
      </li><!--/.carousel05__item-->
      <li class="carousel05__item">
        <figure class="carousel05__img-wrapper">
          <img class="carousel05__img" id="js-carousel05Img-03" src="https://www.igmocho.com/sample/img/img_sample_640x480_03.jpg" alt="">
        </figure>
      </li><!--/.carousel05__item-->
      <li class="carousel05__item">
        <figure class="carousel05__img-wrapper">
          <img class="carousel05__img" id="js-carousel05Img-04" src="https://www.igmocho.com/sample/img/img_sample_640x480_04.jpg" alt="">
        </figure>
      </li><!--/.carousel05__item-->
      <li class="carousel05__item">
        <figure class="carousel05__img-wrapper">
          <img class="carousel05__img" id="js-carousel05Img-05" src="https://www.igmocho.com/sample/img/img_sample_640x480_05.jpg" alt="">
        </figure>
      </li><!--/.carousel05__item-->
    </ul><!--/.carousel05__list-->

    <div class="carousel05__button-wrapper">
      <div class="carousel05__button" id="js-carousel05BtnPrev">&lt;</div>
      <div class="carousel05__page"  id="js-carousel05Page"></div>
      <div class="carousel05__button"  id="js-carousel05BtnNext">&gt;</div>
    </div><!--/.carousel__button-->
      <script src="js/md_carousel.js"></script>
  </div><!--/.carousel05-->

              
            
!

CSS

              
                *{box-sizing:border-box;}

figure{
  width:100%;
  margin:0;
  padding:0;
}
img{width:100%;}
ul{
  margin:0;
  padding:0;
}
li{
  margin:0;
  padding:0;
  list-style:none;
}
.main{
  width:655px;
  margin:auto;
  overflow:hidden;
  
  
}
/*carousel05*/
.carousel05{
  width:100%;
  margin:auto;
}
.carousel05__list{
  display:flex;
  transition: transform 0.3s ease-in-out;
}
.carousel05__item{
  flex:0 0 20%;
  padding:10px;
  transition: transform 0.3s ease-in-out;
}

.carousel05__button-wrapper{
  display:flex;
  justify-content:center;
  margin:auto;
  padding-top:1em;
  padding-bottom:1em;
}
.carousel05__button{
  cursor:pointer;
}
.carousel05__page{
  margin-left:1em;
  margin-right:1em;
}
              
            
!

JS

              
                const crsl05List = document.querySelector('.carousel05__list');
const crsl05Item = document.querySelectorAll('.carousel05__item');
const crsl05ItemLength = crsl05Item.length;
const preView = 5;
let totalScrol = 0;
let current = 0;
const crsl05ItemFirst = crsl05Item[0];
const crsl05ItemLast = crsl05Item[crsl05Item.length - 1];
const copyCrsl05ItemFirst = crsl05ItemFirst.cloneNode(true);
const copyCrsl05ItemLast = crsl05ItemLast.cloneNode(true);
// ループ用の複製
crsl05List.insertBefore(copyCrsl05ItemLast, crsl05ItemFirst);
for (const crsl05ItemAll of crsl05Item){
  console.log(crsl05ItemAll);
  const copyCrsl05ItemAll = crsl05ItemAll.cloneNode(true);
  crsl05List.insertBefore(copyCrsl05ItemAll, crsl05ItemFirst);
}

let isAnimating = false; // アニメーション中かどうかを追跡

const btnPrev = document.getElementById('js-carousel05BtnPrev');
const page = document.getElementById('js-carousel05Page');
const btnNext = document.getElementById('js-carousel05BtnNext');
const crsl05ItemWidth = crsl05Item[0].offsetWidth;

function updatePageNumber(currentPage, totalItems) {
  page.textContent = `${currentPage + 1} / ${totalItems}`;
}

// 初期化
function updateCrsl05(){
  const crsl05ItemWidth = crsl05Item[0].offsetWidth;
  current++;
  crsl05List.style.transition = 'none';
  crsl05List.style.transform = `translateX(-${crsl05ItemWidth}px)`;
}
updateCrsl05();


function slideCrsl05(offset) {
  if (isAnimating) {
    return; // アニメーション中は何もしない
  }
  isAnimating = true;

  const translateX = -offset * crsl05ItemWidth;
  crsl05List.style.transition = 'transform 0.5s ease-in-out';
  crsl05List.style.transform = `translateX(${translateX}px)`;

  // 0.5秒後にアニメーション完了を検出する
  setTimeout(() => {
    isAnimating = false;
    crsl05List.style.transition = 'none';

    // 最初のアイテムのコピーに到達した場合、実際の最初のアイテムにジャンプする
    if (current === crsl05Item.length +1) {
      current = 0;
      updateCrsl05();
    } else if (current === 0) {
      // 最後のアイテムのコピーに到達した場合、実際の最後のアイテムにジャンプする
      current = crsl05Item.length;
      const finalTranslateX = -(crsl05Item.length ) * crsl05ItemWidth;
      crsl05List.style.transform = `translateX(${finalTranslateX}px)`;
    }

    updatePageNumber(current, crsl05Item.length);
  }, 500);
}


btnPrev.addEventListener('click', () => {
  current--;
  slideCrsl05(current);
});
btnNext.addEventListener('click', () => {
  current++;
  slideCrsl05(current);
});
updatePageNumber(current, crsl05Item.length);

              
            
!
999px

Console