【问题标题】:images aren't appearing in image gallery图片没有出现在图片库中
【发布时间】:2020-04-22 08:54:00
【问题描述】:

我正在尝试基于来自 Javascript30 (Repo Link) 的 Flex 面板库创建一个游泳图像库。 尝试在浏览器中显示网站时,图像立即超出框架,我只能看到页面上五个 flex 列的背景。

我不确定为什么它们在浏览器中被向下推到视野之外,以及为什么文本(不在列方向)直到我在列中单击后才会出现。

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

function toggleOpen() {
  console.log('Hello');
  this.classList.toggle('open');
}

function toggleActive(e) {
  console.log(e.propertyName);
  if (e.propertyName.includes('flex')) {
    this.classList.toggle('open-active');
  }
}

panels.forEach(panel => panel.addEventListener('click', toggleOpen));
panels.forEach(panel => panel.addEventListener('transitionend', toggleActive));
body {
  box-sizing: border-box;
  background: lightblue;
  font-family: "Source Sans Pro", sans-serif;
  font-size: 20px;
  font-weight: 200;
  margin: 0;
  box-sizing: inherit;
}

.image-panels {
  min-height: 100vh;
  overflow: hidden;
  display: flex;
}

.panel {
  display: flex;
  background: #6B0F9C;
  box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
  color: white;
  text-align: center;
  align-items: center;
  transition:
    font-size 0.7s cubic-bezier(0.51, -0.19, 0.7, -0.11),
    flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11),
    background 0.2s;
  font-size: 20px;
  background-size: cover;
  background-position: center;
  flex: 1;
  justify-content: center;
  flex-direction: column;
}

#first-panel {
  background-image: url(https://picsum.photos/id/10/2500/1667);
}

#second-panel {
  background-image: url(https://picsum.photos/id/10/2500/1667);
}

#third-panel {
  background-image: url(https://picsum.photos/id/10/2500/1667);
}

#fourth-panel {
  background-image: url(https://picsum.photos/id/10/2500/1667);
}

#fifth-panel {
  background-image: url(https://picsum.photos/id/10/2500/1667);
}

.panel > * {
  display: flex;
  margin: 0;
  width: 100%;
  transition: transform 0.5s;
  flex: 1 0 auto;
  justify-content: center;
  align-items: center;
}

.panel > *:first-child {
  transform: translateY(-100%);
}

.panel.open-active > *:first-child {
  transform: translateY(0);
}

.panel > *:last-child {
  transform: translateY(100%);
}

.panel.open-active > *:last-child {
  transform: translateY(0);
}

.panel div p {
  text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
  font-size: 2em;
}

.panel div p:nth-child(2) {
  font-size: 4em;
}

.panel.open {
  flex: 5;
  font-size: 40px;
}
<html lang="en">

  <head>
    <meta charset="utf-8">
    <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="styles.css">
    <title>Swim Club Image Gallery</title>
  </head>

  <body>
    <div class="image-panels">
      <div class="panel">
        <div id="first-panel">
          <p>Join</p>
          <p>This</p>
          <p>Quarter!</p>
        </div>
      </div>
      <div class="panel">
        <div id="second-panel">
          <p>Swimming</p>
          <p>Is</p>
          <p>Fun!</p>
        </div>
      </div>
      <div class="panel">
        <div id="third-panel">
          <p>Every</p>
          <p>Husky</p>
          <p>Is Welcome!</p>
        </div>
      </div>
      <div class="panel">
        <div id="fourth-panel">
          <p>Recreational</p>
          <p>Swim</p>
          <p>Atmosphere!</p>
        </div>
      </div>
      <div class="panel">
        <div id="fifth-panel">
          <p>Best</p>
          <p>Club</p>
          <p>To Stay Fit!</p>
        </div>
      </div>
    </div>
    <script src="index.js">
    </script>
  </body>

</html>

