Fluid width fixed position

后端 未结 7 1740
清酒与你
清酒与你 2021-02-18 22:32

Imagine:

Where:

  • .outer is
7条回答
  •  -上瘾入骨i
    2021-02-18 22:52

    Fixed elements take only absolute values as width. If your parent container is fluid (width is a percentage), you need to set the width of the fixed element dynamically. You need to get the width of the wrapping container and set it on the sticky element.

    CSS

    .outer {width: 25%;}
    .inner {position: fixed;}
    

    JS

    var fixedWidth = $('.outer').css('width');
    $('.inner').css('width', fixedWidth);
    

    Additionally, you can add an event listener in case window resizes.

    JS

    window.addEventListener('resize', resize);
    function resize() {
        var fixedWidth = $('.outer').css('width');
        $('.inner').css('width', fixedWidth);
    }
    

提交回复
热议问题