Prevent a centered layout from shifting its position when scrollbar appears

前端 未结 6 1299
灰色年华
灰色年华 2020-12-03 05:05

My page layout looks something like this:



相关标签:
6条回答
  • 2020-12-03 05:25

    Use jquery and put this in the start of your tag:

    <script type="text/javascript">
    function checkheight(){
     if ($(document).height() > $(window).height()) {
     //that is if there is vertical scrollbar
     document.getElementById('yourcenteredcontainer').style.paddingLeft='8px'; 
     //8px because the scrollbars are (?always?) 16px
     }else{
     document.getElementById('yourcenteredcontainer').style.paddingLeft='0px';
     }
    }
    </script>
    

    and call the function checkheight(); in the end of your tag plus wherever you have onclick (or other) events that make the page longer or shorter in height.

    0 讨论(0)
  • 2020-12-03 05:26

    If your site is "responsive" (reacts to width):

    Step 1: Add width: 100vw to a wrapper div. This makes it as wide as the viewport, ignoring the appearance of a scrollbar.

    Step 2: Add overflow-x: hidden to the body.This will remove the horizontal scrollbar (created when vertical scrollbar appears, to allow the user to "look under" it).

    "wrapper div" is referring to another div around your #content-wrap

    Will work for your case too, tested:

    <style type="text/css">
    body {
        overflow-x: hidden;
    }
    #wrap-wrap {
        width: 100vw;
    }
    #content-wrap
    {
        margin: 0 auto;
        width: 800px;
    }
    </style>
    <div id="wrap-wrap">
        <div id="content-wrap">
        </div>
    </div>
    

    Make sure nothing on your page is wide enough to get caught under the scrollbar.


    If your site is fixed width + centered (your case):

    html {
        margin-left: calc(100vw - 100%);
        margin-right: 0;
    }
    

    This will add a left margin equal in width to the scrollbar when it appears. Which is 0 when it does not. Taken from here, but tested.

    0 讨论(0)
  • 2020-12-03 05:34

    If you can use Javascript, you can set the width of the content-wrap to the inner width of the window minus the standard width of a scrollbar.

    You will run into some problems though.

    • The user will have to have Javascript enabled
    • You don't know what the width of the vertical scrollbar is, especially if the scrollbar isn't there! So you will have to guess. 20px seems like a good guess.
    • Different browsers have different ways of telling you want the inner width of the window is.

    So if you can live with all that, you can do something like this (in pseudo code)

    if window.innerWidth is defined :
       set the width of the div to window.innerWidth-20px
    else if we're running on Internet Explorer :
       set the width to document.documentElement.offsetWidth-20px
    otherwise :
       we're out of luck and we best leave the width as is.
    
    0 讨论(0)
  • 2020-12-03 05:35

    You must use:

    html {
      overflow-y: overlay;
    }
    

    Only supported by WebKit (Safari) or Blink (Chrome, Opera)

    0 讨论(0)
  • 2020-12-03 05:41

    I'm afraid the best way to solve this is to force the scroll bar to be visible at all times with html {overflow-y: scroll;}. The problem you have is that the "available area" shrinks with say 10 px when the scroll bar appear. This cause the calculated margin on your left side to shrink with half the width of the scroll bar, thus shifting the centered content somewhat to the left.

    A possible solution might be to calculate the margin with JavaScript instead of using margin: 0 auto; and somehow compensate for the "lost" pixels when the scroll bar appear, but I'm afraid it will be messy and the content will probably move a little bit anyway while you calculate and apply the new margin.

    0 讨论(0)
  • 2020-12-03 05:45

    First I would recommend optimizing the HTML so that it won't take so long to load/render. If load/render is fast the scrollbar won't appear "too late". What is it that takes long to load/render? Check the network tab in chrome debug tools (F12). Do an audit in Chrome debug tools.

    There are multiple things that could make the document "reflow", and the scrollbar appear even though the browser could have known the necessary measurements right from the start. Are you using tables for layout - don't! They may need multiple passes of rendering. Do you have images without width/height specified? Then the document will need to be rerendered when each image loads. Specify <img ... style="width: ..px; height: ..px">. Is the CSS sane and efficient?

    If you can't get load/rendering speed down I think your best bet is to not use the browser's scrollbar if javascript is enabled. That way you can control it and place it absolutely positioned so that it won't affect horizontal positioning.

    Let your slider start of with display: none. Monitor dom ready event as well as image load events as well as window resize events. When the page has been loaded, images have been loaded and when window gets resized just run the same function every time. It would determine if the scrollbar is needed and either display it or hide it.

    You could use JQuery UI Slider for example and set it's maxValue to $(document).height() - $(window).height(), monitor the slider change event and then scroll the body to the value of the slider and so forth.

    If javascript is disabled the fallback will be the regular scrollbar and there's nothing you can do about the slight horizontal shift then.

    But really I think the problem of the horizontal shift is too small to spend time fixing with a custom scrollbar, and check that it actually works well on all platforms etc. Do HTML/CSS optimizations first.

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