将鼠标悬停在 DIV 上以展开宽度

新手上路,请多包涵

这是我到目前为止所拥有的 - https://jsfiddle.net/8216Lntb/

 body {
  background-color: black;
  margin: 0 auto;
  height: 100%;
  width: 100%;
}
.grow {
  height: 100vw;
  /* Origional height */
  width: 25%;
  /* Origional width */
  margin: 0px 0 0px 0;
  /* Just for presentation (Not required) */
  float: left;
  /* Just for presentation (Not required) */
  position: relative;
  /* Just for presentation (Not required) */
  transition: height 0.5s;
  /* Animation time */
  -webkit-transition: height 0.5s;
  /* For Safari */
}
.grow:hover {
  width: 25%;
  /* This is the height on hover */
}
 <html>

<head>
  <title>HOMEPAGE</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="style.css" media="screen,projection" />
</head>

<body>
  <div class="grow" style="background-color:#2A75A9;"></div>
  <div class="grow" style="background-color:#274257;"></div>
  <div class="grow" style="background-color:#644436;"></div>
  <div class="grow" style="background-color:#8F6048;"></div>
  <!--<div class="grow" style="background-color:red;"></div>-->
</body>

</html>

我想要实现的是这个 https://artsandculture.withgoogle.com/en-us/national-parks-service/parks

每次我将鼠标悬停在一个 div 上时,它都会从页面上删除一个,因为它已经超过 100%。

我的问题是我该怎么做,以便当一个 div 展开时,其他的 div 会变小,所以它们都停留在一页上

原文由 himynameis 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 468
2 个回答

我认为 你不需要javascript

 html,body{
	margin: 0 auto;
	height: 100%;
	width: 100%;
}
body {
	background-color: black;
}
#growContainer{
	display: table;
	width:100%;
	height:100%;
}
.grow{
	display: table-cell;
	height:100%;
	width: 25%;
	-webkit-transition:width 500ms;
	-moz-transition:width 500ms;
	transition:width 500ms;
}
#growContainer:hover .grow{
	width:20%;
}
#growContainer:hover .grow:hover {
	width:40%;
}
 <div id="growContainer">
<div class="grow" style="background-color:#2A75A9;"></div>
<div class="grow" style="background-color:#274257;"></div>
<div class="grow" style="background-color:#644436;"></div>
<div class="grow" style="background-color:#8F6048;"></div>
</div>

原文由 Vixed 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用 toggleClass() 轻松解决它

试试这个:

 $(function() {
    $('div').hover(function() {
        $(this).toggleClass('expand');
        $('div').not(this).toggleClass('shrink');
    });
});
 body {
    background-color: black;
    margin: 0 auto;
    height: 100%;
    width: 100%;
}
.grow {
    height: 100vw; /* Origional height */
    width: 25%; /* Origional width */
    margin: 0px 0 0px 0; /* Just for presentation (Not required) */
    float: left; /* Just for presentation (Not required) */
    position: relative; /* Just for presentation (Not required) */
    -transition-duration:0.5s;
    -webkit-transition-duration:0.5s;
    -moz-transition-duration:0.5s;
}

.expand{
    width: 40%;
}
.shrink{
    width: 20%;
}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="grow" style="background-color:#2A75A9;"></div>
<div class="grow" style="background-color:#274257;"></div>
<div class="grow" style="background-color:#644436;"></div>
<div class="grow" style="background-color:#8F6048;"></div>

原文由 Hitesh Misro 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题