首页 > 解决方案 > 附加多个 div 并使其响应

问题描述

大家好,

我正在使用 HTML 和 CSS 创建加载动画。由于我在响应式前端方面并不是很熟练,因此我真的很难使文本和圆圈响应式。

我真正想要的是将带有背景图像和文本的 div 附加到栏上并使它们响应,以便不移动并保持在同一位置。

这就是我想要实现的目标: 正在加载

这是我目前拥有的代码。我已经尝试过修复附件和类似的东西,但主要问题是当我使用最大高度/宽度时图像会不断缩放,并且文本会根据网站的宽度向右移动。

希望你能帮助我,谢谢指教。

body {
  background: #111 url("");
  background-size: 25vmin;
  background-repeat: no-repeat;
  background-position: center 40%;
  height: 100vh;
  margin: 0;
}

.logo {
  background: url("https://openclipart.org/download/256338/whitecircle.svg");
  background-size: 25vmin;
  background-repeat: no-repeat;
  position: absolute;
  left: 28%;
  bottom: 10vh;
  height: 25vh;
  width: 100px;
  max-width: 150px;
}

h1 {
  color: #fff;
  position: absolute;
  bottom: 20vh;
  left: 35%;
}

.progress {
  width: 400px;
  max-width: 85vw;
  height: 8px;
  position: absolute;
  bottom: 20vh;
  left: 50%;
  background: rgba(255, 255, 255, 0.5);
  transform: translate(-50%, -50%);
  overflow: hidden;
}

.progress:after {
  content: '';
  display: block;
  width: 100%;
  height: 8px;
  background: #fff;
  animation: load 5s linear;
}

@-moz-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-webkit-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-o-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
<div class="logo"> </div>
<h1 class="title"> Loading </h1>
<div class="progress"> </div>

标签: htmlcssresponsive

解决方案


我通常做的是让一个项目响应并且许多部分需要密切合作,创建一个包含所有相关项目的容器。然后在容器中,我使用 % 对齐项目,以便它们很好地缩放。主容器(在我的示例中称为loader)我使用 vh 和 vw 单位使用宽度和高度。

这是解决此问题的一种方法。我还用使用 css 制作的圆圈替换了 SVG。这样您就不需要加载图像。这将使您的页面减少资源负担。如果您特别想使用 SVG,请告诉我,我可以更新示例。

注意:我为loaderdiv 添加了一个浅色边框,以便您在调整窗口大小时可以看到它是如何调整大小的。将其复制到页面时将其删除。

body {
  height: 100vh;
  margin: 0;
  background-color: black;
  display: flex;
  justify-content: center;
  align-items: center;
}

.loader {
  position: relative;
  height: 30vh;
  width: 50vw;
  min-width: 200px;
  min-height: 100px;
  border: 1px solid #444; // added to see the responsiveness
}

.circle {
  width: 55px;
  height: 55px;
  bottom: calc(40% - 27.5px);
  left: -2px;
  position: absolute;
  border: 5px solid white;
  border-radius: 50%;
}

h1 {
  color: #fff;
  position: absolute;
  left: 75px;
  bottom: 32%;
}

.progress {
  position: absolute;
  width: calc(100% - 60px);
  height: 8px;
  bottom: 40%;
  left: 60px;
  background: rgba(255, 255, 255, 0.5);
  overflow: hidden;
}

.progress:after {
  content: '';
  display: block;
  width: 100%;
  height: 8px;
  background: #fff;
  animation: load 5s linear;
}

@-moz-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-webkit-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@-o-keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}

@keyframes load {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
<div class="loader">
  <!--<div class="logo"> </div>-->
  <!--<img class="img-logo" src="https://openclipart.org/download/256338/whitecircle.svg">-->
  <div class="circle"></div>
  <h1 class="title">Loading</h1>
  <div class="progress"></div>
</div>


推荐阅读