防止网格项的高度扩展到所有可用的高度。

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

Prevent height of grid item from expanding all available height

问题

你的代码基本上正常工作,但有一个小问题。每当我想要切换特定球员的框时,框会扩展(正常情况下),但与我切换的框在同一行的其他球员的框也会扩展。有没有办法防止这种情况发生?

以下是你的代码中需要翻译的部分:

const arsenal = {
    name: 'Arsenal',
    city: 'London',
    startingLineup: {
        // ...其他球员数据...
    }
}

let container = document.querySelector(".container")

Object.entries(arsenal.startingLineup).forEach(([key,value]) => {
    let player = document.createElement("div")
    // ...其他DOM元素的创建和添加...
    player.addEventListener("click",() => {
        player.classList.toggle("active")
    })
})

let title = document.querySelector("h1")
title.innerHTML = arsenal.name
* {
  margin: 0;
  padding: 0
}

body {
  background-color: rgb(203, 17, 17);
  font-family: arial;
}

.container {
  background-color: aquamarine transparent;
  width: fit-content;
  margin: auto;
  display: grid;
  grid-template-columns: auto auto auto;
}

.player {
  background-color: rgb(168, 21, 21);
  width: fit-content;
  box-shadow: 0px 2px 3px 4px rgba(255, 255, 255, .5);
  border-radius: 10px;
  margin: 20px;
}

.player img {
  width: 200px;
  height: 200px;
  padding: 5px
}

.playerData {
  display: none;
  max-height: 0;
}

.player.active .playerData {
  background-color: rgb(118, 11, 11);
  padding: 5px;
  border-radius: 0px 0px 10px 10px;
  max-height: 200px;
  display: block;
}

h3 {
  color: white
}
<h1>Hello World</h1>
<div class="container">
  <!-- 这里是球员信息的HTML结构 -->
</div>
英文:

My code is generally working fine but there is one small issue. Everytime that I want to toggle a particular players' box the box expands (as it should) but then the other players' boxes that are in the same row as the box im toggling will expand too. Is there any way to prevent this from happening?

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

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

