预加载器svg垂直对齐问题

时间:2017-06-09 10:03:16

标签: html css svg

我无法垂直对齐我的preloader svg。显示在屏幕高度的50%以下。黑色边框代表屏幕的50%,因此仅用于演示目的。

感谢任何帮助。

jsfiddle demo

enter image description here

body,
html {
  height: 100%;
}

.demo-square1 {
  height: 50%;
  width: 100%;
  border-bottom: 1px solid #333333;
  box-sizing: border-box;
  position: absolute;
  top: 0;
}

.preloader-box {
  height: 100%;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 999999;
}

.preloader {
  height: 35px;
  width: 35px;
  font-size: 0;
  display: inline-block;
  left: 50%;
  position: absolute;
  top: 50%;
  bottom: 50%;
  transform: translate(-50%, -50%);
  -webkit-animation: outer 6600ms linear infinite;
  animation: outer 6600ms linear infinite;
}

.preloader svg {
  -webkit-animation: inner 1320ms linear infinite;
  animation: inner 1320ms linear infinite;
}

.preloader svg circle {
  fill: none;
  stroke: #448AFF;
  stroke-linecap: square;
  -webkit-animation: arc 1320ms cubic-bezier(0.8, 0, 0.4, 0.8) infinite;
  animation: arc 1320ms cubic-bezier(0.8, 0, 0.4, 0.8) infinite;
}

@-webkit-keyframes outer {
  0% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@keyframes outer {
  0% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@-webkit-keyframes inner {
  0% {
    -webkit-transform: rotate(-100.8deg);
    transform: rotate(-100.8deg);
  }
  100% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
}

@keyframes inner {
  0% {
    -webkit-transform: rotate(-100.8deg);
    transform: rotate(-100.8deg);
  }
  100% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
}

@-webkit-keyframes arc {
  0% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: 0;
  }
  40% {
    stroke-dasharray: 151.55042961px, 210.48670779px;
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: -151.55042961px;
  }
}

@keyframes arc {
  0% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: 0;
  }
  40% {
    stroke-dasharray: 151.55042961px, 210.48670779px;
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: -151.55042961px;
  }
}
<div class="preloader-box">
  <div class="preloader"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="35" width="35" viewbox="0 0 75 75"><circle cx="37.5" cy="37.5" r="33.5" stroke-width="8"/></svg></div>
</div>

<div class="demo-square1"></div>

2 个答案:

答案 0 :(得分:1)

Keyframes outer转换属性覆盖.preloader的转换属性,您可以使用top: calc(50% - 17px);transform: rotate(360deg) translate(-50%, -50%);

&#13;
&#13;
body,
html {
  height: 100%;
}

.demo-square1 {
  height: 50%;
  width: 100%;
  border-bottom: 1px solid #333333;
  box-sizing: border-box;
  position: absolute;
  top: 0;
}

.preloader-box {
  height: 100%;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 999999;
}

.preloader {
  height: 35px;
  width: 35px;
  font-size: 0;
  display: inline-block;
  left: 50%;
  position: absolute;
  top: calc(50% - 17px);
  bottom: 50%;
  transform: translate(-50%, -50%);
  -webkit-animation: outer 6600ms linear infinite;
  animation: outer 6600ms linear infinite;
}

.preloader svg {
  -webkit-animation: inner 1320ms linear infinite;
  animation: inner 1320ms linear infinite;
}

.preloader svg circle {
  fill: none;
  stroke: #448AFF;
  stroke-linecap: square;
  -webkit-animation: arc 1320ms cubic-bezier(0.8, 0, 0.4, 0.8) infinite;
  animation: arc 1320ms cubic-bezier(0.8, 0, 0.4, 0.8) infinite;
}

