需要在javascript中计算offsetRight

问题描述 投票:7回答:4

我需要计算一个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;    
}
javascript offset
4个回答
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';

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
}

这不是弹丸之地,但你通常可以通过调用:getOffsetRight("[object.id]")来获取 "offsetRight"。


1
投票

不能再简单了。

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

0
投票

如果你有兴趣使用一些Js库,那么可以试试下面这个原型js的功能。http:/api.prototypejs.orgdomelementoffset。

© www.soinside.com 2019 - 2024. All rights reserved.