代码之家  ›  专栏  ›  技术社区  ›  Alan Souza

CSS伪:溢出后

  •  0
  • Alan Souza  · 技术社区  · 6 年前

    我想有一个进度指示,在一个DIV中使用模拟边界动画,该DIV是一个位置相对的flexbox元素。我正在添加一个伪元素来动画边框底部。

    CSS

           .test {
                 display: flex;
                 box-sizing: border-box;
                 width: 384px;
                 border: 1px solid;
                 height: 48px;
                 position: relative;
            }
    
            .test:before {
                 content: '';
                 position: absolute;
                 bottom: 0;
                 left: 0;
                 // width: 100%;
                 width: -webkit-fill-available;
                 height: 2px;
                 background: red;
                 animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
            }
    
             @keyframes running-progress {
                 0% { margin-left: 0px; margin-right: 100%; }
                 50% { margin-left: 25%; margin-right: 0%; }
                 100% { margin-left: 100%; margin-right: 0; } }
    

    下面是一个有问题的代码沙盒:

    https://codesandbox.io/s/yopwy5klz

    问题

    如何在这个示例中将宽度限制为伪元素不会溢出框定义的宽度?

    如果我加上 width: -webkit-fill-available; 它工作得很好,但我认为这不是正确的解决方案。

    这是一个“有效”的版本 https://codesandbox.io/s/wyp8q26qvk

    3 回复  |  直到 6 年前
        1
  •  2
  •   Asons Oliver Joseph Ash    6 年前

    自从 transform 利用GPU,您希望将其用于动画,因为它将具有比所有其他属性使用的基于CPU的动画更好的性能和更少的延迟。

    在这里我把 transfrom 价值 scaleX() translateX() ,在哪里 斯卡克斯 将设置其宽度和 翻译器 它的水平位置。

    作为注释, 转型 从右到左执行它的值,因此如果切换它们的位置,结果将不同

    堆栈片段-就像“非工作”一样,当涉及到如何设置动画时,没有溢出。

    .test {
      display: flex;
      box-sizing: border-box;
      width: 384px;
      border: 1px solid;
      height: 48px;
      position: relative;
    }
    
    .test::before {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      transform: translateX(0%) scaleX(1);
      height: 2px;
      background: red;
      animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
    }
    
    @keyframes running-progress {
      0% {
        transform: translateX(0%) scaleX(1);
      }
      50% {
        transform: translateX(12.5%) scaleX(0.75);
      }
      100% {
        transform: translateX(50%) scaleX(0);
      }
    }
    <div class="test"></div>

    堆栈代码段-作为您的“工作”版本,使用 转型

    .test {
      display: flex;
      box-sizing: border-box;
      width: 384px;
      border: 1px solid;
      height: 48px;
      position: relative;
    }
    
    .test::before {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      transform: translateX(-50%) scaleX(0);
      height: 2px;
      background: red;
      animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
    }
    
    @keyframes running-progress {
      0% {
        transform: translateX(-50%) scaleX(0);
      }
      50% {
        transform: translateX(12.5%) scaleX(.75);
      }
      100% {
        transform: translateX(50%) scaleX(0);
      }
    }
    <DIV class=“测试”></DIV>

    三个注意事项:

    • <div /> 不是自动结束标记,它需要开始和结束标记 <div></div>

    • 宽度 fill-available 价值决定了工作,而且可能是比下面更好的选择 overflow: hidden (当另一个孩子可能需要溢出时),并且很快将成为一个选项,尽管它仍然是 实验的 不推荐用于生产

    • 有时一个人所需要的就是 溢出:隐藏 ,此处适用于您的原始“非工作”样本

      .test {
        display: flex;
        box-sizing: border-box;
        width: 384px;
        border: 1px solid;
        height: 48px;
        position: relative;
        overflow: hidden;
      }
      
      .test::before {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 2px;
        background: red;
        animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
      }
      
      @keyframes running-progress {
        0% {
          margin-left: 0px;
          margin-right: 100%;
        }
        50% {
          margin-left: 25%;
          margin-right: 0%;
        }
        100% {
          margin-left: 100%;
          margin-right: 0;
        }
      }
      <DIV class=“测试”></DIV>
        2
  •  0
  •   Temani Afif    6 年前

    您也可以使用一个元素来完成此操作,并且 linear-gradient :

    .box {
      position: relative;
      box-sizing: border-box;
      width: 384px;
      height: 48px;
      border: 1px solid;
      background:
        linear-gradient(red,red) bottom/100% 2px no-repeat;
      animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite
    }
    
    @keyframes running-progress {
      0% {
        background-position: bottom left;
        background-size:0% 2px;
      }
      50% {
        background-position: bottom right;
        background-size:70% 2px;
      }
      100% {
        background-position: bottom right;
        background-size:0% 2px;
      }
    }
    <div class="box"></div>
        3
  •  0
  •   Ori Drori    6 年前

    而不是动画边缘动画 left right 属性,并移除伪元素的 width: 100% :

    .test {
      position: relative;
      box-sizing: border-box;
      width: 384px;
      height: 48px;
      border: 1px solid;
    }
    
    .test::before {
      position: absolute;
      bottom: 0;
      height: 2px;
      background: red;
      animation: running-progress 2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
      content: '';
    }
    
    @keyframes running-progress {
      0% {
        left: 0px;
        right: 100%;
      }
      50% {
        left: 25%;
        right: 0%;
      }
      100% {
        left: 100%;
        right: 0;
      }
    }
    <div class="test"></div>