@-webkit-keyframes outer {
  0% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@keyframes outer {
  0% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@-webkit-keyframes inner {
  0% {
    -webkit-transform: rotate(-100.8deg);
    transform: rotate(-100.8deg);
  }
  100% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
}

@keyframes inner {
  0% {
    -webkit-transform: rotate(-100.8deg);
    transform: rotate(-100.8deg);
  }
  100% {
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
}

@-webkit-keyframes arc {
  0% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: 0;
  }
  40% {
    stroke-dasharray: 151.55042961px, 210.48670779px;
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: -151.55042961px;
  }
}

@keyframes arc {
  0% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: 0;
  }
  40% {
    stroke-dasharray: 151.55042961px, 210.48670779px;
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: -151.55042961px;
  }
}
&#13;
<div class="preloader-box">
  <div class="preloader"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="35" width="35" viewbox="0 0 75 75"><circle cx="37.5" cy="37.5" r="33.5" stroke-width="8"/></svg></div>
</div>

<div class="demo-square1"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您应该更新您的css并使用flex获取相同的

工作小提琴 fiddle code

.preloader-box {
  height: 100%;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 999999;
  display:flex; /* Add this */
  justify-content:center;  /* Add this */
  align-items:center;  /* Add this */
  flex-flow:column nowrap;  /* Add this */
  position:relative;  /* Add this */
} 

.preloader {
  height: 35px;
  width: 35px;
  font-size: 0;
  display: block;
  position: absolute;  /* Add this  and remove top, bottom, left and transform */
  -webkit-animation: outer 6600ms linear infinite;
          animation: outer 6600ms linear infinite;
}

&#13;
&#13;
body, html {
  height: 100%;
  padding:0px;
  margin:0px;
}

.demo-square1{
  height: 50%;
  width: 100%;
  border-bottom: 1px solid #333333;
  box-sizing:border-box;
  position:absolute;
  top:0;
}

.preloader-box {
  height: 100%;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 999999;
  display:flex;
  justify-content:center;
  align-items:center;
  flex-flow:column nowrap;
  position:relative;
}     

.preloader {
  height: 35px;
  width: 35px;
  font-size: 0;
  display: block;
  position: absolute;
  -webkit-animation: outer 6600ms linear infinite;
          animation: outer 6600ms linear infinite;
}
.preloader svg {
  -webkit-animation: inner 1320ms linear infinite;
          animation: inner 1320ms linear infinite;
}
.preloader svg circle {
  fill: none;
  stroke: #448AFF;
  stroke-linecap: square;
  -webkit-animation: arc 1320ms cubic-bezier(0.8, 0, 0.4, 0.8) infinite;
          animation: arc 1320ms cubic-bezier(0.8, 0, 0.4, 0.8) infinite;
}
@-webkit-keyframes outer {
  0% {
    -webkit-transform: rotate(0);
            transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
            transform: rotate(360deg);
  }
}
@keyframes outer {
  0% {
    -webkit-transform: rotate(0);
            transform: rotate(0);
  }
  100% {
    -webkit-transform: rotate(360deg);
            transform: rotate(360deg);
  }
}
@-webkit-keyframes inner {
  0% {
    -webkit-transform: rotate(-100.8deg);
            transform: rotate(-100.8deg);
  }
  100% {
    -webkit-transform: rotate(0);
            transform: rotate(0);
  }
}
@keyframes inner {
  0% {
    -webkit-transform: rotate(-100.8deg);
            transform: rotate(-100.8deg);
  }
  100% {
    -webkit-transform: rotate(0);
            transform: rotate(0);
  }
}
@-webkit-keyframes arc {
  0% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: 0;
  }
  40% {
    stroke-dasharray: 151.55042961px, 210.48670779px;
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: -151.55042961px;
  }
}
@keyframes arc {
  0% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: 0;
  }
  40% {
    stroke-dasharray: 151.55042961px, 210.48670779px;
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dasharray: 1 210.48670779px;
    stroke-dashoffset: -151.55042961px;
  }
}
&#13;
  <div class="preloader-box">
		<div class="preloader"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="35" width="35" viewbox="0 0 75 75"><circle cx="37.5" cy="37.5" r="33.5" stroke-width="8"/></svg></div>
	</div>
  
  <div class="demo-square1"></div>
  
&#13;
&#13;
&#13;