How can I prevent the scrollbar overlaying content in IE10?

前端 未结 6 1847
执笔经年
执笔经年 2020-11-28 17:41

In IE10, the scrollbar is not always there... and when it appears it comes on as an overlay... It\'s a cool feature but I would like to turn it off for my specific website a

6条回答
  •  半阙折子戏
    2020-11-28 18:18

    SOLUTION: Two steps - detect if IE10, then use CSS:

    do this on init:

    if (/msie\s10\.0/gi.test(navigator.appVersion)) {
        $('body').addClass('IE10');
    } else if (/rv:11.0/gi.test(navigator.appVersion)) {
        $('body').addClass('IE11');
    }
    
    // --OR--
    
    $('body').addClass(
      /msie\s10\.0/gi.test(navigator.appVersion) ? 'IE10' :
      /rv:11.0/gi.test(navigator.appVersion)     ? 'IE11' :
      ''  // Neither
    );
    
    // --OR (vanilla JS [best])--
    
    document.body.className +=
      /msie\s10\.0/gi.test(navigator.appVersion) ? ' IE10' :
      /rv:11.0/gi.test(navigator.appVersion)     ? ' IE11' :
      '';  // Neither
    

    Add this CSS:

    body.IE10, body.IE11 {
        overflow-y: scroll;
        -ms-overflow-style: scrollbar;
    }
    

    Why it works:

    • The overflow-y:scroll permanently turns on the tag vertical scrollbar.
    • The -ms-overflow-style:scrollbar turns off the auto-hiding behavior, thus pushing the content over and giving us the scrollbar layout behavior we're all used to.

    Updated for users asking about IE11. - Reference https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537503(v=vs.85)

提交回复
热议问题