图片库中的图片定位

时间:2019-08-19 09:07:29

标签: javascript html css flexbox

我正在尝试使排行榜/摄影师图像始终保持在顶部位置,即使在做出响应时也是如此。

这是我的代码

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial;
}

img {
  position: relative;
  overflow: hidden;
  top: 0;
  left: 0;
  opacity: 1;
  background-color: rgba(0, 0, 0, 0.5);
  -webkit-transition: all .4s ease-in-out;
  transition: all .4s ease-in-out;
}

.row {
  display: -ms-flexbox;
  /* IE10 */
  display: flex;
  -ms-flex-wrap: wrap;
  /* IE10 */
  flex-wrap: wrap;
  padding: 0 20px;
}

.column {
  -ms-flex: 25%;
  /* IE10 */
  flex: 25%;
  max-width: 25%;
  padding: 10px;
}

.column img {
  margin-top: 20px;
  vertical-align: middle;
}

#overlay:hover {
  overflow: hidden;
  top: 0;
  left: 0;
  opacity: 0.7;
  background-color: rgba(0, 0, 0, 0.5);
  -webkit-transition: all .4s ease-in-out;
  transition: all .4s ease-in-out;
}

@media screen and (max-width: 800px) {
  .column {
    -ms-flex: 50%;
    flex: 50%;
    max-width: 50%;
  }
}


/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */

@media screen and (max-width: 600px) {
  .column {
    -ms-flex: 100%;
    flex: 100%;
    max-width: 100%;
  }
}
<html>

<body>


  <div class="row">
    <div class="column">
      <a href="#"><img src="https://www.w3schools.com/w3images/wedding.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/rocks.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/falls2.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/paris.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/nature.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/mist.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/paris.jpg" id="overlay" style="width:100%"></a>
    </div>
    <div class="column">
      <a href="#"><img src="https://www.w3schools.com/w3images/underwater.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/ocean.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/wedding.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/mountainskies.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/rocks.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/underwater.jpg" id="overlay" style="width:100%"></a>
    </div>
    <div class="column">
      <a href="#"><img src="https://www.w3schools.com/w3images/wedding.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/rocks.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/falls2.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/paris.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/nature.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/mist.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/paris.jpg" id="overlay" style="width:100%"></a>
    </div>
    <div class="column">
      <a class="js-leaderboard" href="#"><img src="https://i.imgur.com/C5vLv9p.png" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/ocean.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/wedding.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/mountainskies.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/rocks.jpg" id="overlay" style="width:100%"></a>
      <a href="#"><img src="https://www.w3schools.com/w3images/underwater.jpg" id="overlay" style="width:100%"></a>
    </div>
  </div>
  </div>

</body>

</html>

如果您看到pexels网站,则排行榜始终保持在右上方。我可以这样做吗?此外,我为此图像添加了一个类js-leaderboard

1 个答案:

答案 0 :(得分:1)

这可以使用order的{​​{1}}属性来完成。我已经在小屏幕中重新排列了列。

flex-items
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial;
}

img {
  position: relative;
  overflow: hidden;
  top: 0;
  left: 0;
  opacity: 1;
  background-color: rgba(0, 0, 0, 0.5);
  -webkit-transition: all .4s ease-in-out;
  transition: all .4s ease-in-out;
}

.row {
  display: -ms-flexbox;
  /* IE10 */
  display: flex;
  -ms-flex-wrap: wrap;
  /* IE10 */
  flex-wrap: wrap;
  padding: 0 20px;
}

.column {
  -ms-flex: 25%;
  /* IE10 */
  flex: 25%;
  max-width: 25%;
  padding: 10px;
}

.column img {
  margin-top: 20px;
  vertical-align: middle;
}

#overlay:hover {
  overflow: hidden;
  top: 0;
  left: 0;
  opacity: 0.7;
  background-color: rgba(0, 0, 0, 0.5);
  -webkit-transition: all .4s ease-in-out;
  transition: all .4s ease-in-out;
}

@media screen and (max-width: 800px) {
  .column {
    -ms-flex: 50%;
    flex: 50%;
    max-width: 50%;
    order: 3;
  }
  
  .row > .column:first-child{
  order: 1;
  }
  
  .right-top-col{
  order: 2;
  }
}


/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */

@media screen and (max-width: 600px) {
  .column {
    -ms-flex: 100%;
    flex: 100%;
    max-width: 100%;
  }
}