Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. jQuery.fn.extend({
  2.         position: function() {
  3.                 if ( !this[0] ) {
  4.                         return null;
  5.                 }
  6.  
  7.                 var elem = this[0],
  8.  
  9.                 // Get *real* offsetParent
  10.                 offsetParent = this.offsetParent(),
  11.  
  12.                 // Get correct offsets
  13.                 offset       = this.offset(),
  14.                 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
  15.  
  16.                 // Subtract element margins
  17.                 // note: when an element has margin: auto the offsetLeft and marginLeft
  18.                 // are the same in Safari causing offset.left to incorrectly be 0
  19.                 offset.top  -= parseFloat( jQuery.curCSS(elem, "marginTop",  true) ) || 0;
  20.                 offset.left -= parseFloat( jQuery.curCSS(elem, "marginLeft", true) ) || 0;
  21.  
  22.                 // Add offsetParent borders
  23.                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], "borderTopWidth",  true) ) || 0;
  24.                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], "borderLeftWidth", true) ) || 0;
  25.  
  26.                 // Subtract the two offsets
  27.                 return {
  28.                         top:  offset.top  - parentOffset.top,
  29.                         left: offset.left - parentOffset.left
  30.                 };
  31.         },
  32.  
  33.         offsetParent: function() {
  34.                 return this.map(function() {
  35.                         var offsetParent = this.offsetParent || document.body;
  36.                         while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
  37.                                 offsetParent = offsetParent.offsetParent;
  38.                         }
  39.                         return offsetParent;
  40.                 });
  41.         }
  42. });