Twitter-bootstrap 3 affixed sidebar overlapping content when window resized and also footer

前端 未结 2 651
天涯浪人
天涯浪人 2021-01-13 05:19

I am having problems with an overlapping sidebar when I use affix for bootstrap 3 and resize the window. Is there a way to add some css class so that instead of overlapping

2条回答
  •  -上瘾入骨i
    2021-01-13 05:37

    I wanted the affix to be responsive when resizing the columns so I came up with a pretty simple solution.

    What we'll do is match the width of our affix to the width of the column it is in when the grid is resized.

    To do this all you need to do is give your column and affix an Id. (Or someway to reference the elements with javascript)

    I went for a javascript solution.

    function fixAffix() {
        try {
    
            // Bootstrap affix will overflow columns 
            // We'll fix this by matching our affix width to our affixed column width
            var bb = document.getElementById('affixColumn').getBoundingClientRect(),
                columnWidth = bb.right - bb.left;
    
            // Make sure affix is not past this width.
            var affixElm = document.getElementById('affixDiv');
            affixElm.style.width = (columnWidth - 30) + "px";
    
            // We can also define the breakpoint we want to stop applying affix pretty easily
            if (document.documentElement.clientWidth < 975) 
                affixElm.className = "";
            else
                affixElm.className = "affix";
    
        }
        catch (err) {
            alert(err);
        }
    }
    
    
    
    $(window).resize(function () {
           fixAffix();
       });
    
    $(document).ready(function () {
        fixAffix();
    });
    

    I've included code to remove the affix at a specified breakpoints. That's specific to my application, but you'll most likely want to do something like this too to stop the affix on smaller devices.

提交回复
热议问题