简体   繁体   English

根据子元素正确居中div

[英]Properly centering div based on child element

This is a repost from StackExchange's GameDev section, yet, I find that the problem seems more applicable to StackOverflow because it pertains more to CSS, JS, and positioning techniques rather than JavaScript topics (mostly UnityScript and Phaser) discussed on the former. 这是StackExchange的GameDev部分的转贴,但是,我发现该问题似乎更适用于StackOverflow,因为它与CSS,JS和定位技术有关,而不是与前者讨论的JavaScript主题(主要是UnityScript和Phaser)有关。

I've been making a roguelike-style game in HTML5 without using canvas (only divs) with pure JS ( fiddle! ). 我一直在用HTML5制作类似roguelike的游戏而没有使用画布(仅div)纯JS小提琴! )。 I've been trying to enlarge the tile size (font size) while keeping the player centered within the camera. 我一直在尝试扩大图块大小(字体大小),同时保持播放器在相机内居中。 For some reason, when the tile size isn't equal to the map size, the camera will be slightly off. 由于某些原因,当图块大小与地图大小不相等时,相机将稍微关闭。

Note that the 3D effect is on purpose; 请注意,3D效果是故意的; I believe it adds some much needed depth, and just looks super cool. 我相信它增加了一些急需的深度,而且看起来非常酷。 :D :d

When tileScale (line 4 in the fiddle) is 9 (very undesirable; the dots on the player's y axis should be aligned, not at a slight angle): tileScale (小提琴中的第4行)为9 (非常不希望;玩家y轴上的点tileScale ,而不是成一个小角度):

相机关闭!

When tileScale is 25 (on point!): tileScale25 (点!):

相机对准点。

Here's some relevant (trimmed) code: 这是一些相关的(修剪过的)代码:

window.onresize = function(){
    game.viewportWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
    game.viewportHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
    game.windowSize = Math.min(game.viewportWidth, game.viewportHeight);
    // Droid Sans Mono has a ratio of 3:4 (width:height), conveniently.
    // This may be problematic. I'm not sure.
    game.tileWidth = game.windowSize*.6 / game.tileScale;
    game.tileHeight = game.windowSize*.8 / game.tileScale;
}

// Update the camera position (needs help?)
this.updateCamera = function(){
    // Get player coordinates (-.5 because we need to get the player tile center)
    // times the tileWidth plus the game window (inner square) size divided by two.
    var left = ((-game.player.x-.5)*game.tileWidth+game.windowSize/2)+"px";
    var top = ((-game.player.y-.5)*game.tileHeight+game.windowSize/2)+"px";
    game.planeContainer.style.left = left;
    game.planeContainer.style.top = top;
}

How can I ensure that the dots in the center of the screen will be always lined up, instead of on a slight angle? 如何确保屏幕中心的点始终对齐,而不是成一个小角度? My current evidence suggests that the position of the game.planeContainer object isn't being established correctly. 我目前的证据表明game.planeContainer对象的位置没有正确建立。

I know this is a super-tough problem, so any help will be greatly appreciated. 我知道这是一个非常棘手的问题,因此将不胜感激。 Thanks in advance! 提前致谢!

( Another fiddle link, in case you skimmed over the first one. :D ) 另一个小提琴链接,以防您略过第一个。:D

Remove this line from your css stylings on .inner-text to take out the skew: 从.inner-text上的CSS样式中删除此行,以消除倾斜:
transform: translate(-50%, -50%); 转换:translate(-50%,-50%);

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

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