html - 如何垂直对齐div中的动画文本?

标签 html vertical-alignment

我的目标是能够在 div 元素中垂直对齐动画文本。我曾尝试在这里寻找答案,但似乎找不到任何答案。具体来说,我尝试过 display:table-cell、vertical-align:middle 和 line-height:__,但它们都给出相同的结果。

let elemIds = ['a', 'b', 'c', 'd', 'e']
let currentAnimId = 0

function anims() {
  let elem = document.getElementById(elemIds[currentAnimId])
  let bgimg = document.getElementById('backgroundImg')
  currentAnimId += 1
  bgimg.style.animation="bgblur 5s";
  elem.style.display = 'block'
  elem.style.animation="textAnim 5s";
  window.setTimeout(() => {
    bgimg.style.animation="none";
    elem.style.display = 'none'
    elem.style.animation="none";
    elem.style.webkitAnimation = 'none';
    window.setTimeout(function() {
      elem.style.webkitAnimation = '';
      if (currentAnimId < elemIds.length) {
        anims(currentAnimId)
      } else {
        console.log("You have reached the end of the text cycle.")
      }
    }, 1000);
  }, 5000)
}

anims(currentAnimId)
body {
  margin: 0px;
  padding: 0px;
  font-family: 'Trebuchet MS', sans-serif;
}

#backgroundImg:empty {
  display: block;
  position: relative;
}

h1 {
  position: absolute;
  vertical-align: middle;
}

#backgroundImg {
  margin: 0px;
  padding: 0px;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  background-image: url("https://images.unsplash.com/photo-1492305175278-3b3afaa2f31f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxjb2xsZWN0aW9uLXBhZ2V8MXwxNjk1Mzk5fHxlbnwwfHx8fA%3D%3D&w=1000&q=80");
  /* animation: blurry 5s; */
  -webkit-filter: blur(0px);
  background-repeat: no-repeat;
  background-size: 100% auto;
  top: 0;
  left: 0;
}

#backgroundImg, #textDiv {
  width: 100%;
  height: 100%;
  position: absolute;
}

#textDiv {
  left: 100px;
  height: 80%;
  width: 80%;
  color: transparent;
  -webkit-text-stroke: 1px white;
  text-shadow: 10px 10px 20px black;
  z-index: 10;
  vertical-align: middle;
  text-align: left;
}

#a, #b, #c, #d, #e {
  font-size: 800%;
  display: none;
}

@keyframes bgblur {
  0% {
    -webkit-filter: blur(0px);
  }
  25% {
    -webkit-filter: blur(5px);
  }
  75%
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="backgroundImg">
    </div>
    <div id="textDiv">
      <h1 id="a">a</h1>
      <h1 id="b">b</h1>
      <h1 id="c">c</h1>
      <h1 id="d">d</h1>
      <h1 id="e">e</h1>
    </div>
    <script src="script.js"></script>
  </body>
</html>

如果有人能提供帮助那就太好了!

PS:我只想让它垂直居中,而不是水平居中。 谢谢!

最佳答案

添加显示:flex; align-items: center; 到您的 textDiv。您的 textDiv 高度为 80%,如果您想在所有屏幕上垂直居中,则应将其设为 100%

let elemIds = ['a', 'b', 'c', 'd', 'e']
let currentAnimId = 0

function anims() {
  let elem = document.getElementById(elemIds[currentAnimId])
  let bgimg = document.getElementById('backgroundImg')
  currentAnimId += 1
  bgimg.style.animation="bgblur 5s";
  elem.style.display = 'block'
  elem.style.animation="textAnim 5s";
  window.setTimeout(() => {
    bgimg.style.animation="none";
    elem.style.display = 'none'
    elem.style.animation="none";
    elem.style.webkitAnimation = 'none';
    window.setTimeout(function() {
      elem.style.webkitAnimation = '';
      if (currentAnimId < elemIds.length) {
        anims(currentAnimId)
      } else {
        console.log("You have reached the end of the text cycle.")
      }
    }, 1000);
  }, 5000)
}

anims(currentAnimId)
body {
  margin: 0px;
  padding: 0px;
  font-family: 'Trebuchet MS', sans-serif;
}

#backgroundImg:empty {
  display: block;
  position: relative;
}

h1 {
  position: absolute;
  vertical-align: middle;
}

#backgroundImg {
  margin: 0px;
  padding: 0px;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  background-image: url("https://images.unsplash.com/photo-1492305175278-3b3afaa2f31f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxjb2xsZWN0aW9uLXBhZ2V8MXwxNjk1Mzk5fHxlbnwwfHx8fA%3D%3D&w=1000&q=80");
  /* animation: blurry 5s; */
  -webkit-filter: blur(0px);
  background-repeat: no-repeat;
  background-size: 100% auto;
  top: 0;
  left: 0;
}

#backgroundImg, #textDiv {
  width: 100%;
  height: 100%;
  position: absolute;
}

#textDiv {
  left: 100px;
  height: 80%;
  width: 80%;
  color: transparent;
  -webkit-text-stroke: 1px white;
  text-shadow: 10px 10px 20px black;
  z-index: 10;
  display: flex;
  align-items: center;
  text-align: left;
}

#a, #b, #c, #d, #e {
  font-size: 800%;
  display: none;
}

@keyframes bgblur {
  0% {
    -webkit-filter: blur(0px);
  }
  25% {
    -webkit-filter: blur(5px);
  }
  75%
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Test</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <div id="backgroundImg">
    </div>
    <div id="textDiv">
      <h1 id="a">a</h1>
      <h1 id="b">b</h1>
      <h1 id="c">c</h1>
      <h1 id="d">d</h1>
      <h1 id="e">e</h1>
    </div>
    <script src="script.js"></script>
  </body>
</html>

关于html - 如何垂直对齐div中的动画文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70370509/

相关文章:

javascript - 放大的弹出窗口仅显示第一项

javascript - 切换样式表闪烁

html - flexbox 中的中心垂直对齐

css - 另一个 div 中的一个 div 的垂直对齐方式,具有 %-height

html - 如何将元素与垂直线对齐

html - 如何着色未读链接phpBB

html - 使 div 宽度拉伸(stretch)以适合其内容

php - 直接从 .js 或通过 .php 加载 javascript 源代码

css - 在表格中垂直堆叠两个字符,它们之间没有空格

html - <table> 上的垂直对齐稍微偏离中心......知道为什么吗?