【问题标题】:centering div fixed position -no solution worked居中 div 固定位置 - 没有解决方案
【发布时间】:2016-10-29 17:31:03
【问题描述】:

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

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

它只是一个在 body 中带有类 'preloader' 的 div。 Body 是这个 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;
}

我已经完成了:

  • 显示:块;
  • 显示:内联块;
  • translateX(-50%);
  • translateX(30px);
  • 翻译(-50%, 0);
  • 翻译(30px, 0);
  • 翻译(-50%, -50%);
  • 重新排列变换.. 哈哈
  • 起飞-o & -mos
  • margin: 0 auto(在我不需要固定时使用 position: relative)

https://jsfiddle.net/goa3v2ke/#

【问题讨论】:

  • 将您的代码添加到 jsfiddle 并提供链接。

标签: html css


【解决方案1】:

您需要这样做,将top/left 设置为50%,然后使用相同的值将它们移回translate,在本例中为-50%。

样品 1 现在水平和垂直居中,通过更改 top/left 值,您可以将其移动到您想要的方向,样品 2 有 33% 作为 top 值。


根据问题编辑更新

较小的 X/Y 轴偏移是由在 translate 之前执行的 rotate 引起的,因此请将您的 @keyframes 规则更改为此,如示例 3 和 update of your fiddle 所示

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

示例 1

.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;
}
<div class="preloader"></div>

示例 2

.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;
}
<div class="preloader"></div>

示例 3

.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>

【讨论】:

  • 你告诉我的,我已经试过了。 “翻译(-50%,0)”这就是我所拥有的,无论我将预加载器放在它们之前还是之后,它都不适用于其他 2 个 div。 jsfiddle.net/goa3v2ke/#
  • @reddtoric 那是由rotate引起的。这是您的小提琴的更新:jsfiddle.net/goa3v2ke/1 ...请注意将translate(-50%, -50%)添加到您的@keyframes
【解决方案2】:

这是因为您在关键帧动画中覆盖了 transform translate 值。试试这样:

.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>

【讨论】:

    猜你喜欢
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2014-09-02
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多