【问题标题】:Javascript placement using grid on site:在网站上使用网格放置 Javascript:
【发布时间】:2021-09-06 05:00:30
【问题描述】:

您好,我有几个 javascript 文件,我想将它们放在网站上,但使用 CSS 中的网格放置它们。 我把代码分享给你,我不明白: 我需要在哪里写入列和行,以便在我将列和行添加到 .box-1 类之后它会改变它的位置?

我还有几个要更改位置的 javascript 文件,我的问题是,是否建议单独保留每个 javascript 文件并将其链接到 HTML,或者是否建议将所有 javascript 文件合并到一个文件中,然后链接到 HTML? 谢谢。

const panels = document.querySelectorAll('.panel')

panels.forEach(panel => {
  panel.addEventListener('click', () => {
    removeActiveClasses()
    panel.classList.add('active')
  })
})

function removeActiveClasses() {
  panels.forEach(panel => {
    panel.classList.remove('active')
  })
}
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
  box-sizing: border-box;
}

body {
  font-family: 'Muli', sans-serif;
  display: grid;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
}

.box-1 {
  display: flex;
  width: 30vw;
  height: auto;
  flex-direction: row;
}

.container {
  display: flex;
  width: 90vw;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr 1fr;
}

.box-1 {
  grid-column: 3/4;
  grid-row: 2/3;
}

.panel {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 80vh;
  border-radius: 50px;
  color: #fff;
  cursor: pointer;
  flex: 0.5;
  margin: 10px;
  position: relative;
  -webkit-transition: all 700ms ease-in;
}

.panel h3 {
  font-size: 24px;
  position: absolute;
  bottom: 20px;
  left: 20px;
  margin: 0;
  opacity: 0;
}

.panel.active {
  flex: 5;
}

.panel.active h3 {
  opacity: 1;
  transition: opacity 0.3s ease-in 0.4s;
}

@media (max-width: 480px) {
  .container {
    width: 100vw;
  }
  .panel:nth-of-type(4),
  .panel:nth-of-type(5) {
    display: none;
  }
}
<div class="box 1">

  <div class="container">
    <div class="panel active" style="background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
      <h3>Explore The World</h3>
    </div>
    <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
      <h3>Wild Forest</h3>
    </div>
    <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80')">
      <h3>Sunny Beach</h3>
    </div>
    <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')">
      <h3>City on Winter</h3>
    </div>
    <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')">
      <h3>Mountains - Clouds</h3>
    </div>

  </div>

</div>

【问题讨论】:

    标签: javascript css css-grid


    【解决方案1】:

    我成功了: 我添加了 2 个队列以便它们可以占用空间,并且我减小了 JavaScript 文件的宽度-

    enter code here @import 
    
    url('https://fonts.googleapis.com/css?family=Muli&display=swap');
    
    * {
      box-sizing: border-box;
    }
    
    
    
    body {
      font-family: 'Muli', sans-serif;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      overflow: hidden;
      margin: 0;
       display: grid;
      grid-template-columns: 1fr 1fr 1fr; 
    }
    
    .container {
      display: flex;
      width: 30vw;
     justify-items: center;
     grid-column: 2/3;
    }
    
    .card{
      grid-column: 1/2;
      width: 80vh;
      margin: 30px;
    }
    .card2{
      grid-column: 3/4;
      width: 80vh;
      margin: 30px;
    }
    
    .panel {
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
      height: 30vh;
      border-radius: 50px;
      color: #fff;
      cursor: pointer;
      flex: 0.5;
      margin: 10px;
      position: relative;
      -webkit-transition: all 700ms ease-in;
      grid-column: 3/4;
      width: 30vh;
    }
    
    .panel h3 {
      font-size: 24px;
      position: absolute;
      bottom: 20px;
      left: 20px;
      margin: 0;
      opacity: 0;
      
    }
    
    .panel.active {
      flex: 5;
    }
    
    .panel.active h3 {
      opacity: 1;
      transition: opacity 0.3s ease-in 0.4s;
    }
    
    @media (max-width: 480px) {
      .container {
        width: 100vw;
      }
    
      .panel:nth-of-type(4),
      .panel:nth-of-type(5) {
        display: none;
      }
    }
    

    【讨论】:

    • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多