javascript - 带有机车滚动的水平滚动粘性部分
问题描述
希望使用 Locomotive 滚动框架 ( https://github.com/locomotivemtl/locomotive-scroll )在具有以下结构的粘性 div 内创建水平滚动部分。我的HTML如下:
<div id="sec01" class="the-height-400vh-section"><!-- this has the same height as does the 400vh width, timing should match -->
<div class="the-sticky-div" id="sticky" data-scroll data-scroll-sticky data-scroll-target="#sec01"><!-- this is stickying to viewport while we scroll -->
<div class="the-overflow-hidden-mask">
<div class="the-width-400vh-scrollable-div" data-scroll data-scroll-direction="horizontal" data-scroll-speed="12" data-scroll-target="#sec01"><!-- we're scrolling this 400vh to the right while we're scrolling the 400vh height of the parent -->
<div class="the-content">
<div class="a-block"></div>
<div class="a-block"></div>
<div class="a-block"></div>
<div class="a-block"></div>
</div>
</div>
</div>
</div>
</div>
并应用了以下 CSS:
.the-sticky-div {
position: absolute;
top: 0;
height: 100vh;
width: 100%;
overflow-x: hidden;
background: #ccc;
z-index: 1;
}
.the-overflow-hidden-mask {
position: relative;
z-index: 200;
overflow: hidden;
width: 100%;
height: 100%;
background: #000;
}
.the-height-400vh-section {
position: relative;
display: flex;
height: 400vh;
margin-left: 0px;
justify-content: center;
align-items: center;
border-top: 60px none rgba(36, 36, 36, 0.09);
background-color: #fff;
}
.the-width-400vh-scrollable-div {
display: flex;
width: 400vh;
position:relative;
height: 100%;
flex-wrap: wrap;
align-items: center;
will-change: transform;
}
.the-content {
display: flex;
width: 100%;
height: 100%;
padding-bottom: 10vh;
justify-content: flex-start;
align-items: flex-end;
.a-block {
overflow: hidden;
width: 100vw;
height: 100vh;
left:0;
margin-right: 40px;
margin-left: 40px;
flex: 0 0 auto;
border-radius: 6px;
background-color: hsla(0, 0%, 87.1%, 0.72);
box-shadow: 0 0 100px 8px rgba(205, 43, 177, 0.25);
}
}
这背后的逻辑应该是这样的:
但我有两个主要问题:
水平滚动初始化太早
水平内容从右侧滑入并且不在开始滚动的初始位置。
任何想法可以用当前的解决方案来实现与 www.reformcollective.com 类似的最终结果(部分进入屏幕,滚动开始,在查看最后一个部分时滚动结束)。
提前致谢
解决方案
你不需要 Locomotive Scroll 只需20 行 vanilla JS。
您需要三个容器,我们可以将其称为:
- space holder - 此容器将保持垂直空间滚动空间。更多关于这下面。
- 粘性 - 这个容器将有一个设定的高度(
100vh
在我的例子中)。它也将是position: sticky; top: 0;
。 - 水平 - 此容器将包含您希望水平滚动的内容。它的宽度将由它包含的内容决定。
为了达到预期的效果,您必须将“space holder”的高度设置为“horizontal”的宽度。然后在滚动时,您必须水平平移“水平”等于“粘性”的偏移顶部。
在此处查看我对类似问题的回答。
推荐阅读
- printing - 如何在颤动的控制台中显示生成的消息?
- javascript - 5 个 DIV 框 - 使用 querySelector 和 addEventListener 填充颜色
- python - 使用元类设置时 Django 迁移忽略 db_table
- windows - Windows 10上的GHCi在打开后立即退出
- python - 在双端队列中交换 2 个节点,python
- php - 在 Woocommerce 产品类别存档页面中获取当前术语名称
- android - Android Studio 中显示的版本的设计实现应用程序 Gradle 中存在一些错误
- r - 按特定方面订购 geom_point
- python - 合并多个数据框熊猫
- javascript - Console.log 输出错误结果 [奇怪的行为]