我正在使用 Locomotive-Scroll ( https://locomotivemtl.github.io/locomotive-scroll/ ) 进行平滑和水平滚动,它很容易并且效果很好。但是,我也想集成一段垂直滚动。
我已经尝试了很多东西,但没有一个完全有效。我主要面临三个问题:
- 我不明白为什么我的解决方案在一定程度上有效,而且我找不到任何关于 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>