如何从每个具有4个子元素的4个网格元素创建横幅?

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

How to create banner from 4 grid elements each has 4 child elements

问题

你好!根据你提供的代码和描述,你想要实现的功能是点击元素时展开并显示其子元素。以下是你提供的代码的翻译:

function toggleAdditionalGrid(clickedElement) {
  var section = clickedElement.closest('.shopify-section');
  var additionalGrid = section.querySelector('.dt-sc-additional-grids');
  if(window.innerWidth < 1020){
    if(additionalGrid.style.display == 'none'){
      additionalGrid.style.cssText = 'display: flex; flex-wrap: wrap; justify-content: center; flex-direction: column; align-content: center;';
    } else {
      console.log(window.innerWidth);
      additionalGrid.style.cssText = 'display: none; grid-template-columns: repeat(4, 25%) !important; margin: auto; width: 100% !important; justify-content: center !important; @media (max-width: 1020px) {display: none; flex-wrap: wrap; justify-content: center; flex-direction: column; align-content: center;}';
    }
  } else {
    if(additionalGrid.style.display == 'none'){
      additionalGrid.style.cssText = 'display: inline-grid; grid-template-columns: repeat(4, 25%) !important; margin: auto; width: 100% !important; justify-content: center !important;';
    } else {
      console.log(window.innerWidth);
      additionalGrid.style.cssText = 'display: none; grid-template-columns: repeat(4, 25%) !important; margin: auto; width: 100% !important; justify-content: center !important; @media (max-width: 1020px) {display: grid; grid-template-columns: repeat(1, 100%); justify-content: center; flex-direction: column; align-content: center;}';
    }
  }
};

希望这可以帮助到你!如果你还有其他问题,请随时提问。

英文:

may you help me how to recreate my idea from image.

如何从每个具有4个子元素的4个网格元素创建横幅?

如何从每个具有4个子元素的4个网格元素创建横幅?

I don't understand the prince of selecting and placing others elements and after unselecting to put back...

I tried to use grid but it doesn't work well...

First of all this is my code that shows me the child elements

  function toggleAdditionalGrid(clickedElement) {
  var section = clickedElement.closest(&#39;.shopify-section&#39;);
  var additionalGrid = section.querySelector(&#39;.dt-sc-additional-grids&#39;);
  if(window.innerWidth&lt;1020){
              if(additionalGrid.style.display==&#39;none&#39; ){
                  additionalGrid.style.cssText=&#39;display: flex; flex-wrap: wrap;justify-content: center;flex-direction: column;align-content: center;&#39;;
                }
              else {
                console.log(window.innerWidth);
               additionalGrid.style.cssText=&#39;display: none; grid-template-columns: repeat(4, 25%) !important; margin: auto; width: 100% !important;justify-content: center !important; @media (max-width: 1020px) {display: none; flex-wrap: wrap;justify-content: center;flex-direction: column;align-content: center;}&#39;;
              }
  }
 else{
              if(additionalGrid.style.display==&#39;none&#39; ){
                additionalGrid.style.cssText=&#39;display: inline-grid; grid-template-columns: repeat(4, 25%) !important; margin: auto; width: 100% !important;justify-content: center !important;&#39;;
              }
            else{
                console.log(window.innerWidth);
             additionalGrid.style.cssText=&#39;display: none; grid-template-columns: repeat(4, 25%) !important; margin: auto; width: 100% !important;justify-content: center !important; @media (max-width: 1020px) {display: grid;grid-template-columns: repeat(1, 100%) ;justify-content: center;flex-direction: column;align-content: center;}&#39;;
            }
    }
};

So i just need a code that will expand clicked element and show child elements.

答案1

得分: 2

你可以使用Flexbox布局来构建一个“网格”,而不是使用display: grid

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

<!-- language: lang-css -->
*, *::before, *::after {
  box-sizing: border-box;
}

* {
  font-family: Arial;
}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.grid {
  display: flex;
  flex-direction: column;
  flex: 1;
  padding: 1rem;
  gap: 1rem;
  height: 100%;
}

.row {
  display: flex;
  flex-direction: row;
  flex: 1;
  gap: 1rem;
}

.col {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
}

.flex-2 {
  flex: 2;
}

.bg-red    { background: #FF2222; color: #FFF; }
.bg-orange { background: #FF6422; color: #FFF; }
.bg-brown  { background: #441212; color: #FFF; }

.fs-1 { font-size: 1rem; }
.fs-2 { font-size: 2rem; }
.fs-3 { font-size: 3rem; }

<!-- language: lang-html -->
<div class="grid">
  <div class="row flex-2 bg-red fs-3">
    <div class="col">Selected</div>
  </div>
  <div class="row">
    <div class="col bg-orange fs-1">Child</div>
    <div class="col bg-orange fs-1">Child</div>
    <div class="col bg-orange fs-1">Child</div>
    <div class="col bg-orange fs-1">Child</div>
  </div>
  <div class="row">
    <div class="col bg-brown"></div>
    <div class="col bg-brown"></div>
    <div class="col bg-brown"></div>
  </div>
</div>

<!-- end snippet -->

以上是使用Flexbox布局构建网格的示例代码。

英文:

You could use Flexbox layout to construct a "grid", rather than using display: grid.

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

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

*, *::before, *::after {
box-sizing: border-box;
}
* {
font-family: Arial;
}
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.grid {
display: flex;
flex-direction: column;
flex: 1;
padding: 1rem;
gap: 1rem;
height: 100%;
}
.row {
display: flex;
flex-direction: row;
flex: 1;
gap: 1rem;
}
.col {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
flex: 1;
}
.flex-2 {
flex: 2;
}
.bg-red    { background: #FF2222; color: #FFF; }
.bg-orange { background: #FF6422; color: #FFF; }
.bg-brown  { background: #441212; color: #FFF; }
.fs-1 { font-size: 1rem; }
.fs-2 { font-size: 2rem; }
.fs-3 { font-size: 3rem; }

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

&lt;div class=&quot;grid&quot;&gt;
&lt;div class=&quot;row flex-2 bg-red fs-3&quot;&gt;
&lt;div class=&quot;col&quot;&gt;Selected&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col bg-orange fs-1&quot;&gt;Child&lt;/div&gt;
&lt;div class=&quot;col bg-orange fs-1&quot;&gt;Child&lt;/div&gt;
&lt;div class=&quot;col bg-orange fs-1&quot;&gt;Child&lt;/div&gt;
&lt;div class=&quot;col bg-orange fs-1&quot;&gt;Child&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col bg-brown&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col bg-brown&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col bg-brown&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月8日 20:53:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859760.html
匿名

发表评论

匿名网友

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

确定