9

我需要计算 DOM 对象的 offsetRight。我已经有一些相当简单的代码来获取 offsetLeft,但是没有 javascript offsetRight 属性。如果我添加 offsetLeft 和 offsetWidth,会起作用吗?或者,还有更好的方法?

function getOffsetLeft(obj)
{
    if(obj == null)
        return 0;
    var offsetLeft = 0;
    var tmp = obj;
    while(tmp != null)
    {
        offsetLeft += tmp.offsetLeft;
        tmp = tmp.offsetParent;
    }
    return offsetLeft;
}

function getOffsetRight(obj)
{
    if (obj == null)
        return 0;
    var offsetRight = 0;
    var tmp = obj;
    while (tmp != null)
    {
        offsetRight += tmp.offsetLeft + tmp.offsetWidth;
        tmp = tmp.offsetParent;
    }
    return offsetRight;    
}
4

4 回答 4

8

不能比这更简单:

let offsetright = window.innerWidth - obj.offsetLeft - obj.offsetWidth
于 2020-06-04T02:46:28.767 回答
5

几个选择:

1)如果您想要相对于视口的“offsetRight”,请使用element.getBoundingClientRect().right;

2)您的示例很好,只需从元素的宽度+ offsetLeft 中减去父宽度。

3)最后,相对于文档,并加快遍历(offsetParent):

在此示例中,我将伪下拉元素放置在引用元素下方,但是因为我避免了一些棘手的 z-index 问题,并且希望从右侧引用该元素并从左侧展开,所以我不得不附加它到 body 元素,并从原始父级获取“offsetRight”。

...

// Set helper defaults
dropdownElem.style.left = 'auto';
dropdownElem.style.zIndex = '10';

// Get the elem and its offsetParent
let elem = dropdownElemContainer;
let elemOffsetParent = elem.offsetParent;

// Cache widths
let elemWidth = elem.offsetWidth;
let elemOffsetParentWidth = 0;

// Set the initial offsets
let top = elem.offsetHeight; // Because I want to visually append the elem at the bottom of the referenced bottom
let right = 0;

// Loop up the DOM getting the offsetParent elements so you don't have to traverse the entire ancestor tree
while (elemOffsetParent) {
    top += elem.offsetTop;
    elemOffsetParentWidth = elemOffsetParent.offsetWidth;
    right += elemOffsetParentWidth - (elem.offsetLeft + elemWidth); // Most important line like your own example
    // Move up the DOM
    elem = elemOffsetParent;
    elemOffsetParent = elemOffsetParent.offsetParent;
    elemWidth = elemOffsetParentWidth;
}

// Set the position and show the elem
dropdownElem.style.top = top + 'px';
dropdownElem.style.right = right + 'px';
dropdownElem.style.display = 'block';
于 2018-07-25T19:57:32.707 回答
3
//Object references
function getObject(id) {
   var object = null;
   if (document.layers) {
    object = document.layers[id];
   } else if (document.all) {
    object = document.all[id];
   } else if (document.getElementById) {
    object = document.getElementById(id);
   }
   return object;
}
//Get pixel dimensions of screen
function getDimensions(){
    var winW = 630, winH = 460;
    if (document.body && document.body.offsetWidth) {
     winW = document.body.offsetWidth;
     winH = document.body.offsetHeight;
    }
    if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) {
     winW = document.documentElement.offsetWidth;
     winH = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
     winW = window.innerWidth;
     winH = window.innerHeight;
    }
    return{"width":winW, "height":winH}
}
//Get the location of element
function getOffsetRight(elem){
    element=getObject(elem)
    var width = element.offsetWidth
    var right = 0;
    while (element.offsetParent) { 
        right += element.offsetLeft; 
        element = element.offsetParent;
    }
    right += element.offsetLeft;
    right = getDimensions()["width"]-right
    right -= width
    return right
}

这不是万无一失的,但您通常可以通过调用获取“offsetRight”: getOffsetRight("[object.id]")

于 2013-03-31T03:10:24.890 回答
0

如果您对使用一些 Js 库感兴趣,请尝试以下原型 js 的功能 http://api.prototypejs.org/dom/element/offset/

于 2010-06-08T20:30:07.190 回答