Fixed position but relative to container

后端 未结 25 1633
独厮守ぢ
独厮守ぢ 2020-11-21 23:52

I am trying to fix a div so it always sticks to the top of the screen, using:

position: fixed;
top: 0px;
right: 0px;

However,

相关标签:
25条回答
  • 2020-11-22 00:20

    I have created this jQuery plugin to solve a similar issue I was having where I had a centered container (tabular data), and I wanted the header to fix to the top of the page when the list was scrolled, yet I wanted it anchored to the tabular data so it would be wherever I put the container (centered, left, right) and also allow it to move left and right with the page when scrolled horizontally.

    Here is the link to this jQuery plugin that may solve this problem:

    https://github.com/bigspotteddog/ScrollToFixed

    The description of this plugin is as follows:

    This plugin is used to fix elements to the top of the page, if the element would have scrolled out of view, vertically; however, it does allow the element to continue to move left or right with the horizontal scroll.

    Given an option marginTop, the element will stop moving vertically upward once the vertical scroll has reached the target position; but, the element will still move horizontally as the page is scrolled left or right. Once the page has been scrolled back down passed the target position, the element will be restored to its original position on the page.

    This plugin has been tested in Firefox 3/4, Google Chrome 10/11, Safari 5, and Internet Explorer 8/9.

    Usage for your particular case:

    <script src="scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="scripts/jquery-scrolltofixed-min.js" type="text/javascript"></script>
    
    $(document).ready(function() {
        $('#mydiv').scrollToFixed();
    });
    
    0 讨论(0)
  • 2020-11-22 00:21

    Yes, according to the specs, there is a way.

    While I agree that Graeme Blackwood's should be the accepted answer, because it practically solves the issue, it should be noted that a fixed element can be positioned relatively to its container.

    I noticed by accident that when applying

    -webkit-transform: translateZ(0);
    

    to the body, it made a fixed child relative to it (instead of the viewport). So my fixed elements left and top properties were now relative to the container.

    So I did some research, and found that the issue was already been covered by Eric Meyer and even if it felt like a "trick", turns out that this is part of the specifications:

    For elements whose layout is governed by the CSS box model, any value other than none for the transform results in the creation of both a stacking context and a containing block. The object acts as a containing block for fixed positioned descendants.

    http://www.w3.org/TR/css3-transforms/

    So, if you apply any transformation to a parent element, it will become the containing block.

    But...

    The problem is that the implementation seems buggy/creative, because the elements also stop behaving as fixed (even if this bit doesn't seem to be part of specification).

    The same behavior will be found in Safari, Chrome and Firefox, but not in IE11 (where the fixed element will still remain fixed).

    Another interesting (undocumented) thing is that when a fixed element is contained inside a transformed element, while its top and left properties will now be related to the container, respecting the box-sizing property, its scrolling context will extend over the border of the element, as if box-sizing was set to border-box. For some creative out there, this could possibly become a plaything :)

    TEST

    0 讨论(0)
  • 2020-11-22 00:21

    I created a jsfiddle to demostrate how this works using transform.

    HTML

    <div class="left">
        Content
    </div>
    <div class="right">
    <div class="fixedContainer">
        X
    </div>
        Side bar
    </div>
    

    CSS

    body {
      margin: 0;
    }
    .left {
      width: 77%;
      background: teal;
      height: 2000px;
    }
    .right {
      width: 23%;
      background: yellow;
      height: 100vh;
      position: fixed;
      right: 0;
      top: 0;
    }
    .fixedContainer {
        background-color:#ffffd;
        position: fixed;
        padding: 2em;
        //right: 0;
        top: 0%;
        transform: translateX(-100px);
    }
    

    jQuery

    $('.fixedContainer').on('click', function() {
        $('.right').animate({'width': '0px'});
      $('.left').animate({'width': '100%'});
    });
    

    https://jsfiddle.net/bx6ktwnn/1/

    0 讨论(0)
  • 2020-11-22 00:23

    I have the same issue, one of our team members gives me a solution. To allowed the div fix position and relative to other div, our solution is to use a father container wrap the fix div and scroll div.

    <div class="container">
      <div class="scroll"></div>
      <div class="fix"></div>
    </div>
    

    css

    .container {
      position: relative;
      flex:1;
      display:flex;
    }
    
    .fix {
      prosition:absolute;
    }
    
    0 讨论(0)
  • 2020-11-22 00:25

    Two HTML elements and pure CSS (modern browsers)

    See this jsFiddle example. Resize and see how the fixed elements even move with the floated elements they are in. Use the inner-most scroll bar to see how the scroll would work on a site (fixed elements staying fixed).

    As many here have stated, one key is not setting any positional settings on the fixed element (no top, right, bottom, or left values).

    Rather, we put all the fixed elements (note how the last box has four of them) first in the box they are to be positioned off of, like so:

    <div class="reference">
      <div class="fixed">Test</div>
      Some other content in.
    </div>
    

    Then we use margin-top and margin-left to "move" them in relation to their container, something like as this CSS does:

    .fixed {
        position: fixed;
        margin-top: 200px; /* Push/pull it up/down */
        margin-left: 200px; /* Push/pull it right/left */
    }
    

    Note that because fixed elements ignore all other layout elements, the final container in our fiddle can have multiple fixed elements, and still have all those elements related to the top left corner. But this is only true if they are all placed first in the container, as this comparison fiddle shows that if dispersed within the container content, positioning becomes unreliable.

    Whether the wrapper is static, relative, or absolute in positioning, it does not matter.

    0 讨论(0)
  • 2020-11-22 00:25

    Another weird solution to achieve a relative fixed position is converting your container into an iframe, that way your fixed element can be fixed to it's container's viewport and not the entire page.

    0 讨论(0)
提交回复
热议问题