Friday, October 26, 2012

In IE Jquery offset().top not returning correct value.

In IE Jquery offset().top not returning correct value.

There is some issue with Jquery offset().top calculation for any element if the browser is IE.
To fix this issue you can use the native java script for calculating the element offset top and left, and use it accordingly. Here-under the java script method that returns the object for calculated top and left for any element in document and it will work for all browser:

function findElementTotalOffset(obj) {
       var oleft = otop = 0;
       if (obj.offsetParent) {
         do {
           oleft += obj.offsetLeft;
           otop += obj.offsetTop;
         }while (obj = obj.offsetParent);
       }
       return {left : oleft, top : otop};
    }

Example: var jscripteltTop = findTotalOffset(elt).top;
where elt is the element of the document.