繁体   English   中英

计算相对内部的居中绝对div

[英]calculate centered absolute div inside relative

我正在使用2个Javscript方法将悬停按钮放在页面上的静态元素中。 中心的按钮在第一个元素内输入并使用绝对位置。 我用来获取父元素测量的代码:

// calculate if the element is in the visible window
function elementVisibleRect(element) {
    element = $(element);
    var rect = {
        top: Math.round(element.offset().top),
        left: Math.round(element.offset().left),
        width: Math.round(element.outerWidth()),
        height: Math.round(element.outerHeight())
    };
    var scrollTop = $(window).scrollTop();
    var windowHeight = $(window).height();
    var scrollBottom = scrollTop + windowHeight;
    var elementBottom = Math.round(rect.height + rect.top);

    if (scrollTop < rect.top && scrollBottom > elementBottom) {
        return rect;
    }
    if (scrollTop > rect.top) {
        rect.top = scrollTop;
    }
    if (scrollBottom < elementBottom) {
        rect.height = scrollBottom - rect.top;
    } else {
        rect.height = windowHeight - (scrollBottom - elementBottom);
    }
    return rect;
}

并使用此信息并使按钮居中

// center the element based on visible screen-frame
function elementPosition (element) {
    var visibleRect = elementVisibleRect(element);
    $('.editHoverButton').css({
        top: visibleRect.top + ((visibleRect.height / 2) - ($('.editHoverButton').outerHeight() / 2)),
        left: visibleRect.left + (visibleRect.width / 2) - ($('.editHoverButton').outerWidth() / 2)
    });
}

现在我的问题是第三方库需要父DIV将位置从浏览器默认“静态”更改为“相对”,这会破坏我在第二个函数中的计算。

可能会迟到,但无论我尝试什么,我似乎都无法弄清楚当父元素的位置设置为相对时如何使其工作。 我似乎无法将数学弄得恰到好处,而且我的头脑开始受伤了。 有什么建议?

编辑 - 添加JSFIDDLE

http://jsfiddle.net/RhTY6/

具有absolute定位的元素从自然流中移除(例如,它们不会留下它们所在的空间) 并且相对于它们的第一父元件以非static定位定位 由于右侧盒子的位置是relative (非static ),您可以将按钮置于top: 50%; left: 50%; 这将使左上角位于父级的中心。 然后你所要做的就是使用margin-topmargin-left从位置减去元素高度和宽度的一半。 这比你正在做的要简单得多,如下所示:

JavaScript的:

function elementPosition() {
    $('.editHoverButton').css('margin-top', 0 - $('.editHoverButton').outerHeight() / 2);
    $('.editHoverButton').css('margin-left', 0 - $('.editHoverButton').outerWidth() / 2);
};

CSS:

.editHoverButton {
  position: absolute;
  z-index: 99;
  font-family: sans-serif;
  font-size: 14px;
  font-weight: normal;
  background-color: #00bb00;
  top: 50%;
  left: 50%;
}

除了从elementPosition()函数中删除this之外,没有其他任何东西需要改变。

DEMO (注意左边的一个不再有效。这是因为它定位为static 。)

编辑 - 使用相同的基本思想,这种方法应该工作:

问题是在定义rect时你已经占据了元素的顶部和左侧位置。 关于定位计算。 将它们更改为0 (不是最好的方法,但它有效)可以解决relative元素的问题。

DEMO (注意左边的一个现在可以工作。这是因为无论如何它都位于0,0。)

编辑 - 这将在页面滚动时起作用:

这会将容器设置为变量,以便在页面滚动时可以自动重新定位。

DEMO

编辑:通过仅更改脚本使其与您的CSS和HTML(相对和绝对定位)一起工作。

水平轴计算完全缺失(我已经应用了您应用于垂直轴的相同计算)。

我已经添加了一些数据和一个标尺来帮助你完成这项工作:正如你所看到的那样(它原来是在你原来的小提琴中)没有完全居中(显然你需要在容器小于时才看它)视口),但这很容易解决。

运行演示

尝试调整小提琴窗口的大小并垂直和水平滚动以查看它的工作原理。

function elementVisibleRect(element) {
    $("#data").html("");

    element = $(element);
    var rect = {
        top: Math.round(element.offset().top),
        left: Math.round(element.offset().left),
        width: Math.round(element.outerWidth()),
        height: Math.round(element.outerHeight())
    };
    var scrollTop = $(window).scrollTop();
    var windowHeight = $(window).height();
    var scrollBottom = scrollTop + windowHeight;
    var elementBottom = Math.round(rect.height + rect.top);

    var scrollLeft = $(window).scrollLeft();
    var windowWidth = $(window).width();
    var scrollRight = scrollLeft + windowWidth;
    var elementRight = Math.round(rect.width + rect.left);


    addData("rect.top", rect.top);
    addData("rect.left", rect.left);
    addData("rect.width", rect.width);
    addData("rect.height", rect.height);
    addData("scrollTop", scrollTop);
    addData("windowHeight", windowHeight);
    addData("scrollBottom", scrollBottom);
    addData("elementBottom", elementBottom);
    addData("scrollLeft", scrollLeft);
    addData("windowWidth", windowWidth);
    addData("scrollRight", scrollRight);
    addData("elementRight", elementRight);


    if (rect.top < scrollTop) {
        rect.top = scrollTop;
    }
    if (scrollBottom < rect.top < scrollTop) {
        rect.top = scrollTop;
    }
    if (scrollBottom < elementBottom) {
        rect.height = scrollBottom - rect.top;
    } else {
        rect.height = windowHeight - (scrollBottom - elementBottom);
    }

    if (rect.left < scrollLeft) {
        rect.left = scrollLeft;
    }
    if (scrollRight < rect.left < scrollLeft) {
        rect.left = scrollLeft;
    }
    if (scrollRight < elementRight) {
        rect.width = scrollRight - rect.left;
    } else {
        rect.width = windowWidth - (scrollRight - elementRight);
    }

    return rect;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM