html - 使用 CSS 实现类似 TikTok 的滚动效果

标签 html css

我的网页上的视频显示在可滚动列表中,其中每个视频覆盖整个屏幕,如下所示:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Videos</title>
  <style>
    body {
      background-color: #090909;
    }

    .video-box {
      display: flex;
      align-items: center;
    }

    .video-box video {
      box-sizing: border-box;
      padding: 8px;
      margin: 0 auto;
      max-height: 100vh;
      max-width: 100%;
    }
  </style>
</head>

<body>
  <section class="videos">
    <div class="video-box">
      <video preload="none" poster="vert1.png" autoplay muted loop playsinline>
        <source src="vert1.mp4" type="video/mp4">
      </video>
    </div>
    <div class="video-box">
      <video preload="none" poster="vert2.png" autoplay muted loop playsinline>
        <source src="vert2.mp4" type="video/mp4">
      </video>
    </div>
    <div class="video-box">
      <video preload="none" poster="vert1.png" autoplay muted loop playsinline>
        <source src="vert1.mp4" type="video/mp4">
      </video>
    </div>
    <div class="video-box">
      <video preload="none" poster="vert2.png" autoplay muted loop playsinline>
        <source src="vert2.mp4" type="video/mp4">
      </video>
    </div>
  </section>
</body>

</html>

我的实现中缺少的部分是使视频快速播放,例如: TikTok 或 YouTube Shorts。我尝试使用 JavaScript 来实现它 fullPage.js但发现将脚本放入网页代码中的额外工作有问题。仅使用 HTML 和 CSS 可以实现此实现吗?

最佳答案

出于此特定目的page-snap-* CSS properties被创建。

要使示例中的页面对齐,只需添加两个额外的 CSS 属性:

  <style>
    html /* IMPORTANT: scroll-snap-type won't work with body selector, so replace it with html */ {
      background-color: #090909;
      scroll-snap-type: y mandatory; /* Define snapping behavior. "y" indicates vertical behavior, "mandatory" won't allow the user to stay in-between two pages. */
    }

    .video-box {
      display: flex;
      align-items: center;
      scroll-snap-align: start; /* Define snapping target. Can also be "center" or "end". */
    }

    .video-box video {
      box-sizing: border-box;
      padding: 8px;
      margin: 0 auto;
      max-height: 100vh;
      max-width: 100%;
    }
  </style>

关于html - 使用 CSS 实现类似 TikTok 的滚动效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75340067/

相关文章:

javascript - GeoChart map 太长无法加载

html &nbsp 在间距上有符号

c# - ASP.NET MVC 4 中的平面图设计

php - 在 jQuery 中收集复选框值并在提交时将其发布

html - 将两个列表相互 float ?

HTML 中的 Python : Iterating over several lists by changing the name of a variable

html - 如何转义单引号?

jquery - 响应式导航菜单 - jQuery slideToggle() 不向下移动内容

html - 我在代码中使用 Bootstrap 4 轮播。Bootstrap 4 轮播不工作

html - 绝对位置响应于绝对位置的宽度自动