Fixed position but relative to container

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

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

    HTML

    Content
    X
    Side bar

    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/

提交回复
热议问题