两种方法
1.元素.getBoundingClientRect().top 方法 直接返回该元素到浏览器顶端的距离
2.
function getElementTop(el) {
if (el.offsetParent ) {
return this.getElementTop(el.offsetParent ) + el.offsetTop
}
return el.offsetTop
}
如果到页面顶部的距离,需要加上document.body.scrollTop的值