首页 文章

CSS动画重叠div

提问于
浏览
2

我正在尝试为滚动文本设置动画(在段落中),以便它从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>

我有两个问题:

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

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

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

1 回答

  • 1

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

    @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;
    }
    
    <div class="infobar">
      <p>
        Infobar test
        <p>
    </div>
    

相关问题