简体   繁体   English

调整浏览器屏幕大小正在弄乱我的div偏移量

[英]Resizing browser screen is messing up my div's offset

I am currently creating a mahjong game. 我目前正在创建一个麻将游戏。 The game is as good as complete. 游戏是一样的完整。 Everything worked fine until I decided to put the game in a div ( div id="holder" ) to make sure it will be centered in the browser window. 在我决定将游戏放入div( div id="holder" )以确保游戏将在浏览器窗口中居中之前,一切工作正常。

What happens? 怎么了?

Step 1: Resize you browser window so it matches the games size (so no white border on the left or on the right. Note: The problem also applies when adding more space to the top, but in this demo it is not possible to adjust this. 步骤1:调整浏览器窗口的大小,使其与游戏大小匹配(因此左侧或右侧没有白色边框。 注意:在顶部添加更多空间时,该问题也适用,但是在此演示中无法调整这个。

Step 2: Click: Click to Start , the game will start and then click any HINT button. 步骤2:单击: Click to Start ,游戏将开始,然后单击任何提示按钮。 You will see a nr appearing above the button you've just clicked, it moves up, and disappears. 您将看到一个nr出现在您刚刚单击的按钮上方,它向上移动并消失。 This nr is positioned correct and should always appear in the middle just above the HINT button you've clicked. 该nr位置正确,应始终显示在您单击的提示按钮上方的中间位置。

Step 3: Now resize your browser window, give the game (let's say) 2cm of white space on the left and on the right. 第3步:现在调整浏览器窗口的大小,在左侧和右侧分别为游戏(例如)2cm的白色空间。

Step 4: Now click another HINT button. 步骤4:现在单击另一个提示按钮。 Notice the nr is appearing, but NOT in the middle and above the HINT button anymore, but 2cm to the right. 请注意,正在显示nr,但不再位于“提示”按钮的中间和上方,而是向右2cm。

So what is happening? 那到底是怎么回事? My guess is that the offset of the div ( div class="score" ), which contains the nr, includes the white space to its offset (or something like this). 我的猜测是,包含nr的div( div class="score" )的偏移量包括其偏移量的空格(或类似的东西)。

This is something I obviously don't want! 这显然是我不想要的! But I have no clue of how to fix this... 但是我不知道如何解决这个问题...

Some code snippets: 一些代码片段:

The DIV's CSS: DIV的CSS:

#holder {
    width: 940px; 
    margin: auto auto;
}
.score {
    display: none;
    pointer-events: none;
    position: absolute;
    font-size: 1em;
    text-align: center;
    color:#FFF;
    z-index: 1;
}

The JS-part (using jQuery): JS部分(使用jQuery):

var offset = $(this).hide(500).offset();
var penalty = Math.round(score * -0.10);  //calculates a score penalty
score += penalty; //this is the result
$('.sc').text(score); //this is needed elsewhere in the script
$('.score') //start of showing the div and it's animation
      .show()
      .text(penalty)
      .css({
          top: offset.top - $('.score').height() - 90,
          left: offset.left,
          width:'3.5em'
      })
      .stop()
      .delay(100)  
      .animate({top: offset.top - $('.score').height() - 140},700)
      .hide(0); 
 ...some more code here, not relevant to this

If more code is needed, please let me know, but I think this is sufficient. 如果需要更多代码,请告诉我,但是我认为这已经足够。 Hope this problem can be solved! 希望这个问题能解决!

Kind regards 亲切的问候

in your holder css add position:relative; 在您的持有人CSS中添加position:relative;

#holder {
    width: 940px; 
    margin: auto auto;
    position: relative;
}

see if that fixes it. 看看是否能解决问题。

When is the offset variable calculated? 何时计算偏移量? It should be stored when the hint is clicked. 单击提示时应将其存储。

What happens it you take the offset from before the hide? 如果您将皮件偏移到偏移位置会发生什么情况?

var offset = $(this).offset().hide(500); var offset = $(this).offset()。hide(500);

I think I have found it. 我想我已经找到了。 IN my CSS I have another div: 在我的CSS中,我还有另一个div:

#s0 {
    position: relative;
    float: left; 
    height: 570px;
    width: 760px;
    background: #222 url('images/bg.png');
}

Removing: position: relative; 去除: position: relative; seems to fix it. 似乎解决了。 Will do some more testing. 会做更多的测试。 All thanks for you suggestions! 谢谢您的建议!

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

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