You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CSS组合实现参考示例中的滑动遮罩与图片入场动画效果

问题根因

  • 缺失animation-fill-mode: forwards属性,动画执行结束后会默认回弹到初始帧,导致遮罩层没有真正滑出视口
  • 需要常驻显示的内容放在了会滑走的.overlay容器内,随着遮罩层一起被移出视口,自然无法留在页面上
  • z-index层级逻辑错误,需要常驻的内容层级低于遮罩层时才会在遮罩滑走后露出

修正后的完整代码

CSS代码

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* 常驻显示的底层内容 */
.fixed-content {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  padding-left: 10%;
  position: relative;
  z-index: 1;
  color: #000;
  font-size: 48px;
  font-weight: bold;
}

.overlay {
  width: 100%;
  height: 100vh;
  display: block;
  overflow: hidden;
  animation: move 1.9s cubic-bezier(0.6, 0, 1, 1) forwards;
  left: 0%;
  clip-path: polygon(0 0, 0% 0, 0% 100%, 0 100%);
  position: absolute;
  top: 0;
  z-index: 2;
}

.overlay:before {
  content: "";
  position: absolute;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.7);
  z-index: 1;
}

.overlay_image {
  width: 100%;
  height: 100%;
  display: block;
  position: relative;
  right: 15%;
  animation: move_image 3s cubic-bezier(0.6, 0, 1, 1) forwards;
  background-image: url("https://i.imgur.com/isvINjD.png");
  background-position: center;
  background-size: contain;
  background-repeat: no-repeat;
}

@keyframes move {
  60% {
    clip-path: polygon(0% 0, 100% 0, 100% 100%, 0% 100%);
  }
  100% {
    clip-path: polygon(100% 0%, 100% 0%, 100% 100%, 100% 100%);
  }
}

@keyframes move_image {
  100% {
    right: -15%;
  }
}

HTML代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="styles.css" />
    <title>Animation</title>
  </head>
  <body>
    <!-- 常驻显示的内容放在overlay外部,不跟随遮罩滑动 -->
    <div class="fixed-content">
      此处替换为你需要保留在左侧的内容
    </div>
    <div class="overlay">
      <div class="overlay_image"></div>
    </div>
  </body>
</html>

核心修改说明

  • 新增独立的.fixed-content容器放置需要常驻显示的内容,和滑动遮罩层完全隔离
  • 所有动画新增forwards参数,保证动画执行结束后停留在最后一帧,不会回弹
  • 调整z-index层级:常驻内容层级为1,遮罩层层级为2,遮罩滑走后内容自然露出
  • 移除了overlay多余的白色背景,避免遮罩滑动过程中挡住底层内容

内容的提问来源于stack exchange,提问作者Karol Landowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 23:36:00