【问题标题】:How to add a small box around a text?如何在文本周围添加一个小框?
【发布时间】:2021-01-20 02:54:14
【问题描述】:

我有以下代码:

// Preloader
 $(window).on('DOMContentLoaded', function() {
    if ($('#preloader').length) {
      $('#preloader').delay(1000).fadeOut('slow');
    }
});
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

@import url('https://fonts.googleapis.com/css2?family=Montserrat+Alternates:wght@500;700&family=Montserrat:wght@400;600&family=Oswald:wght@500&family=Pacifico&family=Roboto:ital,wght@0,400;0,900;1,500&display=swap');

#preloader {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  overflow: hidden;
  background: white;
   display: flex;
    justify-content: center;
    align-items: center;
}


.svg-file path {
    fill: transparent;
    stroke-width: 3;
    stroke: rgb(1, 36, 133);
}

.svg-file.z-logo path {
    stroke-dasharray: 550;
    stroke-dashoffset: 0;
}

.svg-file.z-logo path {
    animation: animate-zlogo 3s linear infinite;
}

svg {
    filter: drop-shadow(2px 2px 3px rgba(0, 0, 0, 1));
    transform: scale(2);
}

.svg-file h2 {
    font-family: "Roboto", cursive;
    transform: translate(0, 50px) skewX(-210deg) rotate(-6deg);
    font-size: 3em;
    color: #044d77;
}

.svg-file span {
    animation: dots 2.5s steps(6, end) infinite;
    font-size: 5em;
    display: block;
    transform: translate(0, 65px) skewX(-210deg) rotate(-6deg);
    background-color: rgb(5, 46, 80);
    width: 8px;
    height: 5px;
}

@keyframes fadein-fadeout {
    0% {
        opacity: 0;
    }

    50% {
        opacity: 1;
    }

    100% {
        opacity: 0;
    }
}

