Javascript 元素的绝对位置是左还是右,取决于它所在的位置?

Javascript 元素的绝对位置是左还是右,取决于它所在的位置?,javascript,jquery,html,css,absolute,Javascript,Jquery,Html,Css,Absolute,如何分配绝对左0px或绝对右0px取决于绝对定位的div是否会超出其容器div 我想一个简单的例子来说明我的意思:在浏览器中单击鼠标右键,看到它的菜单位置就在你单击的地方的右边,而不是一直到页面的右边,而是在页面的内部,所以它仍然可见 示例:(将鼠标悬停在框上) 首先,如果容器div的位置设置小于位置:绝对,则右:0px或左:0px将相对于容器的位置进行定位。否则,它将定位到从所需div向上树的第一个parentNode,该div有一个位置,如果没有找到,它将相对于主体。因此,您可以搜索具有位置

如何分配绝对左0px或绝对右0px取决于绝对定位的div是否会超出其容器div


我想一个简单的例子来说明我的意思:在浏览器中单击鼠标右键,看到它的菜单位置就在你单击的地方的右边,而不是一直到页面的右边,而是在页面的内部,所以它仍然可见

示例:(将鼠标悬停在框上)

首先,如果容器div的位置设置小于
位置:绝对,则右:0px
左:0px
将相对于容器的位置进行定位。否则,它将定位到从所需div向上树的第一个parentNode,该div有一个位置,如果没有找到,它将相对于主体。因此,您可以搜索具有位置集的第一个父容器或祖父母容器。这个问题很难理解,所以如果你想分享一些例子,我们很乐意提供帮助

编辑:


在您发布的示例中,它与我的评论中的一样,计算父对象的offsethWidth和offsetWidth+left,使其不会溢出,如果它是减少左侧或只是删除左侧并设置右侧定位。为了对宽度和高度产生相同的影响,您必须对拐角进行一些处理。

首先,如果容器div设置了一个位置,那么右:0px或左:0px将相对于容器的位置进行定位。否则,它将定位到从所需div向上树的第一个parentNode,该div有一个位置,如果没有找到,它将相对于主体。因此,您可以搜索具有位置集的第一个父容器或祖父母容器。这个问题很难理解,所以如果你想分享一些例子,我们很乐意提供帮助

编辑:


在您发布的示例中,它与我的评论中的一样,计算父对象的offsethWidth和offsetWidth+left,使其不会溢出,如果它是减少左侧或只是删除左侧并设置右侧定位。为了在宽度和高度上获得相同的效果,您必须为角点设置一些示例。

听起来您需要使用脚本来处理“取决于绝对定位的div是否超出其容器div”位,即支持css表达式,但您可能需要跨浏览器解决方案

也就是说,这应该是一件简单的事情

function isOverflow(parent, child){
    var left = 0;
    var op = child;
    while(op && op != parent){
        left += op.offsetLeft;
        op = op.offsetParent;
    }

    return ((left + child.offsetWidth) > parent.offsetWidth);

}

function getHoverHandler(parent, child){
    return function(){
        if(isOverflow(parent, child)){
            child.style.marginLeft = 'auto';
            child.style.right = '0px';
            child.style.left = '';
        }
    }
}

function attach(o,e,f){
    if(o.addEventListener){
        o.addEventListener(e, f, false);
    }else if(o.attachEvent){
        o.attachEvent('on'+e,f);
    }
}

