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>
    <main>
      <!-- SLIDER -->
      <section class="description">
        <div class="container">
          <h2>途中から横スクロール</h2>
          <p>横スクロール中はパララックス</p>
        </div>
      </section>
      <div class="vertical-slider__wrap">
        <div class="proxy"></div>
        <section class="vertical-area teal">
          <div class="img-wrap">
            <div class="mask"></div>
            <img src="https://ryo-sukeblog.net/wp-content/uploads/2022/04/sea.jpg" alt="" class="js-img">
            <div class="heading-wrap"><h2>Beautiful World</h2></div>
          </div>
        </section>
        <section class="vertical-area maroon">
          <div class="img-wrap">
            <div class="mask"></div>
            <img src="https://ryo-sukeblog.net/wp-content/uploads/2022/04/cherry-blossoms.jpg" alt="" class="js-img img2">
            <div class="heading-wrap"><h2>Sakura Nagashi</h2></div>
          </div>
        </section>
        <section class="vertical-area olive">
          <div class="img-wrap">
            <div class="mask"></div>
            <img src="https://ryo-sukeblog.net/wp-content/uploads/2022/04/scene.jpg" alt="" class="js-img img3">
            <div class="heading-wrap"><h2>One Last Kiss</h2></div>
          </div>
        </section>
      </div>
      <section class="last-area">
        <h2>横スクロール終了</h2>
      </section>
      <!-- END SLIDER -->
    </main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/ScrollTrigger.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/Draggable.min.js"></script>
</body>
              
            
!

CSS

              
                *{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
html{
    width: 100%;
}
body{
    width: 100%;
}
main{
    width: 100%;
    overflow: hidden;
}
.description{
    width: 100%;
    height: 85vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #222;
}
.container{
    width: fit-content;
    color: white;
}
.container h2{
    font-size: 40px;
}
.container p{
    width: 100%;
    text-align: center;
}
.vertical-slider__wrap{
    width: fit-content;
    height: 100vh;
    display: flex;
    flex-wrap: nowrap;
}
.vertical-area{
    width: 100vw;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}
.heading-wrap{
    width: 100%;
    overflow: hidden;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
}
.vertical-area h2{
    color: white;
    font-size: 56px;
    width: 100%;
    display: flex;
    justify-content: center;
}
.vertical-area h2 span{
    display: block;
}
.vertical-area .img-wrap{
    width: 70%;
    height: 80%;
    position: relative;
    overflow: hidden;
}
.vertical-area .img-wrap .mask{
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background-color: rgba(0,0,0,.1);
    z-index: 1;
}
.vertical-area .img-wrap img{
    width: 130%;
    height: 130%;
    object-fit: cover;
}
.teal{
    background-color:teal;
}
.maroon{
    background-color: maroon;
}
.olive{
    background-color: olive;
}
.last-area{
    background-color: #222;
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
.last-area h2{
    color: white;
    font-size: 40px;
}
.proxy{
    position: absolute;
    visibility: hidden;
}
              
            
!

JS

              
                let sections = document.querySelectorAll(".vertical-area");
let scrollContainer = document.querySelector(".vertical-slider__wrap");
let images = gsap.utils.toArray(".js-img");
let heading = gsap.utils.toArray(".heading-wrap h2");

let scrollTween = gsap.to(sections, {
    xPercent: -100 * (sections.length - 1),
    ease: "none"
});

let horizontalScroll = ScrollTrigger.create({
    animation: scrollTween,
    trigger: scrollContainer,
    pin: true,
    scrub: 1,
    // markers:true,
    end: () => "+=" + scrollContainer.offsetWidth,
});

function txtSplit(el){//https://sinciate.co.jp/media/2999/
    var content = el.textContent;
    var text = content.trim();
    var newHtml = "";

    text.split("").forEach(function(v) {
        newHtml += "<span>" + v + "</span>";
    });
    el.innerHTML = newHtml;
}
images.forEach((img, i) => {
    gsap.to(img,{
        scrollTrigger:{
            trigger: img,
            start:'top center',
            end:'bottom center',
            horizontal:true,
            // markers:true,
            scrub:.2,
            containerAnimation:scrollTween,
        },
        x:-150,ease:"none"
    });
});
heading.forEach((h, i) => {
    txtSplit(h);
    if(i === 0){
        gsap.from(h.querySelectorAll('span'),{
            scrollTrigger:{
                trigger: h,
                start:'top 10%',
                // markers:true,
                horizontal:true,
                containerAnimation:scrollTween,
                toggleActions:'play none none reverse'
            },
            stagger:{
                each:0.02,
            },
            y:'30%',autoAlpha:0,ease:Power4.easeIn,duration:.4,
        });
    }else{
        gsap.from(h.querySelectorAll('span'),{
            scrollTrigger:{
                trigger: h,
                start:'10% center',
                // markers:true,
                horizontal:true,
                containerAnimation:scrollTween,
                toggleActions:'play none none reverse'
            },
            stagger:{
                each:0.02,
            },
            y:'50%',autoAlpha:0,ease:Power4.easeIn,duration:.4,
        });
    }
});

// total scroll amount divided by the total distance that the sections move gives us the ratio we can apply to the pointer movement so that it fits. 
var dragRatio = scrollContainer.offsetWidth / (window.innerWidth * (sections.length - 1));
var drag = Draggable.create(".proxy", {
  trigger: scrollContainer,
  type: "x",
  onPress() {
    this.startScroll = horizontalScroll.scroll();
  },
  onDrag() {
    horizontalScroll.scroll(this.startScroll - (this.x - this.startX) * dragRatio);
  }
})[0];
              
            
!
999px

Console