.outerWidth([includeMargin])
- returns the width of the element along with left and right indentation, border and (optionally) mark in pixels.
Setting the [includeMargin]
parameter to true
adds fields to this number:
.outerWidth(true)
- returns the width of the element along with the left and right padding, border, and mark , in pixels.
.width()
- returns the pixel value without a unit , not including the left and right padding, border or margin.
According to jQuery docs, the only difference between .width()
and .css(width)
is that the latter includes units ('px'), where width()
just returns a number (without 'PV').
So, if by definition width()
does not contain fields, indents and borders, then css(width)
cannot
I have a jQuery script, this sets the element width to 50% through the normal CSS width attribute.
This parameter sets the element itself (not including margin, padding or borders) to 50% of this container. If the window is a container, the width of the element will be 50% of this.
Example:. With a window width of 1160 pixels, your script will create an element with 50% of 1160 or 580 pixels wide (excluding margins, indents or borders).
If I then print the outerWidth(true)
this element to get the exact value of the pixel, sometimes it doesn't match $(window).width()/2
.
When you look at the same element with outerWidth(true)
, now you add a marker, padding or borders to the number.
Using my example:. With a window of 1160 pixels, $(window).width()/2
will be 580. Assuming your element also has 580 pixels, .outerWidth(true)
will add (or subtract) the fields, add a border and add padding to the element itself so that you did not receive 580 if the fields, padding and border are not equal to zero.
Therefore, naturally, with a different “theme” (using your definition), the element will probably have different additions, borders and fields. I can’t say for sure, since I don’t see any code.
Just use .width()
instead of .outerWidth(true)
so that everything is consistent across all your themes.
EDIT:
In the Safari console, I see a margin on the body
that calls what you are describing. Inside your consolidated-screen-2.css
, you have a 20x left margin.
You must take this body margin into account when doing your calculations.
Since your JavaScript is not dynamically updated, and it is not in jsFiddle, I cannot check it. When I set field 0 to the DOM, I cannot call JS to recount the numbers without reloading the page, which of course wipes my DOM over-ride.
One solution would be to stick with .width()
(ignore fields) in all your calculations.
An alternative solution would seem to include setting the body
marker to zero.
EDIT 2:
You constantly say that you report errors, but I have a translucent pixel line utility superimposed on my screen for the next, and everything is checked ...
This page is in a window with a width of 1228 pixels. The ruler confirms this.
console.log('width = ' + $slider.outerWidth(true));
= 1188
1188 plus 40 pixels ( body
fields left / right) = 1228 (same as ruler)
The box is open, and the ruler checks that it is exactly 614 pixels wide (without the right border).
console.log('width = ' + $slider.css('width'));
= 594 px;
594 plus 20 pixels ( body
left margin) = 614 (same as ruler)
console.log('width = ' + $slider.width());
= 594
594 plus 20 pixels ( body
left margin) = 614 (same as ruler)