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

如何在动画面板代码中替换aspect-ratio为padding-top:56.25%?

解决方案

要将原代码中的aspect-ratio: 1替换为padding-top: 56.25%实现16:9宽高比,需要调整元素尺寸控制逻辑(padding-top百分比基于父元素宽度计算,与aspect-ratio的直接宽高比控制逻辑不同),具体修改如下:

1. 替换核心宽高比规则

找到原CSS中控制元素比例的规则:

.inner span,
.inner span:before,
.inner span:after,
.inner:before,
.inner:after {
  aspect-ratio: 1;
  margin: auto;
  height: 100%;
}

将其拆分为两组规则:

  • 针对.inner span、.inner:before、.inner:after(基础容器):
    .inner span,
    .inner:before,
    .inner:after {
      width: 80%; /* 可按需调整,此处设为父容器宽度80%以保持居中视觉效果 */
      padding-top: 56.25%; /* 16:9比例计算值:9/16×100% */
      margin: auto;
      height: 0; /* 重置高度,避免默认高度干扰padding-top的比例计算 */
      position: relative; /* 为内部绝对定位伪元素提供定位基准 */
    }
    
  • 针对.inner span:before、.inner span:after(span内部的绝对定位伪元素):
    .inner span:before,
    .inner span:after {
      height: 100%; /* 填满父容器高度 */
      width: 100%; /* 填满父容器宽度 */
    }
    

2. 完整修改后CSS代码

:root {
  --color-a: #1155cc;
  --color-b: black;
  --color-c: #1155cc;
  --color-d: black;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: #121212;
  padding: 50px 8px;
}

.panel-left,
.panel-right {
  position: fixed;
  height: 100%;
  width: 50%;
  top: 0%;
  overflow: hidden;
  z-index: 0;
  transform: translateX(0);
  transition: transform 3s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition-delay: 1s;
}

.panel-left {
  left: 0;
}

.panel-right {
  right: 0;
}

.panel-left::before,
.panel-right::before {
  content: "";
  position: fixed;
  height: 100%;
  width: 200%;
  top: 0;
  left: 0;
  background: black;
}

.panel-right::before {
  left: -100%;
}

body:hover .panel-left {
  transform: translateX(-100%);
}

body:hover .panel-right {
  transform: translateX(100%);
}
.inner {
  height: 100%;
  width: 200%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin: auto;
}

.panel-right .inner {
  left: -100%;
}

.inner span,
.inner:before,
.inner:after {
  width: 80%;
  padding-top: 56.25%;
  margin: auto;
  height: 0;
  position: relative;
}

.inner:before {
  content: "";
  background: url(https://i.imgur.com/z5MMJnv.png);
}

.inner:after {
  content: "";
  background: url(https://i.imgur.com/8Jf8LLc.png);
}

.inner span {
  background: url(https://i.imgur.com/5u16syR.png);
  position: relative;
}

.inner span:before,
.inner span:after {
  content: "";
  display: block;
  position: absolute;
  inset: 0 0 0 0;
  height: 100%;
  width: 100%;
}

.inner span:before {
  background: url(https://i.imgur.com/ygTtvme.png);
  transform: translateX(-99.99%);
}

.inner span:after {
  background: url(https://i.imgur.com/QziKNDW.png);
  transform: translateX(99.99%);
}

.inner span,
.inner span:before,
.inner span:after,
.inner:before,
.inner:after {
  background-size: cover;
}

关键说明

  • padding-top: 56.25%是16:9宽高比的标准计算值,依赖元素自身宽度生效,因此需要重置height: 0避免默认高度干扰。
  • 内部绝对定位伪元素通过width:100%+height:100%填满父容器,保证背景图正常显示。
  • 可自行调整width数值(如70%、90%)控制元素整体大小,不影响宽高比。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 22:30:32