[英]How to keep my circle div from moving
Inside this Codepen each of the circles need to transition into a new configuration at 800px
. 在此Codepen中,每个圆圈都需要转换为
800px
的新配置。 The configuration I'm looking for is to have circle1
and circle2
still centered in the page and have circle3
underneath circles 1 and 2. For clarity the image bellow show's how I want the circles to end up. 我在寻找的配置是
circle1
和circle2
仍然集中在网页并circle3
下方盘旋1和2清晰度图像波纹管节目的我多么想圆结束了。
HTML: HTML:
<div class="circles">
<div class="circle1">
<a id="projects" href="#">Projects</a>
</div>
<div class="circle2">
<a id="about" href="#">About</a>
</div>
<div class="circle3">
<a id="contact" href="#">Contact</a>
</div>
</div>
As you can see in the screen shot I'm was able to transition circle1
and circle2
into moving slightly to the left while also moving circle3
underneath circle1
and circle2
by telling circle3
to move left: -150px
. 正如你在截图中看到,我是能够过渡
circle1
和circle2
到稍微向左移动,同时移动circle3
下方circle1
和circle2
告诉circle3
移动left: -150px
。
However circle3
does not want to stay in his position when he is moved. 然而
circle3
并不想留在他的位置,当他移动。 So if I slide him down then we will obviously keep sliding to the bottom left. 因此,如果我将他滑下来,我们显然会向左下方滑动。 No matter where I move
circle3
he keeps on moving when I move the view port to smaller size. 无论我在哪里移动
circle3
,当我将视口移动到更小的尺寸时,他继续移动。
Here is my media query that is telling the circles to move when at 800px
: 这是我的媒体查询,告诉圈子在
800px
时移动:
@media (max-width: 800px) {
.circles {
width: 80%;
}
.circle1, circle2 {
left: 20%;
}
.circle3 {
left: -150px;
}
}
I'm thinking this has something to moving circle3
to left: -150px
. 我认为这有一些东西可以将
circle3
left: -150px
移动left: -150px
。
How do I keep the circle3
from continuing to move while also keeping it centered between circle1
and circle2
? 如何让
circle3
继续移动,同时保持circle1
和circle2
之间的circle2
?
Try my solution (compiled SCSS to CSS for code snippet, see original on Codepen ): 尝试我的解决方案(编译SCSS到CSS代码片段,请参阅Codepen上的原始代码):
.circles { padding: 2rem; display: flex; margin: 0 auto; width: 90%; } .circle1, .circle2, .circle3 { display: flex; position: relative; justify-content: center; border-radius: 50%; padding-bottom: 30%; height: 0; width: 30%; margin: 5px; background-color: magenta; left: 0; transition: margin-top 0.5s linear, left 0.5s linear; } .circle1 #projects, .circle2 #projects, .circle3 #projects, .circle2 #about, .circle3 #contact { line-height: 1; margin-top: -0.5em; padding-top: 50%; color: #fff; } .circle2 { background-color: blue; } .circle3 { background-color: gold; } @media (max-width: 800px) { .circle1, .circle2, .circle3, .circle2 { left: 15%; } .circle3 { margin: 0 auto; margin-top: 28.5%; left: -35.11%; } }
<div class="circles"> <div class="circle1"> <a id="projects" href="#">Projects</a> </div> <div class="circle2"> <a id="about" href="#">About</a> </div> <div class="circle3"> <a id="contact" href="#">Contact</a> </div> </div>
You use relative values to set element's height (it's padding-bottom: 30%
in your case), but absolute value to top position of .circle3
( top: 90px
). 您可以使用相对值来设置元素的高度(它的
padding-bottom: 30%
在您的情况下为padding-bottom: 30%
),但绝对值为.circle3
顶部位置( top: 90px
)。 When you're resizing viewport, height of all circles are changing, but .circle3
continues to have fixed position. 当您调整视口大小时,所有圆的高度都在变化,但
.circle3
继续具有固定位置。
I changed top: 90px
to margin-top: 28.5%
and it seems OK. 我换了
top: 90px
到margin-top: 28.5%
,看起来还可以。 Also removed some unnecessary code. 还删除了一些不必要的代码
PS Maybe I didn't understand your question but I saw only this problem. PS也许我不明白你的问题,但我只看到了这个问题。
I think that circle3
looks like he is moving but he actually is not..What's happening is that I moved him out of his original position in the first place therefore circle3
only knows where he should be based on where I told him to go. 我认为
circle3
看起来像是在移动,但他实际上并不是......发生的事情是我首先将他从原来的位置circle3
,因此circle3
只知道他应该根据我告诉他去哪里。
Since I told circle3
to move left:-150px;
因为我告诉
circle3
left:-150px;
移动left:-150px;
he has gone there and will stay there even as I make the view port smaller. 他已经去了那里,即使我把视口缩小了也会留在那里。 What is changing is the view port not
circle3
has he stays in his position but dose not adjust to the position of the view port as I make it smaller. 改变的是视口不是
circle3
,他留在他的位置但是当我把它变circle3
,不能调整到视口的位置。
I believe this is why circle3
will not stay centered between the two circles as he dose not know how to adjust at this moment. 我相信这就是为什么
circle3
不会在两个圆圈之间保持居中的原因,因为他此时并不知道如何调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.