简体   繁体   English

如何防止我的圈子移动

[英]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. 我在寻找的配置是circle1circle2仍然集中在网页并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 . 正如你在截图中看到,我是能够过渡circle1circle2到稍微向左移动,同时移动circle3下方circle1circle2告诉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继续移动,同时保持circle1circle2之间的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: 90pxmargin-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM