我正在使用一个纯粹的CSS加载微调器(感谢w3schools获得的想法)与position: absolute;
一起定位然后将其居中,我使用calc(50% - (spinner's width-or-height / 2));
为了看到它的实际效果,我制作了一个jsfiddle来演示它here(下面的片段)。我们的想法是.pure-center
是一个弹性框,它将一个较小的圆圈与align-items: center; justify-content: center;
对齐,从而给出一个纯粹的中心元素。但是,与我的旋转器相比,我使用position: absolute;
进行定位时,您可以看到y轴正确地工作(它在中间垂直),但x轴稍微向右。
这是一个演示它的片段:
html,
body {
height: 100%;
overflow: hidden;
}
.pure-center {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.pure-center > div {
border-radius: 50%;
border: 1px solid black;
height: 40px;
width: 40px;
}
.spinner {
--size: 48px; /* Creates variable for height and width */
animation: norm-spin .65s linear infinite;
border: 8px solid white;
border-top: 8px solid #369;
border-radius: 50%;
height: var(--size);
left: calc(50% - (var(--size) / 2));
position: absolute;
top: calc(50% - (var(--size) / 2));
transition: 0.25s;
width: var(--size);
z-index: 1;
}
/*\
* I feel this is REALLY hacky,
* but it is a way of adding another border around the spinner
\*/
.spinner:before {
content: " ";
position: absolute;
top: -9px;
left: -9px;
right: -9px;
bottom: -9px;
border: 1px solid darkgrey;
border-radius: 50%;
}
@keyframes norm-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}

<div class="pure-center">
<div></div>
</div>
<div class="spinner"></div>
&#13;
有没有人知道为什么我的position: absolute;
定位方法可以垂直工作但不是水平工作,因为它是相同的代码(我使用CSS变量--size: 48px;
)来执行垂直和水平定位?
答案 0 :(得分:1)
以下是我将如何处理它...设置主要父项来控制位置,高度和宽度。然后创建旋转器的子div将相应地进行调整。然后,您可以更改.spinhold
的宽度/高度,它将放大/缩小而不会更改动画或内圈。 (实际的微调器将保持在9px,但除非经过调整)
此外,您不必担心更改内圈填充颜色以匹配任何背景..中心是透明的。
我还不是使用CSS变量的粉丝,它们没有得到足够的支持而且灵活性也很好,但我不觉得这里需要它。
html,
body {
margin: 0;
padding: 0;
height: 100%;
background: linear-gradient(to bottom, rgba(0,0,0,.1) 0%, rgba(0,0,0,.2) 100%);
background: -webkit-linear-gradient(bottom, rgba(0,0,0,.1) 0%, rgba(0,0,0,.2)100%);
}
/* center holder*/
.spinhold {
position: relative;
top: 50%;
left: 50%;
height:50px;
width: 50px;
background: transparent;
border: 1px solid #aaa; /* outer border */
border-radius: 50%;
text-align: center;
vertical-align: middle;
transform: translateX(-50%) translateY(-50%);
box-shadow: 0 5px 10px rgba(0,0,0, .3);}
.spinborder {
position: absolute;
top: -1px; /* compensates for border on spinhold */
left: -1px; /* compensates for border on spinhold */
z-index: 3;
display: block;
margin: 10px; /* width of spin border + 1px for border on this element */
width: calc(100% - 20px); /* spinhold width - (left + right) margin */
height: calc(100% - 20px); /* spinhold height - (left + right) margin */
border: 1px solid #aaa; /* inner border */
border-radius: 50%;
box-shadow: inset 0 5px 10px rgba(0,0,0, .3);
}
.spin {
position: absolute;
z-index: 2;
top: 0;
left: 0;
display: block;
margin: 0;
width: calc(100% - 18px); /* spinhold width - this border width * 2 */
height: calc(100% - 18px) ; /* spinhold height - this border width * 2 */
border: 9px solid #eee; /* spin track color */
border-bottom-color: #00e; /* color of spinner */
border-radius: 50%;
animation: norm-spin .65s linear infinite;
}
@keyframes norm-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
&#13;
<div class="spinhold">
<div class="spinborder"></div>
<div class="spin"></div>
</div>
&#13;
答案 1 :(得分:0)
您在计算中忘记了边框宽度。
为什么它不是exaclty 64px,我不知道。不知何故62px是perfekt ..
html,
body {
height: 100%;
overflow: hidden;
}
.pure-center {
position: absolute;
top: calc(50% - 20px);
left: calc(50% - 20px);
height: 40px;
width: 40px;
}
.pure-center > div {
border-radius: 50%;
border: 1px solid black;
height: 40px;
width: 40px;
}
.spinner {
--size: 48px; /* Creates variable for height and width */
--sizeWidthBorder: 62px;
animation: norm-spin .65s linear infinite;
border: 8px solid white;
border-top: 8px solid #369;
border-radius: 50%;
height: var(--size);
left: calc(50% - (var(--sizeWidthBorder) / 2));
position: absolute;
top: calc(50% - (var(--sizeWidthBorder) / 2));
transition: 0.25s;
width: var(--size);
z-index: 1;
}
/*\
* This is REALLY hacky, but it adds another border around the spinner
\*/
.spinner:before {
content: " ";
position: absolute;
top: -9px;
left: -9px;
right: -9px;
bottom: -9px;
border: 1px solid darkgrey;
border-radius: 50%;
}
@keyframes norm-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<div class="pure-center">
<div></div>
</div>
<div class="spinner"></div>
答案 2 :(得分:0)
这是解决方案
更新.spinner
left: calc(50% - (var(--size) / 2) - 8px);
由于您已border
8px
,因此您需要从left
属性
html,
body {
height: 100%;
overflow: hidden;
}
.pure-center {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.pure-center > div {
border-radius: 50%;
border: 1px solid black;
height: 40px;
width: 40px;
}
.spinner {
--size: 48px; /* Creates variable for height and width */
animation: norm-spin .65s linear infinite;
border: 8px solid white;
border-top: 8px solid #369;
border-radius: 50%;
height: var(--size);
left: calc(50% - (var(--size) / 2) - 8px);
position: absolute;
top: calc(50% - (var(--size) / 2));
transition: 0.25s;
width: var(--size);
z-index: 1;
}
/*\
* This is REALLY hacky, but it adds another border around the spinner
\*/
.spinner:before {
content: " ";
position: absolute;
top: -9px;
left: -9px;
right: -9px;
bottom: -9px;
border: 1px solid darkgrey;
border-radius: 50%;
}
@keyframes norm-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
以下是更新的 Demo
<div class="pure-center">
<div></div>
</div>
<div class="spinner"></div>
<RelativeLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@id/rel_actionbar"
android:layout_margin="10dp"
android:padding="4dp">
<RelativeLayout
android:id="@+id/rel_screenshot"
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:id="@+id/img_to_print"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<ImageView
android:id="@+id/img_tshape"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="@drawable/img_t"
android:visibility="visible" />
</RelativeLayout>
答案 3 :(得分:-1)
这只是因为你正在使用border 8px。
left: calc(50% - 32px);
如果您不想硬编码,请考虑边框,将 16px 添加到其宽度