实现滚动时向上滑动的文章内容效果

花韻仙語
发布: 2025-09-10 12:04:13
原创
262人浏览过

实现滚动时向上滑动的文章内容效果

本文介绍如何使用CSS实现一个常见的网页交互效果:当用户向下滚动页面时,文章主体内容区域向上滑动,从而实现一种动态的视觉体验。无需JavaScript,仅通过CSS的background和overflow-y属性即可轻松实现此效果。这种方法简单高效,适用于各种类型的文章页面。

使用CSS实现滚动上升效果

这种方法的核心在于利用background属性的fixed定位,以及overflow-y属性控制滚动条的行为。通过将背景图片固定,并设置内容区域的margin-top,可以模拟出内容向上滑动的效果。

HTML结构

首先,我们需要一个包含背景图片和文章内容的main容器。文章内容放在一个articles容器中,每个article-container代表一篇文章。

<main>
  <div class="articles">
    <div class="article-container" id="article-container">
      <h1>Article Title</h1>
      <div class="date">21/6/2022</div>
      <div class="article-description">Article Description</div>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a typespecimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,and more recently withdesktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy textever since the 1500s, when anunknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. Itwas popularised in the 1960s with therelease of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy textof the printing and typesetting industry. LoremIpsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not onlyfive centuries, but also the leap into electronic typesetting,remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishingsoftware like Aldus PageMaker including versions of Lorem Ipsum.</p>
    </div>
    <div class="article-container" id="article-container">
      <h1>Article Title</h1>
      <div class="date">21/6/2022</div>
      <div class="article-description">Article Description</div>
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a typespecimen book.It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,</p>
    </div>
  </div>
</main>
登录后复制

CSS样式

接下来,我们需要设置CSS样式来实现所需的效果。

火龙果写作
火龙果写作

用火龙果,轻松写作,通过校对、改写、扩展等功能实现高质量内容生产。

火龙果写作 106
查看详情 火龙果写作
body {
  margin: 0;
  overflow: hidden; /* 隐藏body的滚动条 */
}

main {
  display: flex;
  flex-direction: column;
  /* 设置背景图片 */
  background: url("https://source.unsplash.com/iuyR_HEwk24/1600x900") no-repeat center fixed;
  background-size: cover; /* 确保背景图片覆盖整个容器 */
  height: 100vh; /* 设置容器高度为视口高度 */
  overflow-y: overlay; /* 允许垂直方向滚动,并使用overlay滚动条样式 */
}

.articles {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  margin-top: 50vh; /* 将文章内容向下偏移50%的视口高度 */
}

.article-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 15px;
  width: 45vw;
  padding: 30px;
  color: #1a2434;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
  border-radius: 15px;
}

.article-container:last-child {
  margin-bottom: 30px; /* 为最后一个文章容器添加底部外边距 */
}
登录后复制

代码解释:

  • body { overflow: hidden; }: 隐藏body的滚动条,防止出现双滚动条。
  • main { ... background: url(...) no-repeat center fixed; ... height: 100vh; overflow-y: overlay; }: background: url(...) no-repeat center fixed; 设置背景图片并固定。 height: 100vh; 确保背景覆盖整个视口。 overflow-y: overlay; 允许垂直滚动,并且滚动条覆盖在内容之上,不会占据空间。
  • .articles { margin-top: 50vh; }: 将文章容器向下偏移50%的视口高度,使其初始位置在页面中间。

注意事项

  • 确保背景图片的URL正确,并且图片尺寸足够大,以适应不同的屏幕尺寸。
  • 可以根据需要调整margin-top的值,以改变文章内容初始位置。
  • overflow-y: overlay 可能会在某些浏览器上显示不同的滚动条样式。如果需要统一的滚动条样式,可以使用CSS自定义滚动条样式。

总结

通过以上步骤,我们成功地使用CSS实现了滚动时向上滑动的文章内容效果。这种方法简单易懂,无需JavaScript,可以快速应用于各种类型的文章页面,提升用户体验。 这种方法的核心在于巧妙地利用CSS的background和overflow-y属性,避免了使用JavaScript带来的复杂性。

以上就是实现滚动时向上滑动的文章内容效果的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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