Absolutize for jQuery

We’re using jQuery for one of our current projects. Today I found myself in an IE situation that could be solved by using the prototype librarie’s absolutize method. I couldn’t find any equivilent implementation that I liked in jQuery so I went ahead and ported absolutize from prototype to jquery.

Download Here

It can be used in the standard jQuery way, like so:

$('some-selector').absolutize()

Here’s the code:

jQuery.fn.absolutize = function()
{
  return this.each(function()
  {
    var element = jQuery(this);
    if (element.css('position') == 'absolute')
    {
      return element;
    }

    var offsets = element.offset();
    var top = offsets.top;
    var left = offsets.left;
    var width = element[0].clientWidth;
    var height = element[0].clientHeight;

    element._originalLeft = left - parseFloat(element.css("left") || 0);
    element._originalTop = top - parseFloat(element.css("top") || 0);
    element._originalWidth = element.css("width");
    element._originalHeight = element.css("height");

    element.css("position", "absolute");
    element.css("top", top + 'px');
    element.css("left", left + 'px');
    element.css("width", width + 'px');
    element.css("height", height + 'px');
    return element;

  });
}
This entry was posted in Everything Else and tagged , . Bookmark the permalink.