【问题讨论】:

    标签: javascript html css flexbox image-gallery


    【解决方案1】:

    通过观看repo 站点示例,您应该将图像背景应用到父 div,如下所示:

    <div class="panel" id="first-panel">
      <p>Join</p>
      <p>This</p>
      <p>Quarter!</p>
    </div>
    

    const panels = document.querySelectorAll('.panel');
    
    function toggleOpen() {
      console.log('Hello');
      this.classList.toggle('open');
    }
    
    function toggleActive(e) {
      console.log(e.propertyName);
      if (e.propertyName.includes('flex')) {
        this.classList.toggle('open-active');
      }
    }
    
    panels.forEach(panel => panel.addEventListener('click', toggleOpen));
    panels.forEach(panel => panel.addEventListener('transitionend', toggleActive));
    body {
      box-sizing: border-box;
      background: lightblue;
      font-family: "Source Sans Pro", sans-serif;
      font-size: 20px;
      font-weight: 200;
      margin: 0;
      box-sizing: inherit;
    }
    
    .image-panels {
      min-height: 100vh;
      overflow: hidden;
      display: flex;
    }
    
    .panel {
      display: flex;
      background: #6B0F9C;
      box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
      color: white;
      text-align: center;
      align-items: center;
      transition:
        font-size 0.7s cubic-bezier(0.51, -0.19, 0.7, -0.11),
        flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11),
        background 0.2s;
      font-size: 20px;
      background-size: cover;
      background-position: center;
      flex: 1;
      justify-content: center;
      flex-direction: column;
    }
    
    #first-panel {
      background-image: url(https://picsum.photos/id/10/2500/1667);
    }
    
    #second-panel {
      background-image: url(https://picsum.photos/id/10/2500/1667);
    }
    
    #third-panel {
      background-image: url(https://picsum.photos/id/10/2500/1667);
    }
    
    #fourth-panel {
      background-image: url(https://picsum.photos/id/10/2500/1667);
    }
    
    #fifth-panel {
      background-image: url(https://picsum.photos/id/10/2500/1667);
    }
    
    .panel > * {
      display: flex;
      margin: 0;
      width: 100%;
      transition: transform 0.5s;
      flex: 1 0 auto;
      justify-content: center;
      align-items: center;
    }
    
    .panel > *:first-child {
      transform: translateY(-100%);
    }
    
    .panel.open-active > *:first-child {
      transform: translateY(0);
    }
    
    .panel > *:last-child {
      transform: translateY(100%);
    }
    
    .panel.open-active > *:last-child {
      transform: translateY(0);
    }
    
    .panel div p {
      text-shadow: 0 0 4px rgba(0, 0, 0, 0.72), 0 0 14px rgba(0, 0, 0, 0.45);
      font-size: 2em;
    }
    
    .panel div p:nth-child(2) {
      font-size: 4em;
    }
    
    .panel.open {
      flex: 5;
      font-size: 40px;
    }
    <html lang="en">
    
      <head>
        <meta charset="utf-8">
        <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="styles.css">
        <title>Swim Club Image Gallery</title>
      </head>
    
      <body>
        <div class="image-panels">
          <div class="panel" id="first-panel">
            <p>Join</p>
            <p>This</p>
            <p>Quarter!</p>
          </div>
          <div class="panel" id="second-panel">
            <p>Swimming</p>
            <p>Is</p>
            <p>Fun!</p>
          </div>
          <div class="panel" id="third-panel">
            <p>Every</p>
            <p>Husky</p>
            <p>Is Welcome!</p>
          </div>
          <div class="panel" id="fourth-panel">
            <p>Recreational</p>
            <p>Swim</p>
            <p>Atmosphere!</p>
          </div>
          <div class="panel" id="fifth-panel">
            <p>Best</p>
            <p>Club</p>
            <p>To Stay Fit!</p>
          </div>
        </div>
        <script src="index.js">
        </script>
      </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 2018-10-20
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 2017-08-27
      相关资源
      最近更新 更多