【问题标题】:Spinner Overlay Bootstrap 4 Card Body Only - Flex AlignmentSpinner Overlay Bootstrap 4 Card Body Only - Flex Alignment
【发布时间】:2019-07-26 08:18:26
【问题描述】:

尝试在 bootstrap 4 卡体中垂直和水平对齐微调器。

我已经尝试过 my-auto 和 justify-content-center & align-items-center,但我遗漏了一些东西。

我已经检查了显示类型,我相信我的绝对显示是正确的

我也检查了我的位置,我相信我正确使用了 flex。

我的目标是将旋转器垂直和水平加载到任何卡片上的主体上。

我在看什么?

代码笔:https://codepen.io/sterling415/pen/xBErWV

HTML:

<div class="card">
  <h5 class="card-header">Featured</h5>
  <div class="card-body justify-content-center align-items-center">
                <div id="overlay" onclick="off()">
                <div id="justify-content-center align-items-center">
                    <div class="spinner"></div>
                </div>
            </div>
    <h5 class="card-title">Special title treatment</h5>
    <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
     <button type="button" class="btn btn-primary" onclick="on()">Turn on overlay effect</button>
  </div>
</div>      

    </div>
</div>

CSS:

.spinner {
  height: 60px;
  width: 60px;
  margin: auto;
  display: flex;
  position: absolute;
  -webkit-animation: rotation .6s infinite linear;
  -moz-animation: rotation .6s infinite linear;
  -o-animation: rotation .6s infinite linear;
  animation: rotation .6s infinite linear;
  border-left: 6px solid rgba(0, 174, 239, .15);
  border-right: 6px solid rgba(0, 174, 239, .15);
  border-bottom: 6px solid rgba(0, 174, 239, .15);
  border-top: 6px solid rgba(0, 174, 239, .8);
  border-radius: 100%;
}

@-webkit-keyframes rotation {
  from {
    -webkit-transform: rotate(0deg);
  }
  to {
    -webkit-transform: rotate(359deg);
  }
}

@-moz-keyframes rotation {
  from {
    -moz-transform: rotate(0deg);
  }
  to {
    -moz-transform: rotate(359deg);
  }
}

@-o-keyframes rotation {
  from {
    -o-transform: rotate(0deg);
  }
  to {
    -o-transform: rotate(359deg);
  }
}

@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
}

#overlay {
  position: absolute;
  display: none;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0,0,0,0.5);
  z-index: 2;
  cursor: pointer;
}

JS:

function on() {
  document.getElementById("overlay").style.display = "flex";
}

function off() {
  document.getElementById("overlay").style.display = "none";
}

【问题讨论】:

  • 对 Bootstrap flex 命令使用类而不是 ID。

标签: javascript html css flexbox bootstrap-4


【解决方案1】:

&lt;div id="justify-content-center align-items-center"&gt;更改为

&lt;div class="w-100 d-flex justify-content-center align-items-center"&gt;

在下面查看它的实际效果:

function on() {
  document.getElementById("overlay").style.display = "flex";
}

function off() {
  document.getElementById("overlay").style.display = "none";
}
.spinner {
  height: 60px;
  width: 60px;
  margin: auto;
  display: flex;
  position: absolute;
  -webkit-animation: rotation .6s infinite linear;
  -moz-animation: rotation .6s infinite linear;
  -o-animation: rotation .6s infinite linear;
  animation: rotation .6s infinite linear;
  border-left: 6px solid rgba(0, 174, 239, .15);
  border-right: 6px solid rgba(0, 174, 239, .15);
  border-bottom: 6px solid rgba(0, 174, 239, .15);
  border-top: 6px solid rgba(0, 174, 239, .8);
  border-radius: 100%;
}

@-webkit-keyframes rotation {
  from {
    -webkit-transform: rotate(0deg);
  }
  to {
    -webkit-transform: rotate(359deg);
  }
}

@-moz-keyframes rotation {
  from {
    -moz-transform: rotate(0deg);
  }
  to {
    -moz-transform: rotate(359deg);
  }
}

@-o-keyframes rotation {
  from {
    -o-transform: rotate(0deg);
  }
  to {
    -o-transform: rotate(359deg);
  }
}

@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
}

#overlay {
  position: absolute;
  display: none;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 2;
  cursor: pointer;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">

    <div class="card">
      <h5 class="card-header">Featured</h5>
      <div class="card-body justify-content-center align-items-center">
        <div id="overlay" onclick="off()">
          <div class="w-100 d-flex justify-content-center align-items-center">
            <div class="spinner"></div>
          </div>
        </div>
        <h5 class="card-title">Special title treatment</h5>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <button type="button" class="btn btn-primary" onclick="on()">Turn on overlay effect</button>
      </div>
    </div>

  </div>
</div>

Codepen

【讨论】:

  • 你能解释一下为什么它覆盖了整个卡片而不是仅仅覆盖了卡片主体,即使覆盖 div 和 spinner 是卡片主体的子元素?
  • 覆盖有position: absolute,有top: 0; left: 0; right: 0; bottom: 0;,因为它是父级,.card-body没有position: relative;属性,它绝对定位w.r.t。具有position: relative 的第一个父级,即.card,使其扩展到.card 的全高和全宽。可以把position: relative;加到.card-body看看有什么区别
猜你喜欢
  • 1970-01-01
  • 2019-06-09
  • 2019-05-14
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 2017-08-17
相关资源
最近更新 更多