简体   繁体   English

增加悬停时的高度并将元素推入顶部和底部

[英]Increase height on hover and push elements top and bottom

I have 3 equal elements on the page (33.333% height each), when the user hovers over one of the elements I would like the height of that element to grow to 100%. 我在页面上有3个相等的元素(每个元素的高度为33.333%),当用户将鼠标悬停在其中一个元素上时,我希望该元素的高度增加到100%。

The first section works as I'd like it to, the 2nd section I would like it to grow from the top and bottom, pushing the elements above and below off of the screen - I can do this with absolute positioning, but then the elements above and below stay in the same place and the element grows over them (with the right z-index). 第一部分按照我的意愿进行工作,第二部分则希望它从顶部和底部开始增长,将屏幕上方和下方的元素推离屏幕-我可以通过绝对定位来做到这一点,但是接下来是元素上方和下方的位置保持不变,并且元素在上方生长(具有正确的z索引)。

I've tried scaling the element, and using borders that increase/decrease, however I will be using background images and potentially videos so this won't work. 我尝试缩放元素,并使用增加/减少的边框,但是我将使用背景图像和可能的视频,因此将无法使用。

Here is a jsfiddle of what I have currently: 这是我目前拥有的东西:

 body { height: 100%; overflow: hidden; } .home-split { height: 100vh; } .home-split .item { height: 33.333%; width: 100%; transition: all 1s; z-index: 999; text-align: center; } .h-100 { height: 100%; } .home-split .item:hover { height: 100%; transition: all 1s; z-index: 9990; top: 0 !important; } .home-split .item .title { align-self: center; } .home-split .item a { text-decoration: none; color: #FFF; display: table; height: 100%; width: 100%; } .home-split .item a h2 { display: table-cell; vertical-align: middle; } .home-split .item:nth-child(1) { background-color: #000; } .home-split .item:nth-child(2) { background-color: #d7d7d7; } .home-split .item:nth-child(3) { background-color: #ebebeb; } 
 <section class="home-split"> <div class="row no-gutters item"> <div class="col-12 text-center h-100"> <a href="#"> <h2>Item 1</h2> </a> </div> </div> <div class="row no-gutters item"> <div class="col-12 text-center h-100"> <a href="#"> <h2>Item 2</h2> </a> </div> </div> <div class="row no-gutters item"> <div class="col-12 h-100"> <a href="#"> <h2>Item 3</h2> </a> </div> </div> </section> 

https://jsfiddle.net/d81mxuL5/ https://jsfiddle.net/d81mxuL5/

You need a less specific selector to hide all sections when the outer container is hovered: 当外部容器悬停时,您需要一个不太具体的选择器来隐藏所有部分:

.home-split:hover .item {
  height: 0;
  overflow: hidden;
}

friend I use JQuery to achive 我用JQuery实现的朋友

 $(".item").hover(function() { $(this).siblings().css( "height", "1px"); $(this).css( "height", "98%"); }, function() { $(this).siblings().css( "height", "33%"); $(this).css( "height", "33%"); }); 
 body { height: 100%; overflow: hidden; } .home-split { height: 100vh; } .home-split .item { height: 33.333%; width: 100%; transition: all 1s; z-index: 999; text-align: center; overflow: hidden } .h-100 { height: 100%; } .home-split .item:hover { height: 100%; transition: all 1s; z-index: 9990; top: 0 !important; } .home-split .item .title { align-self: center; } .home-split .item a { text-decoration: none; color: #FFF; display: table; height: 100%; width: 100%; } .home-split .item a h2 { display: table-cell; vertical-align: middle; } .home-split .item:nth-child(1) { background-color: #000; } .home-split .item:nth-child(2) { background-color: #d7d7d7; } .home-split .item:nth-child(3) { background-color: #ebebeb; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section class="home-split"> <div class="row no-gutters item"> <div class="col-12 text-center h-100"> <a href="#"> <h2>Item 1</h2> </a> </div> </div> <div class="row no-gutters item"> <div class="col-12 text-center h-100"> <a href="#"> <h2>Item 2</h2> </a> </div> </div> <div class="row no-gutters item"> <div class="col-12 h-100"> <a href="#"> <h2>Item 3</h2> </a> </div> </div> </section> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM