标题重写为:如何实现底部粘贴功能的滚动效果?
P粉587780103
P粉587780103 2023-09-09 19:41:49
[CSS3讨论组]

我正在尝试实现一个类似我在这里看到的效果

但是每个部分的内容也应该是可滚动的,即只有在达到部分底部时才会固定。

例如,当内容为200vh时,像这样:

section {
  position: sticky;
  top: 0;
  height: 200vh;
}

section img{
  width:100%;
  height:100%
}


section:nth-of-type(1) {
  background: rgb(225, 204, 200);
}

section:nth-of-type(2) {
  background: rgb(240, 216, 163);
}

section:nth-of-type(3) {
  background: rgb(192, 211, 217);
}
<section>
    <img src="https://picsum.photos/id/128/800/300" alt="">
</section>

<section>
    <img src="https://picsum.photos/id/48/800/300"alt="">
</section>

<section>
    <img src="https://picsum.photos/id/42/800/300" alt="">
</section>

正如你所看到的,第1和第2部分固定在顶部,我们无法滚动查看它们的照片。

但是最后一部分完美地工作。

那么我该如何实现这个效果?理想情况下使用纯CSS,但我也接受JavaScript解决方案。

P粉587780103
P粉587780103

全部回复(1)
P粉141925181

我找到了一个解决方案!使用JavaScript中的一行代码来设置top

document.querySelectorAll(".item").forEach((el)=>{ el.style.top=(document.documentElement.clientHeight - el.offsetHeight)+"px" });
section{
  position:sticky;
  width:100%;
  height:200vh;
}

img{
  object-fit:cover;
  height:100%;
  width:100%;
}
<section class="item">
 <img src="https://picsum.photos/id/128/800">
</section>

<section class="item">
  <img src="https://picsum.photos/id/48/800">
</section>

<section class="item">
  <img src="https://picsum.photos/id/42/800">
</section>

虽然我不确定如果窗口被调整大小是否会起作用
但你可以始终使用onresize来处理这种情况

如果有的话,我仍然可以接受纯CSS解决方案?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号