Skip to content

Instantly share code, notes, and snippets.

@wheresrhys
Created June 20, 2013 14:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wheresrhys/5823198 to your computer and use it in GitHub Desktop.
Save wheresrhys/5823198 to your computer and use it in GitHub Desktop.
Adds outer/innerHeight/Width methods to Zepto Adapted from https://gist.github.com/alanhogan/3935463
(function($) {
// Add inner and outer width to zepto (adapted from https://gist.github.com/alanhogan/3935463)
var ioDim = function(dimension, includeBorder) {
return function (includeMargin) {
var sides, size, elem;
if (this) {
elem = this;
size = elem[dimension]();
sides = {
width: ["left", "right"],
height: ["top", "bottom"]
};
sides[dimension].forEach(function(side) {
size += parseInt(elem.css("padding-" + side), 10);
if (includeBorder) {
size += parseInt(elem.css("border-" + side + "-width"), 10);
}
if (includeMargin) {
size += parseInt(elem.css("margin-" + side), 10);
}
});
return size;
} else {
return null;
}
}
};
["width", "height"].forEach(function(dimension) {
var Dimension = dimension.substr(0,1).toUpperCase() + dimension.substr(1);
$.fn["inner" + Dimension] = ioDim(dimension, false);
$.fn["outer" + Dimension] = ioDim(dimension, true);
});
})(Zepto);
@Goodwine
Copy link

Hey @wheresrhys

I think you should update Lines #15-17 to:

// Note the !, and the -
if (!includeBorder) {
    size -= parseInt(elem.css("border-" + side + "-width"), 10);
}

That's because the $(elem).width() and $(elem).height() functions already include the border-width in the returning value, so you want to subtract it instead of adding it.

@TassosD
Copy link

TassosD commented Dec 21, 2013

@Goodwine:
This is not true my friend. Please check jQuery#width here: http://api.jquery.com/width/
Playing with the demo on the page will enlighten you :)

@EloB
Copy link

EloB commented Apr 27, 2015

This polyfill doesn't support box-sizing.

@emkayy
Copy link

emkayy commented Apr 12, 2017

How would I add support for box-sizing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment