【问题标题】:How to unveil a div with fixed contents如何揭开具有固定内容的 div
【发布时间】:2015-06-27 03:54:44
【问题描述】:

我通过从页面底部揭开元素来制作动画。子元素的位置不应受揭幕动画大小变化的影响。

这就是我要找的:

<div>
    <h1>Hello stranger</h1>
</div>

两个元素都定位在absolute,顶部、右侧、底部和左侧设置为0。通过将divtop100% 降低到0% 来显示内容。 有一个Fiddle

我的目标是

  1. 使标题始终保持在固定位置(中间)
  2. 在标题尚未被发现时隐藏它

但这些都是我的问题(包括小提琴)

  • position:fixed 用于h1 时,overflow becomes always visible
  • 当使用position:absolute时,位置isn't fixed correctly
  • 使用clip:rect(500px auto auto auto) 时,我无法使用%。因此,如果不使用 JavaScript,我无法确定视口的确切高度。

在没有 JS 的情况下,还可以做什么来归档所描述的行为?

【问题讨论】:

    标签: css overflow css-position


    【解决方案1】:

    解决方案是同时将h1 移动到与div 动画相反的方向,从而产生h1 保持静止的错觉。然而,h1 实际上也相对于div 在相反的方向移动。

    我保留了相同的标记以及您在两个元素上声明的 position: absolute; top: 0; right:0; left:0; bottom: 0; 要求。

    http://jsfiddle.net/eLbcdc9c/6/

    HTML

    <div>
        <h1>Hello stranger</h1>
    </div>
    

    CSS

    div {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: #000;
        transform: translate3d(0, 0%, 0);
        overflow: hidden;
    }
    
    h1 {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        color: #fff;
        text-align: center;
        transform: translate3d(0, 50%, 0);
    }
    
    /* Transition effect and timing must be the same for both elements to give the perfect illusion */
    div,
    h1 {
        transition: transform 500ms ease-in;
    }
    
    /* The transform class added to the div to begin the animation on the h1 and div at the same time */
    .hide {
        transform: translate3d(0, 100%, 0);
    }
    .hide h1 {
        transform: translate3d(0, -50%, 0);
    }
    

    hide 类应用于div(通过单击小提琴中的切换按钮),您应该会获得所需的效果。

    这里需要注意的是 3dtransforms 的使用。这些比动画topmargins 更流畅,并利用使用设备GPU for Hardware acceleration 来实现这一点。

    3dtransforms 是 very well supported,但在某些情况下您可能需要应用浏览器前缀,具体取决于您要提供的支持。

    【讨论】:

    • 很好的答案!特别是关于GPU加速的部分。谢谢!
    【解决方案2】:

    我认为您可以将 h1 从 position:absolute 更改为 fixed

    http://jsfiddle.net/o8d79jum/8/

    div {
        position:absolute;
        overflow:hidden;
        top:100%;
        right:0;
        bottom:0;
        left:0;
        background:black;
        color:white;
        animation:ani 2s infinite;
    }
    h1 {
        position:fixed;
        top:50%;
        left:0;
        right:0;
        text-align:center;
        transform:translateY(-50%);
        margin:0;
    }
    @keyframes ani {
        0% {
            top:100%;
        }
        100% {
            top:0%;
        }
    }
    <div>
         <h1>Hi, I'm centerized</h1>
    </div>

    【讨论】:

    • 抱歉回复晚了;不幸的是,我不能使用position:fixed,因为它使标题始终可见(请参阅your fiddle (edited))。还有其他想法吗?谢谢
    • @RienNeVaPlu͢ 好,你不能像demo一样把它设置成和默认背景一样的颜色吗?
    • 我正在处理的项目使用附加到标题的图像,所以我不能伪造它的可见性(opacity 也不会做这项工作)。为了简单起见,我删除了问题中的代码。
    【解决方案3】:

    这是我的尝试,使用伪元素隐藏 h1

    这个伪元素具有任意高的高度,并且总是位于 div 的上侧。

    div:after {
        content: "";
        position: absolute;
        height: 9999px;
        width: 100%;
        bottom: 100%;
        background-color: white;
    }
    

    fiddle

    【讨论】:

      【解决方案4】:

      作为替代方案,您可以翻译文本顶部的div:after

      fiddle

      $$('button').first().on('click', function(e){ $$('body').first().toggleClassName('animate'); });
      body{
          margin:0
      }
      
      
      div {
           position:absolute;
          height: 100%;
          width: 100%;
          z-index: -1;
          overflow:hidden
      }
      
      div:after {
          content: "";
          position: absolute;
          height: 100%;
          width: 100%;
          bottom: 100%;
          background-color: black;
          top:0;
          z-index:999;
      }
      h1 {
          position:absolute;
          color:red;
          top:50%;
          left:0;
          right:0;
          text-align:center;
          transform:translateY(-50%);
      }
      
      .animate div:after {
          animation-name:ani;
          animation-duration:2s;
          animation-fill-mode: forwards;
      }
      
      @keyframes ani {
          0% {
              transform: translateY(0);
          }
          
          100% {
             transform: translateY(-100%);
          }
      }
      button{position:absolute;top:5px;left:5px;
      z-index:9999}
      <script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.2/prototype.min.js"></script>
      <div><h1>Hi, I'm centerized</h1></div>
      
      
      <button>Start</button>

      【讨论】:

        【解决方案5】:

        这就是你想要的exemple

        你需要这样做。并查看示例中的 css。

        <h1>Hello stranger</h1>
        <div class="test"></div>
        

        【讨论】:

        • 我需要在 div 中包含标题。只是掩盖它不会为我做这项工作:/
        • 但是您的 div 高度不会保持 100%。您想将该标题居中,但您的 div 只有 20% 的高度?我不认为你可以做到这一点。您需要另一个 div 来对齐文本或其他内容。
        • seems possible 使用clip。只是没有百分比单位,这使得很难确定高度。所以我想可能还有另一种方式......
        • 剪辑不支持百分比高度w3.org/TR/CSS2/visufx.html#propdef-clip,但您可以使用javascript找到一些东西来确定高度
        猜你喜欢
        • 1970-01-01
        • 2015-10-29
        • 2019-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-02
        相关资源
        最近更新 更多