javascript - 中心div(位置:绝对)垂直和水平到屏幕视图

时间:2014-09-06 06:28:44

标签: javascript css

我正在尝试制作一个用于在图像上叠加文本框的用户脚本:它使用带有固定位置的interactjs的可拖动菜单。菜单有一个按钮,我需要它来创建一个20px * 60px的div并将其显示在屏幕视图的中心(所以不要滚动到页面底部并从那里拖动它)。我可以(在某种程度上)使用:

var div = document.getElementById("inserted_div_2");
div.style.position = 'fixed';
div.style.top = '50%'; //relative to screen
div.style.left = '50%';

从那里我可以将它拖动/调整到我想要的位置(也使用interactjs)然后,我怎样才能将它更改为position:absolute所以它滚动内容在图像上保持相同的位置(例如:在img2的左上角)?类似的东西:

var posX = div.getPosX(); //relative to page; in %, px or em
var posY = div.getPosY();
// when I change it from fixed to absolute the div goes back to the bottom of the page
div.style.position = 'absolute';
div.style.left = posX;
div.style.top = posY;

HTML结构如下所示:

<body>
    ...
    <div id="content">
        ...
        <img src="/img1.jpg"> // size and number of imgs is variable.
        <img src="/img2.jpg"> // are in a strip format.
        <img src="/img3.jpg">
        ...
    </div>
    ...
    <div id="overlays">

         //eg: div1 was dragged from the center of screen 
         //into in between img1 and img2
         <div id="inserted_div_1">Text</div>

         //now I need to do the same for div2, 
         //dragging it to the top left corner of img2
         <div id="inserted_div_2">Text</div>

    </div>
</body>

我不想使用jQuery或其他库,但如果它太难,那么我将使用它。 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用offsetTopoffsetLeft来获取相对于页面的元素位置(以px为单位)。

var posX = div.offsetLeft;
var posY = div.offsetTop;
div.style.position = 'absolute';
div.style.left = posX;
div.style.top = posY;

更新

offsetTopoffsetLeft返回的值不包括应用的transform:translate样式。我创建了一个test case - 它不可拖动,但它会显示如何通过添加offsettranslate值来计算相对位置:

var div = document.getElementById("inserted_div_2");
var content = document.getElementById("content");

function testpos(){
    var ol = div.offsetLeft,
        ot = div.offsetTop,
        cs = window.getComputedStyle(div, null),
        tr = cs.getPropertyValue("-webkit-transform") ||
             cs.getPropertyValue("-moz-transform") ||
             cs.getPropertyValue("-ms-transform") ||
             cs.getPropertyValue("-o-transform") ||
             cs.getPropertyValue("transform") ||
             false;
        //outputs something like 'matrix(1, 0, 0, 1, 80, 90)'
        var values = tr.replace(/[^0-9\.\,]/g,'').split(','),//split into array
            tx = values[4] || 0,//take the x value (else 0)
            ty = values[5] || 0;//take the y value (else 0)       
    //
    content.innerHTML+=("<hr />position: "+div.style.position+"<br />");
    content.innerHTML+=("offsetLeft:"+ol+", offsetTop:"+ot+"<br />");
    content.innerHTML+=("translate-x:"+tx+", translate-y:"+ty+"<br />");
    //so the actual position is the offset + the translate ==
    var x = parseInt(ol) + parseInt(tx),
        y = parseInt(ot) + parseInt(ty);
    content.innerHTML+=("x:"+x+" y:"+y+"<br />");
}

/* TEST */
//1 set to fixed
div.style.position = 'fixed';
testpos();//test position
//2 move using transfor:translate
div.style.transform = 'translate(80px,90px)';
testpos();//test position (note the offset does not include the transform)
/3 set to absolute and get the position
div.style.position = 'absolute';
testpos();

http://jsfiddle.net/u3ay74bs/

答案 1 :(得分:0)

你可以垂直和水平地使用css作为中心div

例如

#content{
     position:absolute;
     width:100px;
     height:100px;
     left:50%;
     top:50%;
     margin-left:-50px; /*half width*/
     margin-top:-50px; /*half height*/
}

<div id='content'>
     ...
</div>

如果宽度等于100px margin-left等于-50px 如果宽度等于200px margin-left等于-100px 等等

边距 - 左半边宽 和 边缘 - 上半高

相关问题