const arsenal = {
    name: &#39;Arsenal&#39;,
    city: &#39;London&#39;,
    startingLineup: {
        gk: {name:&#39;Aaron Ramsdale&#39;, age: 24, country:&#39;England&#39;, img:&#39;img/ramsdale.png&#39;},
        lb: {name:&#39;Oleksandr Zinchenko&#39;, age: 26, country:&#39;Ukraine&#39;, img: &#39;img/zinchenko.jpg&#39;},
        rb: {name:&#39;Ben White&#39;, age: 25, country:&#39;England&#39;, img: &#39;img/white.png&#39;},
        lcb: {name:&#39;Gabriel Magalhaes&#39;, age: 25, country:&#39;Brazil&#39;,img: &#39;img/gabriel.png&#39;},
        rcb: {name: &#39;William Saliba&#39;, age: 22, country:&#39;France&#39;, img: &#39;img/saliba.png&#39;},
        cdm: {name:&#39;Thomas Partey&#39;,age: 29,country:&#39;Ghana&#39;, img: &#39;img/partey.png&#39;},
        cm: {name:&#39;Granit Xhaka&#39;,age:30,country:&#39;Switzerland&#39;,img: &#39;img/xhaka face.png&#39;},
        cam: {name:&#39;Martin Odegaard&#39;,age:23,country:&#39;Norway&#39;,img: &#39;img/odegaard.png&#39;},
        lw: {name:&#39;Gabriel Martinelli&#39;,age:21, country:&#39;Brazil&#39;,img: &#39;img/martinelli.png&#39;},
        rw: {name:&#39;Bukayo Saka&#39;,age:21,country:&#39;England&#39;,img:&#39;img/saka.png&#39;},
        st: {name:&#39;Gabriel Jesus&#39;,age:26,country:&#39;Brazil&#39;, img: &#39;img/jesus.png&#39;}
    }
}

let container = document.querySelector(&quot;.container&quot;)

Object.entries(arsenal.startingLineup).forEach(([key,value]) =&gt; {
    let player = document.createElement(&quot;div&quot;)
    let playerData = document.createElement(&quot;div&quot;)

    let image = document.createElement(&quot;img&quot;)
    let name = document.createElement(&quot;h3&quot;)
    let age = document.createElement(&quot;h3&quot;)
    let country = document.createElement(&quot;h3&quot;)

    image.src = value.img
    let nameNode = document.createTextNode(value.name)
    let ageNode = document.createTextNode(value.age)
    let countryNode = document.createTextNode(value.country)

    name.appendChild(nameNode)
    age.appendChild(ageNode)
    country.appendChild(countryNode)

    image.classList.add(&quot;image&quot;)
    name.classList.add(&quot;h3&quot;)
    age.classList.add(&quot;h3&quot;)
    country.classList.add(&quot;h3&quot;)

    playerData.append(name, age, country)
    player.append(image, playerData)

    playerData.classList.add(&quot;playerData&quot;)
    player.classList.add(&quot;player&quot;)

    container.appendChild(player)

    player.addEventListener(&quot;click&quot;,() =&gt; {
        player.classList.toggle(&quot;active&quot;)
    })
})

let title = document.querySelector(&quot;h1&quot;)
title.innerHTML = arsenal.name

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

* {
  margin: 0;
  padding: 0
}

body {
  background-color: rgb(203, 17, 17);
  font-family: arial;
}

.container {
  background-color: aquamarine transparent;
  width: fit-content;
  margin: auto;
  display: grid;
  grid-template-columns: auto auto auto;
}

.player {
  background-color: rgb(168, 21, 21);
  width: fit-content;
  box-shadow: 0px 2px 3px 4px rgba(255, 255, 255, .5);
  border-radius: 10px;
  margin: 20px;
}

.player img {
  width: 200px;
  height: 200px;
  padding: 5px
}

.playerData {
  display: none;
  max-height: 0;
}

.player.active .playerData {
  background-color: rgb(118, 11, 11);
  padding: 5px;
  border-radius: 0px 0px 10px 10px;
  max-height: 200px;
  display: block;
}

h3 {
  color: white
}

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

&lt;h1&gt;Hello World&lt;/h1&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;!--&lt;div class=&quot;player&quot;&gt;
        &lt;img class=&quot;image&quot; src=&quot;img/jesus.png&quot;&gt;
        &lt;div class=&quot;playerData&quot;&gt;
            &lt;h3 class=&quot;name&quot;&gt;Gabriel Jesus&lt;/h3&gt;
            &lt;h3 class=&quot;age&quot;&gt;25&lt;/h3&gt;
            &lt;h3 class=&quot;country&quot;&gt;Brazil&lt;/h3&gt;
        &lt;/div&gt;
    &lt;/div&gt;--&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是您要翻译的内容:

"The point is that with grid, the child elements expand to all the available height. To prevent your cards from expanding all the height, you can wrap them in another div like this :"

"关键是,使用网格布局,子元素会扩展到所有可用的高度。为了防止您的卡片扩展到整个高度,您可以像这样将它们包装在另一个 div 中:"

英文:

The point is that with grid, the child elements expand to all the available height. To prevent your cards from expanding all the height, you can wrap them in another div like this :

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

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

const arsenal = {
    name: &#39;Arsenal&#39;,
    city: &#39;London&#39;,
    startingLineup: {
        gk: {name:&#39;Aaron Ramsdale&#39;, age: 24, country:&#39;England&#39;, img:&#39;img/ramsdale.png&#39;},
        lb: {name:&#39;Oleksandr Zinchenko&#39;, age: 26, country:&#39;Ukraine&#39;, img: &#39;img/zinchenko.jpg&#39;},
        rb: {name:&#39;Ben White&#39;, age: 25, country:&#39;England&#39;, img: &#39;img/white.png&#39;},
        lcb: {name:&#39;Gabriel Magalhaes&#39;, age: 25, country:&#39;Brazil&#39;,img: &#39;img/gabriel.png&#39;},
        rcb: {name: &#39;William Saliba&#39;, age: 22, country:&#39;France&#39;, img: &#39;img/saliba.png&#39;},
        cdm: {name:&#39;Thomas Partey&#39;,age: 29,country:&#39;Ghana&#39;, img: &#39;img/partey.png&#39;},
        cm: {name:&#39;Granit Xhaka&#39;,age:30,country:&#39;Switzerland&#39;,img: &#39;img/xhaka face.png&#39;},
        cam: {name:&#39;Martin Odegaard&#39;,age:23,country:&#39;Norway&#39;,img: &#39;img/odegaard.png&#39;},
        lw: {name:&#39;Gabriel Martinelli&#39;,age:21, country:&#39;Brazil&#39;,img: &#39;img/martinelli.png&#39;},
        rw: {name:&#39;Bukayo Saka&#39;,age:21,country:&#39;England&#39;,img:&#39;img/saka.png&#39;},
        st: {name:&#39;Gabriel Jesus&#39;,age:26,country:&#39;Brazil&#39;, img: &#39;img/jesus.png&#39;}
    }
}

let container = document.querySelector(&quot;.container&quot;)

Object.entries(arsenal.startingLineup).forEach(([key, value]) =&gt; {
  let wrapper = document.createElement(&quot;div&quot;)
  let player = document.createElement(&quot;div&quot;)
  let playerData = document.createElement(&quot;div&quot;)

  let image = document.createElement(&quot;img&quot;)
  let name = document.createElement(&quot;h3&quot;)
  let age = document.createElement(&quot;h3&quot;)
  let country = document.createElement(&quot;h3&quot;)

  image.src = value.img
  let nameNode = document.createTextNode(value.name)
  let ageNode = document.createTextNode(value.age)
  let countryNode = document.createTextNode(value.country)

  name.appendChild(nameNode)
  age.appendChild(ageNode)
  country.appendChild(countryNode)

  image.classList.add(&quot;image&quot;)
  name.classList.add(&quot;h3&quot;)
  age.classList.add(&quot;h3&quot;)
  country.classList.add(&quot;h3&quot;)

  playerData.append(name, age, country)
  player.append(image, playerData)
  wrapper.append(player)

  playerData.classList.add(&quot;playerData&quot;)
  player.classList.add(&quot;player&quot;)
  wrapper.classList.add(&quot;wrapper&quot;)

  container.appendChild(wrapper)

  player.addEventListener(&quot;click&quot;, () =&gt; {
    player.classList.toggle(&quot;active&quot;)
  })
})

let title = document.querySelector(&quot;h1&quot;)
title.innerHTML = arsenal.name

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

* {
  margin: 0;
  padding: 0
}

body {
  background-color: rgb(203, 17, 17);
  font-family: arial;
}

.container {
  background-color: aquamarine transparent;
  width: fit-content;
  margin: auto;
  display: grid;
  grid-template-columns: auto auto auto;
}

.player {
  background-color: rgb(168, 21, 21);
  width: fit-content;
  box-shadow: 0px 2px 3px 4px rgba(255, 255, 255, .5);
  border-radius: 10px;
  margin: 20px;
}

.player img {
  width: 200px;
  height: 200px;
  padding: 5px
}

.playerData {
  display: none;
  max-height: 0;
}

.player.active .playerData {
  background-color: rgb(118, 11, 11);
  padding: 5px;
  border-radius: 0px 0px 10px 10px;
  max-height: 200px;
  display: block;
}

h3 {
  color: white
}

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

&lt;h1&gt;Hello World&lt;/h1&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;!--&lt;div class=&quot;player&quot;&gt;
        &lt;img class=&quot;image&quot; src=&quot;img/jesus.png&quot;&gt;
        &lt;div class=&quot;playerData&quot;&gt;
            &lt;h3 class=&quot;name&quot;&gt;Gabriel Jesus&lt;/h3&gt;
            &lt;h3 class=&quot;age&quot;&gt;25&lt;/h3&gt;
            &lt;h3 class=&quot;country&quot;&gt;Brazil&lt;/h3&gt;
        &lt;/div&gt;
    &lt;/div&gt;--&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 19:51:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227349.html
匿名

发表评论

匿名网友

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

确定