简体   繁体   English

定位父元素以使其子元素在屏幕上居中

[英]Positioning a parent element to center it's child on the screen

I'm creating a panable and zoomable UI that requires a child object to be centered on the screen however i need to move the parent object to center the child object on screen. 我正在创建一个可平移和可缩放的UI,该UI要求子对象在屏幕上居中,但是我需要移动父对象以将子对象在屏幕上居中。

I have the code working at 1x magnification but when i apply a css3 transform to zoom the parent in or out my calculations go awry. 我的代码以1x放大倍数工作,但是当我应用css3变换来放大或缩小父代时,我的计算会出错。

If you change the 1st and 4th elements of the parent elements css transform and the js mult var to say 0.5 or 2 then the calculations are out. 如果将父元素css transform和js mult var的第1个和第4个元素更改为0.5或2,则计算将不可用。

Any help would be appreciated as i have been pulling my hair out over this for days. 任何帮助将不胜感激,因为我在这几天里一直在拔头发。

JS fiddle JS小提琴

Javascript 使用Javascript

$(function() {

$("#child").click(function(e) {

var mult = 1

$("#parent").css('left',
  (($(window).width() - $("#parent").width()) / 2) +
  ($("#parent").width() * mult) / 2 -
  ($("#child").position().left / mult) -
  ($("#child").width() * mult) / 2
);

$("#parent").css('top',
  (($(window).height() - $("#parent").height()) / 2) +
  ($("#parent").height() * mult) / 2 -
  ($("#child").position().top / mult) -
  ($("#child").height() * mult) / 2
);

})

});

CSS CSS

.parent {
  position: relative;
}

#child {
  border: 1px solid red;
  position: absolute;
  z-index: 3;
  width: 150px;
  height: 150px;
  top: 300px;
  left: 230px;
  background-color: cornflowerblue;
  color: white
}

#parent {
  position: relative;
  width: 700px;
  height: 900px;
  transform: matrix(1, 0, 0, 1, 0, 0);
  border: 1px solid red;
  background: pink
}

HTML HTML

<div class="parent">
  <div id="parent">
    <div class="testBox" id="child">CLICK ME</div>
  </div>
</div>

Ok, I don't usually write JS, but I got it working: 好的,我通常不写JS,但是我可以正常工作:

$(function() {

  $("#child").click(function(e) {

    var mult = 3

    $("#parent").css('left',
        (($(window).width()-$("#child").width()*mult) / 2) -
        $("#child").position().left +
        (mult-1) * $("#parent").width() / 2
    );

    $("#parent").css('top',
        (($(window).height()-$("#child").height()*mult) / 2) -
        $("#child").position().top +
        (mult-1) * $("#parent").height() / 2
    );
  })
});

Here are the things that I think might have made it difficult for you (they did for me): 以下是我认为可能使您感到困难的事情(对我来说很困难):

  1. $("#child").position() gives the position after the transform whilst $("#child").width() gives the width before the transform. $("#child").position()给出转换的位置$("#child").width()给出转换的宽度。 that is, with the CSS you posted but with transform: matrix(2, 0, 0, 2, 0, 0); 也就是说,使用您发布的CSS并进行transform: matrix(2, 0, 0, 2, 0, 0); then $("#child").position().left == 460 but $("#child").width() == 150 . 然后$("#child").position().left == 460$("#child").width() == 150

  2. When setting $("#parent").css('left', ... ) it is the left offset before the transform and the transform scales around the center. 设置$("#parent").css('left', ... )它是转换前的左偏移,并且转换围绕中心缩放。 that is, with the CSS you posted but with transform: matrix(1.5, 0, 0, 1.5, 0, 0); 也就是说,使用您发布的CSS并进行transform: matrix(1.5, 0, 0, 1.5, 0, 0); if you would want to set #parent 's left edge to be aligned with the left edge of the window then you have to set $("#parent").css('left', 175) and not $("#parent").css('left', 0) as I initially expected. 如果要设置#parent的左边缘与窗口的左边缘对齐,则必须设置$("#parent").css('left', 175)而不是$("#parent").css('left', 0)就像我最初期望的那样。

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

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