javascript - 如何使 CSS 微调器中的文本居中?

标签 javascript html css

我在互联网上的某个地方找到了一段 CSS,它重新创建了很酷的 PayPal 微调器,我用它做了一个 fiddle :

https://jsfiddle.net/55s5oxkf/5/

效果很好,但我不知道如何将文本放在微调器的正中央,例如“正在加载...”。我进行了修补和尝试,但无法使任何工作正常进行。

这是 CSS:

.spinner.loading {
    display: none;
    padding: 50px;
    text-align: center;
}
.spinner.loading:before {
    content: "";
    height: 90px;
    width: 90px;
    margin: -15px auto auto -15px;
    position: absolute;
    top: 35%;
    left: 45%;
    border-width: 8px;
    border-style: solid;
    border-color: #2180c0 #ccc #ccc;
    border-radius: 100%;
    animation: rotation .7s infinite linear;
}
@keyframes rotation {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(359deg);
    }
}

和 HTML:

<div id="divSpinner" class="spinner loading"></div>

在开始和结束的 div 元素之间放置文本没有任何作用。有什么想法吗?

最佳答案

<center>不再支持(中心在 html5 中弃用)所以使用这样的类:

.centered {
  text-align: center;
}

然后使用 calc 获取加载文本的正确位置:

.loading-text {
  width: 90px;
  position: absolute;
  top: calc(50% - 15px);
  left: calc(50% - 45px);
  text-align: center;
}

$("#btnLoadRecords").click(function() {
  $("#divSpinner").show();
  setTimeout(function() {
    $("#divSpinner").hide();
  }, 10000);
});
.centered {
  text-align: center;
}

.spinner.loading {
  display: none;
  padding: 50px;
  text-align: center;
}

.loading-text {
  width: 90px;
  position: absolute;
  top: calc(50% - 15px);
  left: calc(50% - 45px);
  text-align: center;
}

.spinner.loading:before {
  content: "";
  height: 90px;
  width: 90px;
  margin: -15px auto auto -15px;
  position: absolute;
  top: calc(50% - 45px);
  left: calc(50% - 45px);
  border-width: 8px;
  border-style: solid;
  border-color: #2180c0 #ccc #ccc;
  border-radius: 100%;
  animation: rotation .7s infinite linear;
}

@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <div class="centered">
    <div id="divSpinner" class="spinner loading">
      <div class="loading-text">Loading ...</div>
    </div>

    <button id="btnLoadRecords" style="cursor:pointer;position: absolute; top: 52%; left: 45%;">Load Records</button>
  </div>
</body>

关于javascript - 如何使 CSS 微调器中的文本居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44080331/

相关文章:

javascript - 在 JavaScript 对象中移动并在每次点击时递增 1?

javascript - Popup.js 不工作

javascript - 提交 HTML 表单,一键执行两个操作

javascript - 如何禁用特殊字符粘贴到文本框中

css - 在 Internet Explorer 中调试 CSS 的工具

javascript - Chrome 扩展跨域 AJAX 给出 NETWORK_ERR : XMLHttpRequest Exception 101

javascript - 弃用移动应用程序上的触摸操作

java - JEdi​​torPane 未检测到 <a> 标签,而是将它们放入我的字符串中

html - 添加VH时图像重叠Div

javascript - CSS动画向前然后向后闪烁