javascript - 机车滚动 - 从水平切换到垂直并返回

标签 javascript scroll horizontal-scrolling smooth-scrolling locomotive-scroll

我正在使用 Locomotive-Scroll ( https://locomotivemtl.github.io/locomotive-scroll/ ) 进行平滑和水平滚动,它很简单并且效果很好。不过,我还想集成一部分垂直滚动。

Visual Explanation

我尝试了很多方法,但没有一个完全有效。我主要面临三个问题:

  • 我不明白为什么我的解决方案在一定程度上有效,而且我找不到任何关于 locomotive-scroll 的教程,除了添加 data-scroll 属性之外
  • 我没有找到将垂直滚动方向从向下更改为向上的方法
  • 如果我调整窗口大小,机车滚动会刹车,只能通过重新加载来恢复

我当前的解决方案(我认为自己是初学者):

const scroller = new LocomotiveScroll({
    el: document.querySelector('#scrollable'),
    smooth: true,
    direction: 'horizontal'
});
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Helvetica Neue', sans-serif;
    font-size: 2vh;
}

.section {
    height: 100vh;

}

#section-one {
    width: 100vw;
}

#section-two {
    width: 400vh;
    background-color: blanchedalmond;
}

#section-three {
    width: 100vw;
}

.heading {
    font-size: 5rem;
    font-weight: 500;
    text-transform: uppercase;
}

.home__statement {
    width: 100vw;
    position: relative;
}

.heading.one {
    position: absolute;
    left: 10vw;
    top: 25vh;
}

.heading.two {
    position: absolute;
    left: 10vw;
    top: 35vh;
}

.heading.three {
    position: absolute;
    left: 10vw;
    top: 45vh;
}

#sticky-container{
    width: 100vw;
    height: 100vh;
    background-color: cadetblue;
}

.vertical-canvas {
    position: absolute;
    top: -300vh;
    height: 400vh;
    width: 100vw;
    background-image: linear-gradient(red, yellow);
}

.example-1 {
    height: 50%;
    width: 100vw;
    background-color:rgba(175, 18, 18, 0.3);;
}

.elem {
    width: 100px;
    height: 100px;
    background-color: cornsilk;
}

.example-2 {
    height: 25%;
    width: 100vw;
    background-color: rgba(46, 165, 46, 0.3);
}

.example-3 {
    height: 25%;
    width: 100vw;
    background-color: rgba(75, 92, 168, 0.3);
}
    <div id="scrollable" data-scroll-container>

        <section class="section" id="section-one" data-scroll-section>

            <div class="home__statement">
                <span class="heading one" id="statement__line-one">Some text blabla</span>
            <div>

        </section>
        
        <section class="section" id="section-two" data-scroll-section>

            <div id="sticky-container"
                data-scroll
                data-scroll-sticky
                data-scroll-target="#section-two"
                data-scroll-speed="-1">
                    <div class="vertical-canvas"
                        data-scroll
                        data-scroll-sticky
                        data-scroll-target="#section-two"
                        data-scroll-direction="vertical">
                        <div class="example-1" id="ex1">
                            <div class="elem"></div>
                        </div>
                        <div class="example-2"></div>
                        <div class="example-3"></div>
                    </div>
            <div>

        </section>

        <section class="section" id="section-three" data-scroll-section>

            <div class="home__statement">
                <span class="heading one" id="statement__line-one">Some text blabla</span>
            <div>

        </section>

最佳答案

您无法在同一个窗口中同时初始化水平滚动和垂直滚动 [数据滚动容器]

对于你想要实现的事情,你可以用gsap的scrollTrigger来做 但你必须implement scrollTrigger with locmotive

你可以做任何你想要的效果

关于javascript - 机车滚动 - 从水平切换到垂直并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68242974/

相关文章:

java - TabHost 出现水平滚动问题

android - 滚动的 TextView 值被截断

javascript - 将 RequireJS 与 Google map 结合使用

ios - UITableView 检测最后一个单元格

javascript - 如何使用 img 元素中的图像更改正文背景

android - 在 Android 中滚动时 ListView 项目发生变化

javascript - 使用 jQuery 同步滚动?

javascript - 单击链接时水平滚动

javascript - 无法设置下拉列表的默认值?

javascript - TinyMCE4 网址列表