admin管理员组

文章数量:1336659

jQuery's .width() method doesn't seem to account for scroll bars. This is problematic for me, since I'd like to set the width of some children to equal the width of their parent. I used jQuery similar to the following:

$('#contentDiv').width($('#containerDiv').width())

In this example, #contentDiv is the element I'd like to size, and I want to set it to have the width of #containerDiv, which is its parent element. My problem is that this cuts off the side of #contentDiv, as seen in this fiddle.

In my actual code, I have several elements that I'm sizing with jQuery, which all need to fit in the scrollable div, so just setting the css of #contentDiv to 100% is not an option. What's the best way of dealing with scroll bar widths of divs in jQuery?

jQuery's .width() method doesn't seem to account for scroll bars. This is problematic for me, since I'd like to set the width of some children to equal the width of their parent. I used jQuery similar to the following:

$('#contentDiv').width($('#containerDiv').width())

In this example, #contentDiv is the element I'd like to size, and I want to set it to have the width of #containerDiv, which is its parent element. My problem is that this cuts off the side of #contentDiv, as seen in this fiddle.

In my actual code, I have several elements that I'm sizing with jQuery, which all need to fit in the scrollable div, so just setting the css of #contentDiv to 100% is not an option. What's the best way of dealing with scroll bar widths of divs in jQuery?

Share Improve this question edited Apr 12, 2013 at 16:34 Jeffrey Blake 9,7096 gold badges46 silver badges67 bronze badges asked Apr 12, 2013 at 16:33 ckerschckersch 7,6872 gold badges41 silver badges48 bronze badges 5
  • There is an issue with that, because different browsers implement the scroll bars differently. IE puts the scroll bar outside of the area of the div (which is actually correct according to the standards documentation), whereas all of the other browsers actually have the scroll bar inside of that area. Just something to keep in mind when researching this area. – krillgar Commented Apr 12, 2013 at 16:36
  • 1 possible duplicate of jquery - how to get screen width without scrollbar? (use innerWidth())? – Fabrizio Calderan Commented Apr 12, 2013 at 16:36
  • this is a messy subject because it's not consistent across all browsers. Some will give you an innerwidth, some will give you the width with scrollbars. I think you may need to e up with a solution that is specific to each browser you plan to support – Dylan Hayes Commented Apr 12, 2013 at 16:38
  • @FabrizioCalderan: No, that question is about the window. This question is about an element. – Michael Scheper Commented Oct 24, 2014 at 4:41
  • @jeffrey-blake and ckersch: I suggest you edit the title to make it clear this question is about elements, and not the browser width, to make its distinction clearer to users like FabrizioCalderan. – Michael Scheper Commented Oct 25, 2014 at 0:21
Add a ment  | 

4 Answers 4

Reset to default 1

The best solution I found while working around this solution is this:

http://chris-spittles.co.uk/?p=531

jQuery is all powerful and everything but sometimes a small dash of native JS is all you need to render pixel perfect pages... I hope you will find this solution helpful!

UPDATED:

None of the jQuery width-finding methods account for the scroll bar. In my original example, using .innerWidth(true) LOOKS like it works, but only because it returns and object, which causes width to fail and the inner contents size themselves to fit in the available space, because the example wasn't very good. However, it's possible to write a function to pute the available space in a div with a scroll bar in it, which can then be used to position the contents as you wish.

To write that function, I took advantage of the fact that, when a div is appended to a div with a scroll bar in it, it takes up the full available width (i.e. the inner width of the parent minus the width of the scroll bar).

The function looks like this:

function noScrollWidth(div){
    var measureDiv = $('<div id="measureDiv">');

    div.append(measureDiv);

    var width = measureDiv.outerWidth();

    measureDiv.remove();

    return width
};

I then use this to size my content div:

$('#contentDiv').width(noScrollWidth($('#containerDiv')));

Working fiddle.

Try this:

$('#contentDiv').width($('#containerDiv')[0].clientWidth)

For more information about that solution, see this StackOverflow answer.

Another approach I'd try is setting both elements' box-sizing property to 'border-box', and see whether setting your contentDiv's width to 100% then works the way you want.

Now that fewer projects worry about crufty old browsers anymore, 'border-box' can make things easier to work with. Be sure to test multiple browsers on multiple platforms, though, because I'm not sure they all handle scrollbars the same way.

本文标签: javascriptDealing with scroll bars and jquery width() methodStack Overflow