【问题标题】:Need to calculate offsetRight in javascript需要在javascript中计算offsetRight
【发布时间】:2010-06-08 20:09:29
【问题描述】:

我需要计算一个 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;    
}

【问题讨论】:

  • 你想用“offsetRight”做什么? “left”和“top”存在的原因是窗口(或任何容器框,实际上)的顶部和左侧是固定的;视图框拉伸/收缩(从概念上讲)发生在右侧和底部。
  • 我必须在当前对象后面绘制一个 div,而我正在这样做是一种从右到左的语言。我必须知道对象(文本)在右侧结束的位置才能知道 div 右边缘的位置。由于 DOM 中的所有内容都是从左到右工作的,这很令人头疼。

标签: javascript offset


【解决方案1】:

不能比这更简单了:

let offsetright = window.innerWidth - obj.offsetLeft - obj.offsetWidth

【讨论】:

  • 另一种计算offsetRight的方法window.innerWidth - element.getBoundingClientRect().right
【解决方案2】:

几个选择:

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';

【讨论】:

    【解决方案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]")

    【讨论】:

      【解决方案4】:

      如果你有兴趣使用一些 Js 库,那么试试以下原型 js 的功能 http://api.prototypejs.org/dom/element/offset/

      【讨论】:

      • 这只是 offsetLeft 已经做的另一种方式,但仍然没有给我 offsetRight。这有什么帮助?
      猜你喜欢
      • 2012-10-03
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      相关资源
      最近更新 更多