Javascript 在javascript中获取div位置(顶部)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10564680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:54:28  来源:igfitidea点击:

Get div position (top) in javascript?

javascriptjqueryhtml

提问by Alexey Ogarkov

One div i set the height in css using top:-26px;. I have other divs other places where i'd like to align with that div. I notice in jquery writing .css('top')gets me my css and not the y coord of the div. How do i get the absolute x,y position using javascript?

一个 div 我使用 .css 在 css 中设置了高度top:-26px;。我在其他地方还有其他 div,我想与该 div 保持一致。我注意到在 jquery 写作中.css('top')我得到了我的 css 而不是 div 的 y 坐标。我如何使用 javascript 获得绝对的 x,y 位置?

回答by rlemon

I will give you the vanilla solution.. don't complain.. add a [0] to your element and it's fixed! :P hope this helps.

我会给你香草的解决方案..不要抱怨..在你的元素中添加一个 [0] 并且它已经修复了!:P 希望这会有所帮助。

function getOffset( el ) {
    var _x = 0;
    var _y = 0;
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
    }
    return { top: _y, left: _x };
}
var x = getOffset( document.getElementById('yourElId') ).left; 

回答by Alexey Ogarkov

You can use jQuery(element).offset().top

您可以使用 jQuery(element).offset().top

Hope it helps.

希望能帮助到你。

回答by VisioN

Try this code:

试试这个代码:

function getElementPosition(id) {
    var offsetTrail = document.getElementById(id);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }

    return {
        left: offsetLeft,
        top: offsetTop
    };
}?

It will return an object with leftand topvariables.

它将返回一个带有lefttop变量的对象。

If you use JQuery try offset()method:

如果您使用 JQuery tryoffset()方法:

var pos = $("#element").offset();
console.log(pos.left)
console.log(pos.top)

回答by Simon Steinberger

Cross browser safe down to IE 8, possibly even 7 or 6:

跨浏览器安全到 IE 8,甚至可能是 7 或 6:

function offset(el) {
    var rect = el.getBoundingClientRect(),
    scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
    scrollTop = window.pageYOffset || document.documentElement.scrollTop;
    return { top: rect.top + scrollTop, left: rect.left + scrollLeft }
}
var offsetEl = offset(document.getElementById('some_id'));
console.log(offsetEl.left, offsetEl.top);

Short and fast and no jQuery needed.

简短而快速,不需要 jQuery。

回答by Heider Sati

var MyElement = document.getElementById("...Your_DIV_Control...");

var top = MyElement.getBoundingClientRect().top;
var MyElement = document.getElementById("...Your_DIV_Control...");

var top = MyElement.getBoundingClientRect().top;

回答by Kalpesh Prajapati

Use jQuery(element).offset()

使用 jQuery(element).offset()

Hope it helps.

希望能帮助到你。

For example

例如

var elementPosition = jQuery(element).offset();
var elementTopPositon = elementPosition.top;

回答by D. Strout

With standard JavaScript:

使用标准 JavaScript:

var elem=document.getElementById("elemID");
var top=elem.offsetTop;
var left=elem.offsetLeft;

With jQuery:

使用 jQuery:

var top=$("#elemID").offset().top;
var left=$("#elemID").offset().left;