@keyframes animate-zlogo {
    0% {
        stroke-dashoffset: -50;
    }

    20% {
        stroke-dashoffset: 550;
        fill: transparent;
    }

    40% {
        fill: transparent;
        stroke-dashoffset: 1100;
    }

    60% {
        stroke-dashoffset: 1100;
        fill: #05f7f9;    
        }

    80% {
        stroke-width: 0;
       fill: #05f7f9;
    }

    100% {
        /* stroke-dashoffset: 0; */
        stroke-width: 3;
        fill: transparent;
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="preloader">
<div class="svg-file z-logo">            
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 133 133" width="133" height="133"><g id="H"><path d="M45.33 78.22L87.67 78.22L87.67 133L121.05 133L121.05 0L87.67 0L87.67 49.33L45.33 49.33L45.33 0L11.95 0L11.95 133L45.33 133L45.33 78.22Z" fill="#47AF9A"/></g></svg>
        <span></span>
    </div>
</div>

我想在它周围添加一个小框,以便更好地向您展示我正在寻找什么样的输出,那么我想要这个:

所以,我现在缺少的是黑色的background box,我不希望牛被page cover 填充,它的高度和宽度应该与上图大致相同。将其视为网站的预加载器。在页面中间,一个带有h 徽标的中小型框显示在里面,就像上面显示的那样。

注意:动画应该在盒子里面,不应该在盒子外面,所以当你运行上面的代码时,动画应该在黑盒子里。我只是在寻找有关如何准确设置盒子尺寸的帮助,如图所示。我稍后会调整测量值,但现在我正在寻找要显示的框的输出。

我尝试使用与之前提出类似问题的人相同的逻辑,但它不起作用,它给了我一个黑屏,这不是我想要的。我希望整个屏幕为white,但如上图所示,此图像的背景框为黑色。

预期输出:

动画应该在inside盒子里完美地运行,正如你所见,盒子在屏幕中间,屏幕上有一个whitebackground。

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    您在 .svg-file div 上设置黑色背景...并将 svg 缩放到 0.66 (2/3)。

    // Preloader
     $(window).on('DOMContentLoaded', function() {
        if ($('#preloader').length) {
          $('#preloader').delay(2000).fadeOut('slow');
        }
    });
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    @import url("https://fonts.googleapis.com/css2?family=Montserrat+Alternates:wght@500;700&family=Montserrat:wght@400;600&family=Oswald:wght@500&family=Pacifico&family=Roboto:ital,wght@0,400;0,900;1,500&display=swap");
    
    #preloader {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: 9999;
      overflow: hidden;
      background: white;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    #preloader .svg-file {
      background-color: black;  /* added */
      overflow: hidden;
    }
    
    #preloader .svg-file path {
      fill: transparent;
      stroke-width: 3;
      stroke: rgb(1, 36, 133);
    }
    
    #preloader .svg-file.z-logo path {
      stroke-dasharray: 550;
      stroke-dashoffset: 0;
    }
    
    #preloader .svg-file.z-logo path {
      animation: animate-zlogo 3s linear infinite;
    }
    
    #preloader svg {
      filter: drop-shadow(2px 2px 3px rgba(0, 0, 0, 1));
      transform: scale(0.66);  /* Changed */
    }
    
    #preloader .svg-file h2 {
      font-family: "Roboto", cursive;
      transform: translate(0, 50px) skewX(-210deg) rotate(-6deg);
      font-size: 3em;
      color: #044d77;
    }
    
    #preloader .svg-file span {
      animation: dots 2.5s steps(6, end) infinite;
      font-size: 5em;
      display: block;
      transform: translate(0, 65px) skewX(-210deg) rotate(-6deg);
      background-color: rgb(5, 46, 80);
      width: 8px;
      height: 5px;
    }
    /* Not used 
    @keyframes fadein-fadeout {
      0% {
        opacity: 0;
      }
    
      50% {
        opacity: 1;
      }
    
      100% {
        opacity: 0;
      }
    }
    */
    @keyframes animate-zlogo {
      0% {
        stroke-dashoffset: -50;
      }
    
      20% {
        stroke-dashoffset: 550;
        fill: transparent;
      }
    
      40% {
        fill: transparent;
        stroke-dashoffset: 1100;
      }
    
      60% {
        stroke-dashoffset: 1100;
        fill: #05f7f9;
      }
    
      80% {
        stroke-width: 0;
        fill: #05f7f9;
      }
    
      100% {
        /* stroke-dashoffset: 0; */
        stroke-width: 3;
        fill: transparent;
      }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="preloader">
      <div class="svg-file z-logo">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 133 133" width="133" height="133">
          <g id="H">
            <path d="M45.33 78.22L87.67 78.22L87.67 133L121.05 133L121.05 0L87.67 0L87.67 49.33L45.33 49.33L45.33 0L11.95 0L11.95 133L45.33 133L45.33 78.22Z" fill="#47AF9A" />
          </g>
        </svg>
        <span></span>
      </div>
    </div>

    【讨论】:

    • 你是怎么去掉盒子下面的那个小按钮的?
    • 我认为这是一个 SVG 错误...所以我将 overflow:hidden 添加到 .svg-file。
    • 您能否重命名这些类,因为它会覆盖我的其他 css,我也尝试过这样做,但我没有让 naimation 工作。您可以在每个类名后添加一个1,然后相应地更新html吗?
    • 我在每个 css 规则前面添加了#preloader...所以它们将仅适用于#preloader div 中的元素。
    • 我看到了你的另一个问题,我没有看到任何 CSS 冲突或覆盖...
    【解决方案2】:

    使用 ::before 伪

    // Preloader
    $(window).on('DOMContentLoaded', function() {
      if ($('#preloader').length) {
        $('#preloader').delay(1000).fadeOut('slow');
      }
    });
    * {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }
    
    @import url('https://fonts.googleapis.com/css2?family=Montserrat+Alternates:wght@500;700&family=Montserrat:wght@400;600&family=Oswald:wght@500&family=Pacifico&family=Roboto:ital,wght@0,400;0,900;1,500&display=swap');
    
    
    
    .svg-file path {
        fill: transparent;
        stroke-width: 3;
        stroke: rgb(1, 36, 133);
    }
    
    .svg-file.z-logo path {
        stroke-dasharray: 550;
        stroke-dashoffset: 0;
    }
    
    .svg-file.z-logo path {
        animation: animate-zlogo 2s linear infinite;
    }
    
    svg {
        filter: drop-shadow(2px 2px 3px rgba(0, 0, 0, 1));
        transform: scale(2);
    }
    
    .svg-file h2 {
        font-family: "Roboto", cursive;
        transform: translate(0, 50px) skewX(-210deg) rotate(-6deg);
        font-size: 3em;
        color: #044d77;
    }
    
    .svg-file span {
        animation: dots 2.5s steps(6, end) infinite;
        font-size: 5em;
        display: block;
        transform: translate(0, 65px) skewX(-210deg) rotate(-6deg);
        background-color: rgb(5, 46, 80);
        width: 8px;
        height: 5px;
    }
    
    @keyframes fadein-fadeout {
        0% {
            opacity: 0;
        }
    
        50% {
            opacity: 1;
        }
    
        100% {
            opacity: 0;
        }
    }
    
    @keyframes animate-zlogo {
        0% {
            stroke-dashoffset: -50;
        }
    
        20% {
            stroke-dashoffset: 550;
            fill: transparent;
        }
    
        40% {
            fill: transparent;
            stroke-dashoffset: 1100;
        }
    
        60% {
            stroke-dashoffset: 1100;
            fill: #05f7f9;    
            }
    
        80% {
            stroke-width: 0;
           fill: #05f7f9;
        }
    
        100% {
            /* stroke-dashoffset: 0; */
            stroke-width: 3;
            fill: transparent;
        }
    }
    #preloader {
      position: absolute;
      margin: auto;
      z-index: 9999;
      overflow: hidden;
      background: white;
      display: flex;
      justify-content: center;
      align-items: center;
      align-content: center;
      top: 0px;
      left: 0px;
      bottom: 0px;
      right: 0px;
    }
    
    .z-logo svg {
      top: 0px;
      left: 0px;
      bottom: 0px;
      right: 0px;
      margin: auto;
      justify-content: center;
      align-items: center;
      align-content: center;
      position: relative;
      display: flex;
      width: 50%;
    }
    
    .z-logo::before {
      content: "";
        top: 0px;
      left: 0px;
      bottom: 0px;
      right: 0px;
      margin: auto;
      background: black;
      position: absolute;
      display: inline-flex;
      border: 1px solid black;
      width: 200px;
      height: 200px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="preloader">
      <div class="svg-file z-logo">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 133 133" width="133" height="133"><g id="H"><path d="M45.33 78.22L87.67 78.22L87.67 133L121.05 133L121.05 0L87.67 0L87.67 49.33L45.33 49.33L45.33 0L11.95 0L11.95 133L45.33 133L45.3 78.22Z" fill="#47AF9A"/></g></svg>
        <span></span>
      </div>
    </div>

    【讨论】:

    • 如何去掉盒子下面的那个小按钮?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2014-09-12
    相关资源
    最近更新 更多