事业部的相对位置到顶部视口 [英] Position of Div in relation to the Top of the Viewport

查看:143
本文介绍了事业部的相对位置到顶部视口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎样才能像素从一个div的金额(或一般测量)JavaScript中的窗口的顶部。我不相寻找一个偏移y以文件,只需向其中浏览器显示的顶部。我在这里尝试了回答的解决方案:<一href=\"http://stackoverflow.com/questions/211703/is-it-possible-to-get-the-position-of-div-within-the-browser-viewport-not-with\">http://stackoverflow.com/questions/211703/is-it-possible-to-get-the-position-of-div-within-the-browser-viewport-not-with,但至少在Safari中,我遇到了在那里返回相同数量无论身在何处div的真的是一个问题。

I am wondering how I can get the amount of pixels (or measurement in general) from a div to the top of the window in Javascript. I am not looking for an offset y in relation to the document, simply to the top of where the browser is displaying. I tried the "answered" solution here: http://stackoverflow.com/questions/211703/is-it-possible-to-get-the-position-of-div-within-the-browser-viewport-not-with, but at least in Safari, I am running into a problem where it returns the same number no matter where the div's really are.

感谢您的帮助!

推荐答案

这<一个href=\"http://stackoverflow.com/questions/1896297/how-do-i-calculate-the-width-between-two-elements/1896490#1896490\">answer告诉你如何获得一个元素的x和y COORDS与文档的尊重原点(左上角)。你只需要在.scrollTop值添加到y坐标去视窗顶部从格以像素为单位的距离。

This answer tells you how to get the x and y coords of an element with respect the origin (top left) of a document. You just need to add the .scrollTop value to the y coord to get the distance in pixels from div to top of viewport.

这篇关于事业部的相对位置到顶部视口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