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

him*_*eis 17 html css jquery

这是我到目前为止 - 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 */
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

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

每当我将鼠标悬停在div上时,它将从页面中删除一个,因为它超过了100%.

我的问题是如何做到这一点,当一个div扩展时,其他div变得更小,所以它们都停留在一个页面上

Vix*_*xed 32

我认为你不需要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%;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Hit*_*sro 17

你可以轻松解决它 toggleClass()

试试这个:

$(function() {
    $('div').hover(function() {
        $(this).toggleClass('expand');
        $('div').not(this).toggleClass('shrink');
    });
});
Run Code Online (Sandbox Code Playgroud)
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%;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Gus*_*Wal 14

如果您对使用没问题flexbox,这可能是您正在寻找的解决方案.

html, body{
  height: 100%;
  margin: 0;
}
.grow{
  display: flex;
  justify-content: center;
  height: 100%;
  overflow-x: hidden;
}
.grow > div{
  flex-basis: 25%;
  flex-shrink: 0;
  transition: .5s;
}
.grow > div:hover{
  flex-basis: 40%;
}
.grow > div:first-child:hover{
  margin-left: 15%;
}
.grow > div:last-child:hover{
  margin-right: 15%;
}
Run Code Online (Sandbox Code Playgroud)
<div class="grow">
  <div style="background-color:#2A75A9;"></div>
  <div style="background-color:#274257;"></div>
  <div style="background-color:#644436;"></div>
  <div style="background-color:#8F6048;"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

当中间的两个中的一个正在盘旋时,这段代码实际上隐藏了左右框,就像你展示的网站一样.它也只是CSS,所以这是一个加号!

所以容器是a flexbox,它的子节点总是居中,它们flex-basis是25%,它们不能收缩(flex-shrink: 0),这意味着当其中一个生长时,它们会溢出.

然后当第一个或最后一个被徘徊时,它们会增加15%的额外余量,让它们流回容器.

希望这可以帮助