Fixed position but relative to container

后端 未结 25 1637
独厮守ぢ
独厮守ぢ 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:45

    The answer is yes, as long as you don't set left: 0 or right: 0 after you set the div position to fixed.

    http://jsfiddle.net/T2PL5/85/

    Checkout the sidebar div. It is fixed, but related to the parent, not to the window view point.

    body {
      background: #ccc;
    }
    
    .wrapper {
      margin: 0 auto;
      height: 1400px;
      width: 650px;
      background: green;
    }
    
    .sidebar {
      background-color: #ffffd;
      float: left;
      width: 300px;
      height: 100px;
      position: fixed;
    }
    
    .main {
      float: right;
      background-color: yellow;
      width: 300px;
      height: 1400px;
    }
    <div class="wrapper">wrapper
      <div class="sidebar">sidebar</div>
      <div class="main">main</div>
    </div>

    0 讨论(0)
  • 2020-11-22 00:45
    /* html */
    
    /* this div exists purely for the purpose of positioning the fixed div it contains */
    <div class="fix-my-fixed-div-to-its-parent-not-the-body">
    
         <div class="im-fixed-within-my-container-div-zone">
              my fixed content
         </div>
    
    </div>
    
    
    
    /* css */
    
    /* wraps fixed div to get desired fixed outcome */
    .fix-my-fixed-div-to-its-parent-not-the-body 
    {
        float: right;
    }
    
    .im-fixed-within-my-container-div-zone
    {
        position: fixed;
        transform: translate(-100%);
    }
    
    0 讨论(0)
  • 2020-11-22 00:46

    I did something like that awhile back. I was pretty new to JavaScript, so I'm sure you can do better, but here is a starting point:

    function fixxedtext() {
        if (navigator.appName.indexOf("Microsoft") != -1) {
            if (document.body.offsetWidth > 960) {
                var width = document.body.offsetWidth - 960;
                width = width / 2;
                document.getElementById("side").style.marginRight = width + "px";
            }
            if (document.body.offsetWidth < 960) {
                var width = 960 - document.body.offsetWidth;
                document.getElementById("side").style.marginRight = "-" + width + "px";
            }
        }
        else {
            if (window.innerWidth > 960) {
                var width = window.innerWidth - 960;
                width = width / 2;
                document.getElementById("side").style.marginRight = width + "px";
            }
            if (window.innerWidth < 960) {
                var width = 960 - window.innerWidth;
                document.getElementById("side").style.marginRight = "-" + width + "px";
            }
        }
        window.setTimeout("fixxedtext()", 2500)
    }
    

    You will need to set your width, and then it gets the window width and changes the margin every few seconds. I know it is heavy, but it works.

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

    The magic is to take the screen width minus the container width and divide it by 2:

    //1400px is according to container max-width (left can be also right)
    .fixed {
      position: fixed;
      right: calc((100vw - 1400px)/2);
    }
    

    Note: css calc() function is almost, but not 100% supported. For most use-cases it is definitely supported enough. Click here for more details

    Snippet (with a 300px container just to fit this website's widget):

    .container {
      max-width: 300px;
      margin-left: auto;
      margin-right: auto;
    }
    
    
    .fixed {
      position: fixed;
      right: calc((100vw - 300px)/2);
    }
    
    
    @media screen and (max-width: 300px) {
      right: 0px;
    }
    <div style="height: 3000px">
        <div class="container">
            <button class="fixed">
                FIXED CONTENT
            </button>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Laborum, eum? Animi quidem accusamus minima vel, dolorem suscipit quia accusantium minus harum modi commodi distinctio! Iste voluptatum earum quam voluptate culpa ad, ipsum dolorum recusandae quis atque eligendi necessitatibus magnam nisi dolores beatae qui? Perspiciatis natus cum nostrum in quod odio sapiente doloremque rerum quo dolore tenetur facere, quisquam atque accusamus fugiat eligendi, deleniti nisi minus recusandae distinctio dignissimos! Dicta quos ipsum qui pariatur at vel veritatis veniam quisquam minus modi et voluptas aliquam laborum, cumque in quo magnam sapiente. Expedita ut dolore laboriosam tempora reprehenderit vero eaque blanditiis, cumque voluptatibus, velit nemo, veniam tenetur quisquam numquam adipisci quae odio repellendus neque incidunt! Cum odio corporis soluta voluptate nesciunt, quasi nobis deleniti neque porro expedita fugiat placeat alias autem pariatur animi error, dicta veritatis atque perspiciatis inventore tempora dolor ad! Mollitia in dolorem ipsam eos porro magni perspiciatis possimus maiores, itaque facere ut. Eos culpa eum error quia incidunt repellendus quam possimus, asperiores earum ipsum molestias dicta sint fugit atque veniam dolorum illo? Officia harum sint incidunt totam, reiciendis illum eos maxime sequi neque repellat quis, expedita eum, corporis quaerat nemo qui soluta aspernatur animi. Sint ad rem numquam omnis sit.
        </div>
     </div>

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

    You can give a try to my jQuery plugin, FixTo.

    Usage:

    $('#mydiv').fixTo('#centeredContainer');
    
    0 讨论(0)
  • 2020-11-22 00:47

    My project is .NET ASP Core 2 MVC Angular 4 template with Bootstrap 4. Adding "sticky-top" into main app component html (i.e. app.component.html) on the first row worked, as follows:

    <div class='row sticky-top'>
        <div class='col-sm-12'>
            <nav-menu-top></nav-menu-top>
        </div>
    </div>
    <div class="row">
        <div class='col-sm-3'>
            <nav-menu></nav-menu>
        </div>
        <div class='col-sm-9 body-content'>
            <router-outlet></router-outlet>
        </div>
    </div>

    Is that the convention or did I oversimplify this?

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