居中div固定位置 - 没有解决方案工作

时间:2016-10-29 07:53:36

标签: html css

我只想制作一个位于页面中间的预加载器 要求:

  • 职位:固定;
  • 以X轴为中心

它只是一个带有课程预加载器的分区'身体。身体是这个div的直接父母,其间没有其他包装。

.preloader {
    position: fixed;
    display: inline-block;
    z-index: 99;
    top: 45%;
    left: 50%;
    width: 60px;
    height: 60px;
    -ms-transform: translateX(100px); /*100px just to test if it moved at all, initially had -50%, see list below*/
    -webkit-transform: translateX(100px);
    transform: translateX(100px);
//=====rest is just animation and aesthetics======
    border: 3px solid #8A2EE6;
    border-radius: 50%;
    border-bottom: 3px solid black;
    box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
    animation-name: rotatePreloader;
    animation-duration: 0.65s;
    animation-direction: normal;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    transition: opacity 1s;
}

我已经完成了:

  • display:block;
  • display:inline-block;
  • 平移X(-50%);
  • 平移X(30像素);
  • 翻译(-50%,0);
  • 翻译(30px,0);
  • 翻译(-50%, - 50%);
  • 重新排列变换.. lol
  • 脱掉了-o& -mos
  • 保证金:0自动(与职位合作:当我没有需要修复时相对)

https://jsfiddle.net/goa3v2ke/#

2 个答案:

答案 0 :(得分:2)

您需要执行以下操作:将top / left设置为50%,然后使用相同的值将translate移回去,在这种情况下为-50%。

样本1现在将其水平和垂直居中,并且通过更改top / left值,您可以按照您想要的方向移动它,样本2有33%作为{{1} }值。

根据问题编辑进行更新

小的X / Y轴偏移是由top之前执行的rotate引起的,因此请将translate规则更改为此,在示例3和{{{ 3}}

@keyframes

样本1

@keyframes rotatePreloader {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}
.preloader {
    position: fixed;
    display: inline-block;
    z-index: 99;
    top: 50%;
    left: 50%;
    width: 60px;
    height: 60px;
    transform: translate(-50%,-50%);
  
/*=====rest is just animation and aesthetics======*/
    border: 3px solid #8A2EE6;
    border-radius: 50%;
    border-bottom: 3px solid black;
    box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
    animation-name: rotatePreloader;
    animation-duration: 0.65s;
    animation-direction: normal;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    transition: opacity 1s;
}

样本2

<div class="preloader"></div>
.preloader {
    position: fixed;
    display: inline-block;
    z-index: 99;
    top: 33%;
    left: 50%;
    width: 60px;
    height: 60px;
    transform: translate(-50%,-50%);
  
/*=====rest is just animation and aesthetics======*/
    border: 3px solid #8A2EE6;
    border-radius: 50%;
    border-bottom: 3px solid black;
    box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
    animation-name: rotatePreloader;
    animation-duration: 0.65s;
    animation-direction: normal;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    transition: opacity 1s;
}

样本3

<div class="preloader"></div>
.leftPreloaderBG {
  position: fixed;
  background-color: black;
  width: 50%;
  height: 100%;
  z-index: 98;
  top: 0px;
  left: 0px;
  transition: width 1s;
}

.leftPreloaderBG.loaded {
  width: 0;
}

.rightPreloaderBG {
  position: fixed;
  background-color: black;
  width: 50%;
  height: 100%;
  z-index: 98;
  top: 0px;
  right: 0px;
  transition: width 1s;
}

.rightPreloaderBG.loaded {
  width: 0;
}

@keyframes rotatePreloader {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

.preloader {
  position: fixed;
  display: inline-block;
  z-index: 99;
  top: 33%;
  left: 50%;
  width: 60px;
  height: 60px;
  transform: translate(-50%, -50%);
  /*=====rest is just animation and aesthetics======*/
  border: 3px solid #8A2EE6;
  border-radius: 50%;
  border-bottom: 3px solid black;
  box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
  animation-name: rotatePreloader;
  animation-duration: 0.65s;
  animation-direction: normal;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  transition: opacity 1s;
}

.preloader.loaded {
  opacity: 0;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  padding: 0;
  width: 100%;
  color: white;
  font-size: 1em;
  background-color: gray;
  background-position: center;
  background-repeat: repeat;
}

答案 1 :(得分:1)

这是因为您覆盖了关键帧动画中的变换转换值。试试这样:

.leftPreloaderBG {
  position: fixed;
  background-color: black;
  width: 50%;
  height: 100%;
  z-index: 98;
  top: 0px;
  left: 0px;
  transition: width 1s;
}

.leftPreloaderBG.loaded {
  width: 0;
}

.rightPreloaderBG {
  position: fixed;
  background-color: black;
  width: 50%;
  height: 100%;
  z-index: 98;
  top: 0px;
  right: 0px;
  transition: width 1s;
}

.rightPreloaderBG.loaded {
  width: 0;
}

@keyframes rotatePreloader {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

.preloader {
  position: fixed;
  display: inline-block;
  z-index: 99;
  top: 33%;
  left: 50%;
  width: 60px;
  height: 60px;
  transform: translate(-50%, -50%);
  /*=====rest is just animation and aesthetics======*/
  border: 3px solid #8A2EE6;
  border-radius: 50%;
  border-bottom: 3px solid black;
  box-shadow: 0px 0px 20px 1px rgba(153, 102, 255, 0.5) inset, 0px 0px 20px 1px rgba(153, 102, 255, 0.5);
  animation-name: rotatePreloader;
  animation-duration: 0.65s;
  animation-direction: normal;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
  transition: opacity 1s;
}

.preloader.loaded {
  opacity: 0;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  margin: 0;
  padding: 0;
  width: 100%;
  color: white;
  font-size: 1em;
  background-color: gray;
  background-position: center;
  background-repeat: repeat;
}
<body>
  <div class="preloader"></div>
  <div class="leftPreloaderBG"></div>
  <div class="rightPreloaderBG"></div>
</body>