By inserting a new DIV into the #box1
-Container, this DIV will obtain the available width - and reading its width returns a value that appears to be correct.
HTML and CSS both remain unchanged
JS Becomes
var helperDiv = $('<div />');
$('#box1').append(helperDiv);
$('#iw').text("inner width is: " + helperDiv.width());
helperDiv.remove();??????????????????????????? // because we dont need it anymore, do we?
I also forked your Fiddle to see what I have done: http://jsfiddle.net/xc9xQ/2/
So to get this value with a function:
function widthWithoutScrollbar(selector) {
var tempDiv = $("<div/>");
$(selector).append(tempDiv);
var elemwidth = tempDiv.width();
tempDiv.remove();
return elemwidth;
};
//Example
console.log($('#page').width()); // 1280
console.log(widthWithoutScrollbar('#page')); // 1265
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…