//元素到顶部左侧的距离 elem: DOM对象 type:top,left function getElementDistance (elem,type){ var offset; switch(type){ case 'top': offset='offsetTop'; break; case 'left': offset='offsetLeft'; break; } var elemDis=elem[offset];//获得elem元素距相对定位的父元素的top left var elem=elem.offsetParent;//获取相对定位的父元素 //elem!=null 只要还有相对定位的父元素 while(elem.className != 'comment-wrap'){ elemDis+=elem[offset]; elem=elem.offsetParent; } return elemDis; }