如何使 DIV 向上展开而不是向下?

5

当鼠标悬停在一个div上时,它应该弹出在背景图像之上并显示其他文本。此外,背景图像会更改。

目前,DIV似乎只想向下扩展,但我真的很希望它向上扩展。但是问题在于,我不能使用position: fixed,因为元素需要随着页面滚动而滚动,而不是固定在底部。

这是我所拥有的:

  $('#hover-01').on('mouseenter', function() {
    $('#hover-change').css('background-image', 'url(http://www.w3schools.com/css/trolltunga.jpg)');
  });
  $('#hover-01').on('mouseleave', function() {
    $('#hover-change').css('background-image', 'url(https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4)');
  });

  $('#hover-02').on('mouseenter', function() {
    $('#hover-change').css('background-image', 'url(http://www.w3schools.com/css/trolltunga.jpg)');
  });
  $('#hover-02').on('mouseleave', function() {
    $('#hover-change').css('background-image', 'url(https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4)');
  });

  $('#hover-03').on('mouseenter', function() {
    $('#hover-change').css('background-image', 'url(http://www.w3schools.com/css/trolltunga.jpg)');
  });
  $('#hover-03').on('mouseleave', function() {
    $('#hover-change').css('background-image', 'url(https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4)');
  });
.wrapper {
  position: relative;
}

#hover-change {
  width: 100%;
  height: 300px;
  background-image: url("http://www.w3schools.com/css/trolltunga.jpg");
  background-repeat: no-repeat;
}

