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="container">
      <div class="counter">
          <span>1</span> / 
          <span>3</span>
      </div>
      <div class="slider-container">
        <div class="container-btns">
          <button class="btn-prev">
            <svg viewBox="0 0 256 512" width="100" title="angle-left">
  <path d="M31.7 239l136-136c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9L127.9 256l96.4 96.4c9.4 9.4 9.4 24.6 0 33.9L201.7 409c-9.4 9.4-24.6 9.4-33.9 0l-136-136c-9.5-9.4-9.5-24.6-.1-34z" />
</svg>
          </button>
          <button class="btn-next">
            <svg viewBox="0 0 256 512" width="100" title="angle-right">
  <path d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z" />
</svg>
          </button>
        </div>
        <div class="slide s1">
          <h2>タイトル1</h2>
          <hr />
          <p>
            スライト1
          </p>
        </div>
        <div class="slide s2">
          <h2>タイトル2</h2>
          <hr />
          <p>
スライト2
          </p>
        </div>
        <div class="slide s3">
          <h2>タイトル3</h2>
          <hr />
          <p>
      スライト3     </p>
        </div>
      </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.6.1/gsap.min.js"></script>
              
            
!

CSS

              
                *, ::before, ::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  background: #222;
  padding: 0 20px;
}

.container {
  position: relative;
  height: 500px;
  max-width: 900px;
  margin: 125px auto 0;
}

.slider-container {
  height: 100%;
  width: 100%;
  position: relative;
  overflow: hidden;
}
svg {
  fill: white;
  width: 25px;
  height: 25px;
}
.counter {
  position: absolute;
  z-index: 150;
  top: 0;
  right: 0;
  width: 75px;
  height: 75px;
  background: white;
  color: #222;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top-right-radius: 10px;
  border-bottom-left-radius: 10px;
}
.counter span:nth-child(1){
  font-size: 26px;
}
.counter span:nth-child(2){
  font-size: 16px;
}

.container-btns {
  position: absolute;
  bottom: -20px;
  z-index: 100;
  padding-left: 20px;
  padding-bottom: 50px;
}
.container-btns button {
  padding: 10px 12px;
  border-radius: 3px;
  border: 1px solid #fff;
  background: transparent;
  color: #fff;
  outline: none;
  cursor: pointer;
}
.container-btns button:active {
  transform: scale(1.05)
}
.container-btns button img {
  width: 20px;
}
.container-btns button:nth-child(1){
  margin-right: 5px;
}

.slide {
  height: 100%;
  width: 100%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: absolute;
  color: #fff;
  transform: translateX(-100%);
}

.s1 {
  transform: translateX(0%);
}
.slide h2 {
  font-size: 45px;
  padding-top: 300px;
  text-transform: uppercase;
  padding-left: 20px;
}
.slide hr {
  border: none;
  width: 200px;
  height: 1px;
  background: #fff;
  margin: 10px 0 10px 21px;
  display: block;
}
.slide p {
  font-size: 16px;
  padding-left: 20px;
}
.s1 {
  background-image: url(https://images.unsplash.com/photo-1626462222108-8c1fbe8527b1?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyODA0ODQ5MA&ixlib=rb-1.2.1&q=85);
}
.s2 {
  background-image: url(https://images.unsplash.com/photo-1626209614616-28d518b676e5?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyODA0ODQ5MA&ixlib=rb-1.2.1&q=85);
}
.s3 {
  background-image: url(https://images.unsplash.com/photo-1626982680798-ec99e2aef288?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYyODA0ODQ5MA&ixlib=rb-1.2.1&q=85);
}

              
            
!

JS

              
                const btnNext = document.querySelector('.btn-next');
const btnPrev = document.querySelector('.btn-prev');
const container = document.querySelector('.container');
// Array.from()で配列のメソッドが使えるようになる
const slides = Array.from(document.querySelectorAll('.slide'));
const indexIndication = document.querySelector('.counter span:nth-child(1)');
let index = 0;

function animNext() {
  const TLNEXT = gsap.timeline();

  TLNEXT
    // indexの番号を動的に変更する
    .set(indexIndication, {
      innerText: index + 1,
    })
    .to(slides[index], { duration: 1, x: 0 });
}

function animPrev() {
  const TLPREV = gsap.timeline();
  TLPREV.set(indexIndication, {
    // currentのindex番号を表示
    innerText: index,
  }).to(slides[index], { duration: 1, x: '-100%' });
}

function rejectSlide() {
  gsap.to(container, {
    keyframes: [
      { duration: 0.08, x: -10 },
      { duration: 0.08, x: 8 },
      { duration: 0.08, x: -8 },
      { duration: 0.08, x: 6 },
      { duration: 0.08, x: -6 },
      { duration: 0.08, x: 3 },
      { duration: 0.08, x: 0 },
    ],
  });
}

function handleDirection(direction) {
  if (direction === 'next') {
    // 最後のスライドなら
    if (index === slides.length - 1) {
      rejectSlide();
      return;
    }
    // すでにs1の一枚目(slides[0])が表示されているので、indexに1足してからanimNext()を実行する
    index++;
    animNext();
  } else if (direction === 'prev') {
    if (index === 0) {
      rejectSlide();
      return;
    }
    // まずは現在のindexのスライドを動かす必要があるので、アニメーション実行してからindexを変化させる
    animPrev();
    index--;
  }
}

btnNext.addEventListener('click', () => {
  handleDirection('next');
});

btnPrev.addEventListener('click', () => {
  handleDirection('prev');
});

              
            
!
999px

Console