父级不是 flex,但子级似乎是 flex。

huangapple go评论56阅读模式
英文:

Parent not flex, but children appears flex

问题

这是一个初学者的问题,但我无法理解...
下面我有一个div,一个灰色区域。对于一个小型射击游戏,我使用appendChild()添加了2个子元素。
我的子元素呈现为Flex布局(并排),而不是像下面的JavaScript代码所要求的那样重叠:

let cadre_Jeu = document.querySelector('.cadre_Jeu');

let h_cadre = cadre_Jeu.offsetHeight;
let w_cadre = cadre_Jeu.offsetWidth;
let h_pistol = 150;
let w_pistol = h_pistol * 0.72;

// 测试1
let pistol = document.createElement('img');
pistol.id = 'pistol';
pistol.src = 'https://via.placeholder.com/200';
pistol.style.height = h_pistol + 'px';
pistol.style.width = w_pistol + 'px';

cadre_Jeu.appendChild(pistol);
pistol.style.position = 'relative';
pistol.style.left = w_cadre / 2 - w_pistol / 2 + 'px';
pistol.style.top = h_cadre / 2 - h_pistol / 2 + 'px';
// 测试1结束

// 测试2
let pistol2 = document.createElement('img');
pistol2.id = 'pistol2';
pistol2.src = 'https://via.placeholder.com/200';
pistol2.style.height = h_pistol + 'px';
pistol2.style.width = w_pistol + 'px';

cadre_Jeu.appendChild(pistol2);
pistol2.style.position = 'relative';
pistol2.style.left = w_cadre / 2 - w_pistol / 2 + 'px';
pistol2.style.top = h_cadre / 2 - h_pistol / 2 + 'px';
// 测试2结束
.cadre_Jeu {
  height: 500px;
  width: 50%;
  background-color: #555;
  border: solid black 2px;
  cursor: crosshair;
}
<div class="cadre_Jeu"></div>

我不明白为什么第二个子元素会参照第一个子元素,您有任何想法吗?

英文:

it's a beginner question, but I cannot figure out...
Below I have one div, a grey area. For a small shooting game, I appendchild() 2 children.
My children appears as flex (side to side) instead of supperposing as requested by javascript code below :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let cadre_Jeu = document.querySelector(&#39;.cadre_Jeu&#39;);

let h_cadre = cadre_Jeu.offsetHeight;
let w_cadre = cadre_Jeu.offsetWidth;
let h_pistol = 150;
let w_pistol = h_pistol * 0.72;
//test1
let pistol = document.createElement(&#39;img&#39;);
pistol.id = &#39;pistol&#39;;
pistol.src = &#39;https://via.placeholder.com/200&#39;;
pistol.style.height = h_pistol + &#39;px&#39;;
pistol.style.width = w_pistol + &#39;px&#39;;

cadre_Jeu.appendChild(pistol);
pistol.style.position = &#39;relative&#39;;
pistol.style.left = w_cadre / 2 - w_pistol / 2 + &#39;px&#39;;
pistol.style.top = h_cadre / 2 - h_pistol / 2 + &#39;px&#39;;
//test1 end
//test2
let pistol2 = document.createElement(&#39;img&#39;);
pistol2.id = &#39;pistol2&#39;;
pistol2.src = &#39;https://via.placeholder.com/200&#39;;
pistol2.style.height = h_pistol + &#39;px&#39;;
pistol2.style.width = w_pistol + &#39;px&#39;;

cadre_Jeu.appendChild(pistol2);
pistol2.style.position = &#39;relative&#39;;
pistol2.style.left = w_cadre / 2 - w_pistol / 2 + &#39;px&#39;;
pistol2.style.top = h_cadre / 2 - h_pistol / 2 + &#39;px&#39;;
//test2 end

<!-- language: lang-css -->

.cadre_Jeu {
  height: 500px;
  width: 50%;
  background-color: #555;
  border: solid black 2px;
  cursor: crosshair;
}

<!-- language: lang-html -->

&lt;div class=&quot;cadre_Jeu&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

I don't understand why the second child is taking reference on the first one, do you have any idea ?

答案1

得分: 2

因为&lt;img&gt;元素是内联元素,所以你需要将position更改为absolute以获得该行为。

pistol.style.position = 'absolute';

如果你仍想保持相对位置,那么可以从第二把手枪的左属性中减去手枪的宽度,像下面这样,也可以正常工作:

pistol2.style.left = (w_cadre / 2 - w_pistol / 2) - w_pistol + 'px';
.cadre_Jeu {
    height: 500px;
    width: 50%;
    background-color: #555;
    border: solid black 2px;
    cursor: crosshair;
}
<div class="cadre_Jeu"></div>
英文:

Because &lt;img&gt; elements are inline elements. You need to change the position to absolute to get that behavior.

pistol.style.position = &#39;absolute&#39;;

And also if you want to keep the position relative then subtract the pistol width from the left property of the 2nd pistol like below and it will work too:

pistol2.style.left = (w_cadre / 2 - w_pistol / 2) - w_pistol + &#39;px&#39;;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let cadre_Jeu = document.querySelector(&#39;.cadre_Jeu&#39;);

let h_cadre = cadre_Jeu.offsetHeight;
let w_cadre = cadre_Jeu.offsetWidth;
let h_pistol = 150;
let w_pistol = h_pistol * 0.72;
//test1
let pistol = document.createElement(&#39;img&#39;);
pistol.id = &#39;pistol&#39;;
pistol.src = &#39;chuck_game.png&#39;;
pistol.style.height = h_pistol + &#39;px&#39;;
pistol.style.width = w_pistol + &#39;px&#39;;


cadre_Jeu.appendChild(pistol);
pistol.style.position = &#39;absolute&#39;;
pistol.style.left = w_cadre / 2 - w_pistol / 2 + &#39;px&#39;;
pistol.style.top = h_cadre / 2 - h_pistol / 2 + &#39;px&#39;;
//test1 end
//test2
let pistol2 = document.createElement(&#39;img&#39;);
pistol2.id = &#39;pistol2&#39;;
pistol2.src = &#39;chuck_game.png&#39;;
pistol2.style.height = h_pistol + &#39;px&#39;;
pistol2.style.width = w_pistol + &#39;px&#39;;


cadre_Jeu.appendChild(pistol2);
pistol2.style.position = &#39;absolute&#39;;
pistol2.style.left = w_cadre / 2 - w_pistol / 2 + &#39;px&#39;;
pistol2.style.top = h_cadre / 2 - h_pistol / 2 + &#39;px&#39;;
//test2 end

<!-- language: lang-css -->

.cadre_Jeu {
  height: 500px;
  width: 50%;
  background-color: #555;
  border: solid black 2px;
  cursor: crosshair;
}

<!-- language: lang-html -->

&lt;div class=&quot;cadre_Jeu&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月18日 04:20:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定