gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:07 25 4
gpt4 key购买 nike

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

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

它只是一个主体中带有“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;
}

我已经完成了:

  • 显示: block ;
  • 显示:内联 block ;
  • 翻译X(-50%);
  • translateX(30px);
  • 翻译(-50%, 0);
  • 翻译(30px, 0);
  • 翻译(-50%, -50%);
  • 重新安排转换..哈哈
  • 起飞-o & -mos
  • margin: 0 auto(当我不需要修复它时使用 position: relative)

https://jsfiddle.net/goa3v2ke/#

最佳答案

你需要这样做,将 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>

关于html - 居中 div 固定位置 - 没有解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40317171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com