@import url(https://fonts.googleapis.com/css?family=Merriweather);

* {
  box-sizing: border-box;
}

body {
  font-family: 'Merriweather', serif;
  padding: 20px;
}

a {
  color: #f06d06;
  text-decoration: none;
}

.box {
  padding: 2em;
  border: 1px solid #ccc;
  display: block;
  width: 33%;
  height: 150px;
  float: left;
  margin: -1px 0 0 -1px;
  color: black;
  background: linear-gradient(white, white 50%, #333 50%, #333);
  background-size: 100% 202%;
  transition: all 0.2s ease;
  animation: down-bump 0.4s ease;
  position: absolute;
  margin-top: -150px;
  overflow: hidden;


}

.box h2 {
  font-weight: 400;
  letter-spacing: -1.5px;
  line-height: 1.2;
}

.box h3 {
  font: 0.8em "Lucida Grande", serif;
}

.box:hover {
  background-position: 100% 100%;
  animation: up-bump 0.4s ease;
  height: 400px;
}

.box:hover h2 {
  color: #48ad26;
}

.box:hover h2 span {
  color: white;
}

.box:hover h3 {
  color: #999;
}



.box2 {
  padding: 2em;
  border: 1px solid #ccc;
  display: block;
  width: 33%;
  height: 150px;
  float: right;
  margin: -1px 0 0 -1px;
  color: black;
  background: linear-gradient(white, white 50%, #333 50%, #333);
  background-size: 100% 202%;
  transition: all 0.2s ease;
  animation: down-bump 0.4s ease;
  position: absolute;
  margin-top: -150px;
  margin-left: 33%;
  overflow: hidden;


}

.box2 h2 {
  font-weight: 400;
  letter-spacing: -1.5px;
  line-height: 1.2;
}

.box2 h3 {
  font: 0.8em "Lucida Grande", serif;
}

.box2:hover {
  background-position: 100% 100%;
  animation: up-bump 0.4s ease;
  height: 400px;
}

.box2:hover h2 {
  color: #48ad26;
}

.box2:hover h2 span {
  color: white;
}

.box2:hover h3 {
  color: #999;
}

.box3 {
  padding: 2em;
  border: 1px solid #ccc;
  display: block;
  width: 33%;
  height: 150px;
  float: right;
  margin: -1px 0 0 -1px;
  color: black;
  background: linear-gradient(white, white 50%, #333 50%, #333);
  background-size: 100% 202%;
  transition: all 0.2s ease;
  animation: down-bump 0.4s ease;
  position: absolute;
  margin-top: -150px;
  margin-left: 66%;
  overflow: hidden;


}

.box3 h2 {
  font-weight: 400;
  letter-spacing: -1.5px;
  line-height: 1.2;
}

.box3 h3 {
  font: 0.8em "Lucida Grande", serif;
}

.box3:hover {
  background-position: 100% 100%;
  animation: up-bump 0.4s ease;
  height: 400px;
}

.box3:hover h2 {
  color: #48ad26;
}

.box3:hover h2 span {
  color: white;
}

.box3:hover h3 {
  color: #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">

</div>
<div class="responsive-section-image" id="hover-change">
  <div class="overlay"></div>
</div>
<div class="box">
  <a id="hover-01" class="open-project" href="#">
    <h2><span>Lunch -</span> is delicious anytime</h2>
    <h3>Even after dinner</h3>
    <p>
      test content test content
      test content test content
      test content test content
      test content test content
    </p>
  </a>
</div>
<div class="box2">
  <a id="hover-02" class="open-project" href="#">
    <h2><span>Lunch -</span> is delicious anytime</h2>
    <h3>Even after dinner</h3>
    <p>
      test content test content
      test content test content
      test content test content
      test content test content
    </p>
  </a>
</div>
<div class="box3">
  <a id="hover-03" class="open-project" href="#">
    <h2><span>Lunch -</span> is delicious anytime</h2>
    <h3>Even after dinner</h3>
    <p>
      test content test content
      test content test content
      test content test content
      test content test content
    </p>
  </a>
</div>

</div>


如果使用DRY原则会更容易。应该只有一个.box类。 - mplungjan
3个回答

5

另一个解决方案是使用 flexbox。这种方法可以让项目粘着在容器底部,并在悬停时使它们“向上弹出”。

display:flex;添加到容器元素中,将align-self:flex-end;添加到盒子中。

HTML

<div class="wrap">
    <div class="item">Text</div>
    <div class="item">Text</div>
</div>

CSS(层叠样式表)
.wrap {
   width:50%;
   height:80vh;
   display:flex;
}

.item {
   width:50%;
   height:30%;
   align-self:flex-end;
}

.item:hover {
   height:50%;
}

查看一个工作示例,请点击此处


1
因为您知道 .box 扩展前后的确切高度,所以将 translateY 设置为 -(高度差),可以产生 .box 向上扩展的错觉。

换句话说,在所有 .box:hover 上添加以下代码:

transform: translateY(-250px); // Or other difference in height

对于您的代码,在您的 :hover 上添加了一行代码后,这是结果。我还将 #hover-change 的高度更改为 400px,仅仅是因为它无法适应整个 .box 向上扩展到 400px

$('#hover-01').on('mouseenter', function() {
    $('#hover-change').css('background-image', 'url(http://www.w3schools.com/css/trolltunga.jpg)');
  });
  $('#hover-01').on('mouseleave', function() {
    $('#hover-change').css('background-image', 'url(https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4)');
  });

  $('#hover-02').on('mouseenter', function() {
    $('#hover-change').css('background-image', 'url(http://www.w3schools.com/css/trolltunga.jpg)');
  });
  $('#hover-02').on('mouseleave', function() {
    $('#hover-change').css('background-image', 'url(https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4)');
  });

  $('#hover-03').on('mouseenter', function() {
    $('#hover-change').css('background-image', 'url(http://www.w3schools.com/css/trolltunga.jpg)');
  });
  $('#hover-03').on('mouseleave', function() {
    $('#hover-change').css('background-image', 'url(https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4)');
  });
.wrapper {
  position: relative;
}

#hover-change {
  width: 100%;
  height: 400px;
  background-image: url("http://www.w3schools.com/css/trolltunga.jpg");
  background-repeat: no-repeat;
}

@import url(https://fonts.googleapis.com/css?family=Merriweather);

* {
  box-sizing: border-box;
}

body {
  font-family: 'Merriweather', serif;
  padding: 20px;
}

a {
  color: #f06d06;
  text-decoration: none;
}

.box {
  padding: 2em;
  border: 1px solid #ccc;
  display: block;
  width: 33%;
  height: 150px;
  float: left;
  margin: -1px 0 0 -1px;
  color: black;
  background: linear-gradient(white, white 50%, #333 50%, #333);
  background-size: 100% 202%;
  transition: all 0.2s ease;
  animation: down-bump 0.4s ease;
  position: absolute;
  margin-top: -150px;
  overflow: hidden;


}

.box h2 {
  font-weight: 400;
  letter-spacing: -1.5px;
  line-height: 1.2;
}

.box h3 {
  font: 0.8em "Lucida Grande", serif;
}

.box:hover {
  background-position: 100% 100%;
  animation: up-bump 0.4s ease;
  height: 400px;
  transform: translateY(-250px);
}

.box:hover h2 {
  color: #48ad26;
}

.box:hover h2 span {
  color: white;
}

.box:hover h3 {
  color: #999;
}



.box2 {
  padding: 2em;
  border: 1px solid #ccc;
  display: block;
  width: 33%;
  height: 150px;
  float: right;
  margin: -1px 0 0 -1px;
  color: black;
  background: linear-gradient(white, white 50%, #333 50%, #333);
  background-size: 100% 202%;
  transition: all 0.2s ease;
  animation: down-bump 0.4s ease;
  position: absolute;
  margin-top: -150px;
  margin-left: 33%;
  overflow: hidden;


}

.box2 h2 {
  font-weight: 400;
  letter-spacing: -1.5px;
  line-height: 1.2;
}

.box2 h3 {
  font: 0.8em "Lucida Grande", serif;
}

.box2:hover {
  background-position: 100% 100%;
  animation: up-bump 0.4s ease;
  height: 400px;
  transform: translateY(-250px);
}

.box2:hover h2 {
  color: #48ad26;
}

.box2:hover h2 span {
  color: white;
}

.box2:hover h3 {
  color: #999;
}

.box3 {
  padding: 2em;
  border: 1px solid #ccc;
  display: block;
  width: 33%;
  height: 150px;
  float: right;
  margin: -1px 0 0 -1px;
  color: black;
  background: linear-gradient(white, white 50%, #333 50%, #333);
  background-size: 100% 202%;
  transition: all 0.2s ease;
  animation: down-bump 0.4s ease;
  position: absolute;
  margin-top: -150px;
  margin-left: 66%;
  overflow: hidden;


}

.box3 h2 {
  font-weight: 400;
  letter-spacing: -1.5px;
  line-height: 1.2;
}

.box3 h3 {
  font: 0.8em "Lucida Grande", serif;
}

.box3:hover {
  background-position: 100% 100%;
  animation: up-bump 0.4s ease;
  height: 400px;
  transform: translateY(-250px);
}

.box3:hover h2 {
  color: #48ad26;
}

.box3:hover h2 span {
  color: white;
}

.box3:hover h3 {
  color: #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">

</div>
<div class="responsive-section-image" id="hover-change">
  <div class="overlay"></div>
</div>
<div class="box">
  <a id="hover-01" class="open-project" href="#">
    <h2><span>Lunch -</span> is delicious anytime</h2>
    <h3>Even after dinner</h3>
    <p>
      test content test content
      test content test content
      test content test content
      test content test content
    </p>
  </a>
</div>
<div class="box2">
  <a id="hover-02" class="open-project" href="#">
    <h2><span>Lunch -</span> is delicious anytime</h2>
    <h3>Even after dinner</h3>
    <p>
      test content test content
      test content test content
      test content test content
      test content test content
    </p>
  </a>
</div>
<div class="box3">
  <a id="hover-03" class="open-project" href="#">
    <h2><span>Lunch -</span> is delicious anytime</h2>
    <h3>Even after dinner</h3>
    <p>
      test content test content
      test content test content
      test content test content
      test content test content
    </p>
  </a>
</div>

</div>

然而,正如评论区中的另一个人指出的那样,用基本相同的样式命名三个元素是不好的做法。查找DRY原则。

0

<div class="wrapper">后面的</div>需要删除,因为你有一个多余的闭合div。删除这个div将确保这3个盒子与包装器相关,并且当你滚动时,这些盒子仍然会使用position:absolute。

然后,这个CSS将解决你的问题。

.box, .box2, .box3 {
   bottom : 0px;
}

你的悬停CSS中目前高度不正确,会裁剪掉顶部,只需相应地调整数值即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接