CSS-Div不跟随动画父元素

时间:2018-07-04 22:18:09

标签: html css

我正在尝试让一段文字覆盖动画(关键帧)图像。

到目前为止,我已经尝试了显示属性的组合,例如没有障碍的内联块。我还尝试过使用这些位置,将标题和图像设置为相对和绝对。

我希望标题文本(h4)跟随其后的动画图像。

html {
  overflow: hidden;
}

body {
  margin: 0;
  overflow: hidden;
}

canvas {
  display: block;
  vertical-align: bottom;
}

h4 {
  position: absolute;
  background-color: red;
  width: 128px;
  display: inline-block;
  color: white;
  font-size: 24px;
}

.transactionBG {
  position: absolute;
  left: 0;
  overflow: hidden;
}

.transaction {
  width: 64px;
  height: 64px;
  position: absolute;
  animation: upDown 1.5s alternate infinite ease-in-out;
}

@keyframes upDown {
  to {
    transform: translateY(100px);
  }
}
<div class="transactionBG" style="top: 10%;left: 50%;width: 128px;height: 278px;">
  <img class="transaction" src="https://seeklogo.com/images/I/instagram-circle-logo-E285122AB7-seeklogo.com.png" style="width: 102.4px; height: 102.4px;">
  <h4>test</h4>
</div>

当前状态演示:http://jsfiddle.net/4huj31fb/2/

2 个答案:

答案 0 :(得分:1)

transactionBG是h4的父级。因此,您必须将动画添加到transactionBG

.transactionBG {
  position: absolute;
  left: 0;
  overflow: hidden;
  animation: upDown 1.5s alternate infinite ease-in-out;
}

答案 1 :(得分:1)

您仅将动画赋予了图像。移动此行:

animation: upDown 1.5s alternate infinite ease-in-out;

.transactionBG 选择器

html {
  overflow: hidden;
}

body {
  margin: 0;
  overflow: hidden;
}

canvas {
  display: block;
  vertical-align: bottom;
}

h4 {
  position: absolute;
  background-color: red;
  width: 128px;
  display: inline-block;
  color: white;
  font-size: 24px;
}

.transactionBG {
  position: absolute;
  left: 0;
  overflow: hidden;
  animation: upDown 1.5s alternate infinite ease-in-out;
}

.transaction {
  width: 64px;
  height: 64px;
  position: absolute;
}

@keyframes upDown {
  to {
    transform: translateY(100px);
  }
}
<div class="transactionBG" style="top: 10%;left: 50%;width: 128px;height: 278px;">
  <img class="transaction" src="https://seeklogo.com/images/I/instagram-circle-logo-E285122AB7-seeklogo.com.png" style="width: 102.4px; height: 102.4px;">
  <h4>test</h4>
</div>