将鼠标悬停在它们上方时,为什么只有某些CSS Grid框会扩展?

时间:2019-02-22 16:41:39

标签: html css css3 alignment css-grid

我是CSS网格的新手,但我想尝试一下。我有一个3x3框的网格。当我将鼠标悬停在一个盒子上时,它应该走出完整的一行...但是那不起作用。

当我将鼠标悬停在1上时,它会完全填满屏幕;当我将鼠标悬停在3上时,我的屏幕会开始闪烁并且无法正常工作。

.container {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(3, minmax(100px, auto));
  grid-template-rows: repeat(3, minmax(100px, auto));
}

[class^="item"] {
  text-align: center;
  box-sizing: border-box;
  padding: 10%;
  font-size: 5em;
  color: #0580d5;
  transition: .2s;
}

[class^="item"]:hover {
  grid-column-start: 1;
  grid-column-end: 4;
}

.item-1 {
  /*grid-area: 1 / 2 / span 2 / span 2;*/
}

.container>div {
  border: 2px solid #0580d5;
  background-color: rgba(40, 180, 240, .3);
  border-radius: 5px;
}

@media only screen and (min-width: 789px) {
  .container {
    grid-template-columns: repeat(3, 1fr);
  }
}
<div class="container">
  <div class="item-1">1</div>
  <div class="item-2">2</div>
  <div class="item-3">3</div>
  <div class="item-4">4</div>
  <div class="item-5">5</div>
  <div class="item-6">6</div>
  <div class="item-7">7</div>
  <div class="item-8">8</div>
  <div class="item-9">9</div>
</div>

2 个答案:

答案 0 :(得分:2)

这里是 hack 来解决抖动盘旋的问题-使用display: contents和{{1} }定位到

最后只显示中的悬停的项,nth-child(3n+m)属性带有修饰符-在下面的演示中内联查看更多说明:

display
.container {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
}

.row {
  display: contents;
}

[class^="item"] { /* fix first column */
  grid-column: 1;
}

[class^="item"]:nth-child(3n+2) { /* fix second column */
  grid-column: 2;
}

[class^="item"]:nth-child(3n+3) { /* fix third column*/
  grid-column: 3;
}


[class^="item"]:hover { /* span 3 columns on hover of any cell */
  grid-column: 1 / 4;
  display: block !important; /* Show hovered item */
}

.row:nth-child(1):hover div { /* fix row on hover first row */
  grid-row: 1;
  display: none;
}

.row:nth-child(2):hover div { /* fix row on hover second row */
  grid-row: 2;
  display: none;
}

.row:nth-child(3):hover div { /* fix row on hover third row */
  grid-row: 3;
  display: none;
}


/* Styling */

.container>.row>div {
  border: 2px solid #0580d5;
  background-color: rgba(40, 180, 240, .3);
  border-radius: 5px;
}

[class^="item"] {
  text-align: center;
  box-sizing: border-box;
  font-size: 5em;
  color: #0580d5;
  transition: .2s;
}

答案 1 :(得分:2)

这里是一个想法,您可以依靠负边距来创建重叠效果,而无需更改结构并使用display:contents

.container {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(3, minmax(100px, auto));
  grid-template-rows: repeat(3, minmax(100px, auto));
}

[class^="item"] {
  text-align: center;
  box-sizing: border-box;
  padding: 10%;
  font-size: 5em;
  color: #0580d5;
  transition: .2s;
}

[class^="item"]:hover {
  z-index:2; /*we increase the z-index to cover the other*/
  background:
    /*we this to keep the initial background*/
    linear-gradient(rgba(40, 180, 240, .3),rgba(40, 180, 240, .3)),
    #fff;
}

[class^="item"]:nth-child(3n + 1):hover {
  margin-right:calc(-200% - 20px); /* we remove (2 x grid items + 2 x gap) */
}
[class^="item"]:nth-child(3n + 2):hover {
  margin-left:calc(-100% - 10px);
  margin-right:calc(-100% - 10px);
}
[class^="item"]:nth-child(3n + 3):hover {
  margin-left:calc(-200% - 20px);
}

.container>div {
  border: 2px solid #0580d5;
  background-color: rgba(40, 180, 240, .3);
  border-radius: 5px;
}

@media only screen and (min-width: 789px) {
  .container {
    grid-template-columns: repeat(3, 1fr);
  }
}
<div class="container">
  <div class="item-1">1</div>
  <div class="item-2">2</div>
  <div class="item-3">3</div>
  <div class="item-4">4</div>
  <div class="item-5">5</div>
  <div class="item-6">6</div>
  <div class="item-7">7</div>
  <div class="item-8">8</div>
  <div class="item-9">9</div>
</div>