How to reliably get screen width WITH the scrollbar

前端 未结 9 1183
抹茶落季
抹茶落季 2020-12-03 21:38

Is there a way to reliably tell a browser\'s viewport width that includes the scrollbar, but not the rest of browser window)?

None of the properties listed here tell

9条回答
  •  既然无缘
    2020-12-03 22:02

    You can get the window width with scrollbar , that way:

    function scrollbar_width() {
              if (jQuery('body').height() > jQuery(window).height()) {
    
                  /* Modified from: http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php */
                  var calculation_content = jQuery('
    '); jQuery('body').append(calculation_content); var width_one = jQuery('div', calculation_content).innerWidth(); calculation_content.css('overflow-y', 'scroll'); var width_two = jQuery('div', calculation_content).innerWidth(); jQuery(calculation_content).remove(); return (width_one - width_two); } return 0; }

提交回复
热议问题