代码之家  ›  专栏  ›  技术社区  ›  thisisjohnspeaking

无法弄清楚如何将文本置于此圆动画的中心

  •  0
  • thisisjohnspeaking  · 技术社区  · 3 年前

    我发现这个很酷的动画。我想使用,但我不知道如何将文本居中。如果可能的话,我希望避免使用绝对位置。

    我正在等待带有类名的文本 "text-to-center" 在圆圈内。

    Codepen

    <div class="outer-circle">
      <span class="text-to-center">0:00:00</span>
      <div class="wrapper">
        
        <div class="breath">
          <div class="flare1"></div>
          <div class="flare2"></div>
        </div>
      </div>
    </div>
    
    @keyframes pulse {
      20% {
        transform: scale(1);
      }
      40% {
        transform: scale(0.6);
      }
      50% {
        transform: scale(0.6);
      }
      60% {
        transform: scale(0.6);
      }
      80% {
        transform: scale(1);
      }
    }
    
    @keyframes rotate {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
    
    body {
      background-color: #000215;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      overflow: hidden;
      color: #fff;
      
      .outer-circle {
        width: 300px;
        height: 300px;
        border-radius: 150px;
        box-shadow: 0 0 5px 15px rgba(#2F2B8C, 0.1);
        background-image: radial-gradient(#2F2B8C00, #2F2B8C00 50%, #2F2B8C33 90%);
      }
      
      .wrapper {
        animation: pulse 8s cubic-bezier( 0.7, 0, 0.3, 1 )  infinite;
        position: relative;
        
        .breath {
          width: 300px;
          height: 300px;
          border-radius: 150px;
          position: relative;
          background-color: #000215;
          animation: rotate 16s linear infinite;
          
          &::before {
            content: '';
            position: absolute;
            top: 0; 
            right: 0; 
            bottom: 0; 
            left: 0;
            z-index: -1;
            margin: -3px;
            border-radius: inherit;
            background: linear-gradient(135deg, #D904B5, #2F2B8C);
            box-shadow: 0 0 14px 15px rgba(#2F2B8C, 0.3);
          }
          
          &::after {
            content: "";
            display: block;
            position: relative;
            width: 300px;
            height: 300px;
            border-radius: 150px;
            background-color: #000215;
          }
          
          .flare1 {
            width: 240px;
            height: 240px;
            content: "";
            display: block;
            border-radius: 50px;
            background-image: radial-gradient(#D904B563, #D904B500 60%);
            position: absolute;
            left: -70px;
            top: -70px;
            z-index: -1;
          }
    
          .flare2 {
            width: 240px;
            height: 240px;
            content: "";
            display: block;
            border-radius: 50px;
            background-image: radial-gradient(#2F2B8C63, #D904B500 60%);
            position: absolute;
            right: -70px;
            bottom: -70px;
            z-index: -1;
          }
        }
      }
    }
    
    .text-to-center {
     // want to get this inside the circle, centered. Would like to avoid position absolute if possible
    }
    

    有什么方法可以在不使用绝对位置的情况下实现这一点?

    2 回复  |  直到 3 年前
        1
  •  0
  •   Prince    3 年前

    请尝试使用此css:

          @keyframes pulse {
      20% {
        transform: scale(1);
      }
      40% {
        transform: scale(0.6);
      }
      50% {
        transform: scale(0.6);
      }
      60% {
        transform: scale(0.6);
      }
      80% {
        transform: scale(1);
      }
    }
    
    @keyframes rotate {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
    
    body {
      background-color: #000215;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      overflow: hidden;
      color: #fff;
      
      .outer-circle {
        width: 300px;
        height: 300px;
        border-radius: 150px;
        box-shadow: 0 0 5px 15px rgba(#2F2B8C, 0.1);
        background-image: radial-gradient(#2F2B8C00, #2F2B8C00 50%, #2F2B8C33 90%);
        position: relative;
      }
      
      .wrapper {
        animation: pulse 8s cubic-bezier( 0.7, 0, 0.3, 1 )  infinite;
        position: relative;
        
        .breath {
          width: 300px;
          height: 300px;
          border-radius: 150px;
          position: relative;
          background-color: #000215;
          animation: rotate 16s linear infinite;
          
          &::before {
            content: '';
            position: absolute;
            top: 0; 
            right: 0; 
            bottom: 0; 
            left: 0;
            z-index: -1;
            margin: -3px;
            border-radius: inherit;
            background: linear-gradient(135deg, #D904B5, #2F2B8C);
            box-shadow: 0 0 14px 15px rgba(#2F2B8C, 0.3);
          }
          
          &::after {
            content: "";
            display: block;
            position: relative;
            width: 300px;
            height: 300px;
            border-radius: 150px;
            background-color: #000215;
          }
          
          .flare1 {
            width: 240px;
            height: 240px;
            content: "";
            display: block;
            border-radius: 50px;
            background-image: radial-gradient(#D904B563, #D904B500 60%);
            position: absolute;
            left: -70px;
            top: -70px;
            z-index: -1;
          }
    
          .flare2 {
            width: 240px;
            height: 240px;
            content: "";
            display: block;
            border-radius: 50px;
            background-image: radial-gradient(#2F2B8C63, #D904B500 60%);
            position: absolute;
            right: -70px;
            bottom: -70px;
            z-index: -1;
          }
        }
      }
    }
    
    center {
        height: 100%;
        position: absolute;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .text-to-center {
        position: relative;
        z-index: 9;
    }
    <div class="outer-circle">
            <center><span class="text-to-center">0:00:00</span></center>
            <div class="wrapper">
              
              <div class="breath">
                <div class="flare1"></div>
                <div class="flare2"></div>
              </div>
            </div>
          </div>
        2
  •  -1
  •   Tushar Gupta    3 年前

    鉴于您已要求避免 position: absolute 。

    您可以添加 display:flex 到您的文本 span ,并使用 top 属性进行对齐。你可以根据需要调整它的价值

    .text-to-center {
        top: 160px;
        z-index: 200;
        position: relative;
        display: flex;
        justify-content: center;
    }
    

    工作代码如下:

    @keyframes pulse {
      20% {
        transform: scale(1);
      }
      40% {
        transform: scale(0.6);
      }
      50% {
        transform: scale(0.6);
      }
      60% {
        transform: scale(0.6);
      }
      80% {
        transform: scale(1);
      }
    }
    
    @keyframes rotate {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }
    
    body {
      background-color: #000215;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      overflow: hidden;
      color: #fff;
    }
    
    body .outer-circle {
      position: relative;
      width: 300px;
      height: 300px;
      border-radius: 150px;
      box-shadow: 0 0 5px 15px rgba(47, 43, 140, 0.1);
      background-image: radial-gradient(#2f2b8c 0, #2f2b8c 0 50%, #2f2b8c 33 90%);
    }
    
    body .wrapper {
      animation: pulse 8s cubic-bezier(0.7, 0, 0.3, 1) infinite;
      position: relative;
    }
    
    body .wrapper .breath {
      width: 300px;
      height: 300px;
      border-radius: 150px;
      position: relative;
      background-color: #000215;
      animation: rotate 16s linear infinite;
    }
    
    body .wrapper .breath::before {
      content: '';
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: -1;
      margin: -3px;
      border-radius: inherit;
      background: linear-gradient(135deg, #d904b5, #2f2b8c);
      box-shadow: 0 0 14px 15px rgba(47, 43, 140, 0.3);
    }
    
    body .wrapper .breath::after {
      content: "";
      display: block;
      position: relative;
      width: 300px;
      height: 300px;
      border-radius: 150px;
      background-color: #000215;
    }
    
    body .wrapper .breath .flare1 {
      width: 240px;
      height: 240px;
      content: "";
      display: block;
      border-radius: 50px;
      background-image: radial-gradient(#d904b5 63, #d904b5 0 60%);
      position: absolute;
      left: -70px;
      top: -70px;
      z-index: -1;
    }
    
    body .wrapper .breath .flare2 {
      width: 240px;
      height: 240px;
      content: "";
      display: block;
      border-radius: 50px;
      background-image: radial-gradient(#2f2b8c 63, #d904b5 0 60%);
      position: absolute;
      right: -70px;
      bottom: -70px;
      z-index: -1;
    }
    
    .text-to-center {
      top: 160px;
      z-index: 200;
      position: relative;
      display: flex;
      justify-content: center;
    }
    <div class="outer-circle">
      <span class="text-to-center">0:00:00</span>
      <div class="wrapper">
        <div class="breath">
          <div class="flare1"></div>
          <div class="flare2"></div>
        </div>
      </div>
    </div>