var yellowElement = document.getElementsByTagName('UL')[0];
var list= document.getElementsByTagName('LI');
for(var i = 0; i < list.length; i++){
    var element = list[i];
    var tip = element.getElementsByTagName('DIV')[0];
    attach(element, 'mouseover', getHoverHandler(yellowElement,tip));

}
函数isOverflow(父、子){
左向量=0;
var op=儿童;
while(op&&op!=父级){
左+=操作偏移左;
op=op.offsetParent;
}
返回((左+子.offsetWidth)>父.offsetWidth);
}
函数getHoverHandler(父、子){
返回函数(){
if(isOverflow(父级、子级)){
child.style.marginLeft='auto';
child.style.right='0px';
child.style.left='';
}
}
}
功能连接(o、e、f){
如果(o.addEventListener){
o、 addEventListener(e、f、false);
}否则,如果(o.attachEvent){
o、 附件('on'+e,f);
}
}
var yellowElement=document.getElementsByTagName('UL')[0];
var list=document.getElementsByTagName('LI');
对于(变量i=0;i
听起来您需要使用脚本来处理“取决于绝对定位的div是否超出其容器div”位,即支持css表达式,但您可能需要跨浏览器解决方案

也就是说,这应该是一件简单的事情

function isOverflow(parent, child){
    var left = 0;
    var op = child;
    while(op && op != parent){
        left += op.offsetLeft;
        op = op.offsetParent;
    }

    return ((left + child.offsetWidth) > parent.offsetWidth);

}

function getHoverHandler(parent, child){
    return function(){
        if(isOverflow(parent, child)){
            child.style.marginLeft = 'auto';
            child.style.right = '0px';
            child.style.left = '';
        }
    }
}

function attach(o,e,f){
    if(o.addEventListener){
        o.addEventListener(e, f, false);
    }else if(o.attachEvent){
        o.attachEvent('on'+e,f);
    }
}

var yellowElement = document.getElementsByTagName('UL')[0];
var list= document.getElementsByTagName('LI');
for(var i = 0; i < list.length; i++){
    var element = list[i];
    var tip = element.getElementsByTagName('DIV')[0];
    attach(element, 'mouseover', getHoverHandler(yellowElement,tip));

}
函数isOverflow(父、子){
左向量=0;
var op=儿童;
while(op&&op!=父级){
左+=操作偏移左;
op=op.offsetParent;
}
返回((左+子.offsetWidth)>父.offsetWidth);
}
函数getHoverHandler(父、子){
返回函数(){
if(isOverflow(父级、子级)){
child.style.marginLeft='auto';
child.style.right='0px';
child.style.left='';
}
}
}
功能连接(o、e、f){
如果(o.addEventListener){
o、 addEventListener(e、f、false);
}否则,如果(o.attachEvent){
o、 附件('on'+e,f);
}
}
var yellowElement=document.getElementsByTagName('UL')[0];
var list=document.getElementsByTagName('LI');
对于(变量i=0;i
好朋友, 尝试以下步骤

1. You have a container div and on right clicking on it you will need to display a div for example say div with list of menus.
2. Have the left position of the container div in a variable **contLeft** and width of the container in another variable **contWidth**
3. Assign the oncontextmenu event handler on the container div. 
4. In the event handler function take the mouse x postion in a variable **mosX** and mouse y position in a variable **mosY** and you have to fix the top position of the div to be displayed as mosY and the left as mosX. 
5. In order to maintain the div within the container you have to calculate the container's screen occupation as **totPos = (contLeft + contWidth)**
6. Calculate the screen occupation of the menu div as **posMenu = (mosX + width of the div)**
7. If the totPos greater than or equal to posMenu display the menu in the same top and left postion using the values of mosY and mosX
8. Else place the menu in position top = mosY and left = (mosX - width of menu div)
希望这能解决你的问题。

好朋友, 尝试以下步骤

1. You have a container div and on right clicking on it you will need to display a div for example say div with list of menus.
2. Have the left position of the container div in a variable **contLeft** and width of the container in another variable **contWidth**
3. Assign the oncontextmenu event handler on the container div. 
4. In the event handler function take the mouse x postion in a variable **mosX** and mouse y position in a variable **mosY** and you have to fix the top position of the div to be displayed as mosY and the left as mosX. 
5. In order to maintain the div within the container you have to calculate the container's screen occupation as **totPos = (contLeft + contWidth)**
6. Calculate the screen occupation of the menu div as **posMenu = (mosX + width of the div)**
7. If the totPos greater than or equal to posMenu display the menu in the same top and left postion using the values of mosY and mosX
8. Else place the menu in position top = mosY and left = (mosX - width of menu div)

希望这能解决您的问题。

我想我的意思是一个简单的例子:在浏览器中单击鼠标右键,看到它的菜单位置在您单击的右侧,我们不会一直走到页面的右侧,而不是离开页面,“它位于它的内部,所以它仍然可见。@DylanCross first是上下文菜单的内置算法。”。对于这样的操作,您必须在div的offsetWidth offsetHeight、鼠标位置和客户端宽度和高度之间建立一个公式。然后,在计算鼠标当前位置的左侧、右侧顶部或底部是否有足够的空间后,设置“位置固定”。我想一个简单的例子可以说明我的意思:在浏览器中单击鼠标右键,看到它的菜单位置在您单击的位置的右侧,而不是一直到页面的右侧,“它不在页面外,而是留在页面内,因此仍然可见。@DylanCross first是上下文菜单的内置算法。”。对于这样的操作,你必须在div的offsetWi之间建立一个公式