简体   繁体   English

如何使这就像两个“绝对”和“相对”定位在同一时间,我位置的元素? -CSS

[英]How do I position an element so that it acts like both 'absolutely' & 'relatively' positioned at the same time? - CSS

You can see the implementation here: http://jsfiddle.net/BMWZd/25/ 您可以在此处查看实现: http : //jsfiddle.net/BMWZd/25/

When you click on one of the names in Box#1, you will see the circle in the top left corner of the box move up and down. 当您单击Box#1中的名称之一时,您会看到该框左上角的圆圈上下移动。

How do I stop that? 我该如何阻止呢? While also, making sure that it shows in the top left corner of each of the boxes on all browser sizes? 同时,还要确保它显示在所有浏览器尺寸的每个框的左上角?

So position:absolute will keep it in one place regardless of what happens around it. 所以position:absolute会将它放在一个地方,而不管周围发生了什么。 But it won't put it in the exact same position (relatively) on diff browser sizes. 但这不会将它放在差异浏览器大小的完全相同的位置(相对)。

But position:relative will. 但立场:相对意愿。

How do I get the best of both worlds? 如何获得两全其美?

I know this doesn't directly answer your question, however I think it could be a better solution approach. 我知道这并不能直接回答您的问题,但是我认为这可能是更好的解决方案。 Please correct me if I'm wrong. 如果我错了,请纠正我。

I took a glance at your layout and IMHO it is a bit too complex. 我看了一眼您的布局,恕我直言,这有点太复杂了。 I think you would benefit from simplifying it. 我认为您将从简化中受益。

Here is a quick example I did. 这是我做的一个简单示例。 Honestly, I don't know if it fits your needs but maybe it will be useful in some way. 老实说,我不知道它是否适合您的需求,但也许它会以某种方式有用。

The JS: JS:

<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
  $(function(){

    $('li').click(function(){
      $('.circle').removeClass('inactive').addClass('active');
      $('li').removeClass('big');
      $(this).addClass('big');
    });

  })
</script>

The styles: 样式:

<style>
  li{
    cursor: pointer;
  }
  #super-container{
    width: 200px;
    height: 200px;
    background: #F2F2F2;
  }
  #circle-container{
    padding: 10px;
    height:50px;
  }
  .circle{
    border: #FF0000;
    width: 50px;
    height: 50px;
  }
  .active{
    background: #AA0000;
  }
  .inactive{
    background: #330000;
  }
  .big{
    font-size: 2em;
  }
</style>

The markup: 标记:

<div id="super-container">
  <div id="circle-container">
    <div class="circle inactive"></div>
  </div>
  <ul id="the-buttons">
    <li>Button 1</li>
    <li>Button 2</li>
    <li>Button 3</li>
    <li>Button 4</li>
  </ul>
</div>

So position:absolute will keep it in one place regardless of what happens around it. 所以position:absolute会将它放在一个地方,而不管周围发生了什么。 But it won't put it in the exact same position (relatively) on diff browser sizes. 但这不会将它放在差异浏览器大小的完全相同的位置(相对)。

The position should be the same if you where using a reset file . 如果您使用的是重置文件,则位置应相同。 At least in theory. 至少在理论上。

In my experience, when there are elements that change their size interactively, the best way to avoid "surprises" on the layout is by not using the box model for positioning, and having everything "around the element that changes its size" absolutely positioned. 以我的经验,当某些元素以交互方式更改其大小时,避免在布局上出现“意外”的最佳方法是不使用盒模型进行定位,而将“围绕其大小更改的元素”周围的所有内容都绝对定位。

In your case, I'd make the tds position relative, and everything inside them absolutely positioned: 在您的情况下,我将使tds位置相对,并且将其中的所有内容绝对定位:

  • Make the tds position:relative 使tds position:relative
  • Remove the float:left and the margins from css class #sit-in-corner . 从CSS类#sit-in-corner删除float:left和margins。 Position it by making it position-absolute and adding top and left . 通过使其处于绝对位置并添加topleft对其进行定位。

Now the circled numbers should be "out of the influence" of the links. 现在,带圆圈的数字应该在链接的“影响范围之外”。 You can place the internal tables however you want (margins, etc) inside the td, but I'd also go with position: absolute for them. 您可以根据需要将内部表(边距等)放在td中,但我也要保留位置:绝对值。

EDIT - Noticed that the issue was happening because the td's reacted strangely to the things inside them changing their size. 编辑-注意到该问题正在发生,因为td对其中更改尺寸的事物产生了奇怪的反应。

Solved this mainly by putting a div inside the td, removing the class 'dashed-panel' from the td and put it on the div. 解决此问题的方法主要是将div放在TD内,从TD上删除类“虚线面板”,然后将其放在div上。 Also, made the class 'dashed-panel' be position:relative, and the changes above. 同样,使类“虚线面板”成为position:relative,并进行上面的更改。

The results can be seen in http://jsfiddle.net/BMWZd/30/ 结果可以在http://jsfiddle.net/BMWZd/30/中看到

I have to remove all the extraneous stuff, jsfiddle was too slow with so much(?) html. 我必须删除所有多余的东西,jsfiddle使用太多(?)html太慢了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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