CSS动画重叠div

时间:2017-01-10 03:14:56

标签: html css css3

我正在尝试为滚动文本设置动画(在段落中),以便它从div的底部移动到顶部,滚动div(变为不可见)然后循环。这是相关的css:

@keyframes showAndScroll {
            0% {opacity: 0;}
            10% {opacity: 0.85;}
            50% {opacity: 0.85;}
            60% {opacity: 0;}
            100% {opacity: 0;}

        }

        .infobar {
            position: absolute;
            width: 100%;
            height: 30%;
            bottom: 0%;
            color: white;
            background-color: red;
            opacity: 0.75;
            text-indent: 30px;
            font-size: 200%;


            pointer-events: none;

            animation-name: showAndScroll;
            animation-duration: 40s;
            animation-iteration-count: infinite;
        }

        @keyframes scroll {
            0% {
                transform: translateY(600%); color: red;
                }
            50% {
                transform: translateY(-200%); color: blue;
                }
        }

        .infobar p {
            position: absolute;
            width: 100%;
            overflow: hidden;
            display: inline-block;
            animation-name: scroll;
            animation-duration: 40s;
            animation-iteration-count: infinite;
            animation-timing-function: linear;
        }

和html代码:

<div class="infobar">
        <p>
            Infobar test
        <p>
    </div>

我有两个问题:

  1. 文本与文档的其余部分重叠。如何在段落到其父div的边缘时使段落不可见?我正在寻找这种效果:http://media02.hongkiat.com/marquee-css3-animation//demo/index2.html

  2. 出于某种原因,将段落放在div的100%处似乎并没有将它放在div的“底部”(我目前将其置于600%)。这是为什么?

  3. 任何输入都表示赞赏。这是我的JSfiddle:https://jsfiddle.net/essi/oqh6ok00/1/

1 个答案:

答案 0 :(得分:1)

overflow: hidden;添加到课程.infobar。通过这种方式,溢出被剪裁,您的动画元素将在边缘中可见,类似于您在链接示例中向我们展示的内容。

&#13;
&#13;
@keyframes showAndScroll {
  0% {
    opacity: 0;
  }
  10% {
    opacity: 0.85;
  }
  50% {
    opacity: 0.85;
  }
  60% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

.infobar {
  position: absolute;
  width: 100%;
  height: 30%;
  bottom: 0%;
  color: white;
  background-color: red;
  opacity: 0.75;
  text-indent: 30px;
  font-size: 200%;
  pointer-events: none;
  animation-name: showAndScroll;
  animation-duration: 40s;
  animation-iteration-count: infinite;
  overflow: hidden;
}

@keyframes scroll {
  0% {
    transform: translateY(600%);
    color: red;
  }
  50% {
    transform: translateY(-200%);
    color: blue;
  }
}

.infobar p {
  position: absolute;
  width: 100%;
  overflow: hidden;
  display: inline-block;
  animation-name: scroll;
  animation-duration: 40s;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
&#13;
<div class="infobar">
  <p>
    Infobar test
    <p>
</div>
&#13;
&#13;
&#13;