我想要2个div盒子,每个盒子宽度为50%,高度为100%..在div盒子里面,它就像一个锚链接,我们将鼠标悬停在第一个盒子上(方框A),方框A将从左到右从50%宽度过渡到100%宽度。如果我们将鼠标悬停在方框B上,它也会从右向左过渡并将宽度填满100%。
我不确定如何使用CSS或javascript / jquery或两者兼而有之?如果有人能帮助我,我将不胜感激。
谢谢:)
答案 0 :(得分:4)
这是一个使用flexbox的方法。
为了让悬停效果适用于上一个兄弟,我改变了HTML中的顺序。可能有一种更简单的方法可以做到这一点,但我无法解决它..
a.index(k)

.container {
display: flex;
height: 200px;
border: 2px solid grey;
overflow: hidden;
box-sizing: border-box;
}
.block {
background: pink;
flex: 1;
margin: 10px;
/* below is styling for demo */
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.b {
order: 2;
}
.block:hover {
min-width: calc(100% - 20px);
}
.block:first-of-type:hover+.block {
display: none;
}

答案 1 :(得分:3)
Flexbox可以做任何事情(差不多):
* {
box-sizing: border-box;
}
body,html {
margin:0;
height: 100vh;
}
body {
display: flex;
align-items:center;
justify-content: center;
}
.container {
border: 7px solid #999;
height: 200px;
width: 500px;
background-color: lightgreen;
display: flex;
}
.left, .right {
height: 100%;
transition: all 0.5s;
flex: 0 1 50%;
}
.left:hover, .right:hover {
flex: 1 0 100%;
}
.left {
background-color: lightsalmon;
}
.right {
background-color: mediumpurple;
}

<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
&#13;
答案 2 :(得分:2)
在CSS上,你可以做这样的事情:
.container .box_A,
.container .box_B {
width: 50%;
overflow: hidden;
height: 100%;
-webkit-font-smoothing: antialiased;
-webkit-transition: all .3s;
-moz-transition: all .3s;
-o-transition: all .3s;
transition: all .3s;
}
.container:hover .box_A ,
.container:hover .box_B {
width: 0%;
}
.container:hover .box_A:hover ,
.container:hover .box_B:hover {
width: 100%;
}
对于像这样的HTML:
<div class="container">
<div class="box_A"></div>
<div class="box_B"></div>
</div>
答案 3 :(得分:0)
有一些jquery
$(".a").on("mouseover", function () {
$(".a").css("width" , "100%");
$(".b").css("width" , "0");
});
$(".b").on("mouseover", function () {
$(".b").css("width" , "100%");
$(".a").css("width" , "0");
});
$(".a").on("mouseout", function () {
$(".a").css("width" , "50%");
$(".b").css("width" , "50%");
});
$(".b").on("mouseout", function () {
$(".a").css("width" , "50%");
$(".b").css("width" , "50%");
});
&#13;
body, html {height:100%; margin:0;}
.a {
background-color:red;
width:50%; height:100%;
float:left;
transition: all .3s;
}
.b {
background-color:blue;
width:50%; height:100%;
float:left;
transition: all .3s;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="a"></div>
<div class="b"></div>
&#13;
答案 4 :(得分:0)
您可以像这样使用CSS:
.parent {
border: 2px solid black;
height: 3em;
position: relative;
width: 10em;
}
.left {
background: red;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 50%;
z-index: 1;
transition: width 1s;
}
.left:hover {
width: 100%;
z-index: 2;
transition: width 1s;
}
.right {
background: blue;
height: 100%;
position: absolute;
right: 0;
top: 0;
width: 50%;
z-index: 1;
transition: width 1s;
}
.right:hover {
transition: width 1s;
width: 100%;
z-index: 2;
}
用一个小提琴来说明例子:Fiddle
答案 5 :(得分:0)
对于这样的CSS:
.ParentDiv{
border:1px solid #000;
height:200px;
width:300px;
margin:0 auto;
position: relative;
}
.Box_A {
width:50%;
height:100%;
text-align:center;
left:0;
background: #ff3232;
transition:all 2s ease;
position: absolute;
}
.Box_A:hover {
width:100%;
background: #fff;
z-index: 2;
display:block;
}
.Box_A > a {
display: block;
height: 100%;
}
.Box_B {
width:50%;
height:100%;
text-align:center;
right:0;
background: #ff3232;
transition:all 2s ease;
position: absolute;
border-left:1px solid gray;
}
.Box_B:hover {
width:100%;
background: #fff;
z-index: 2;
display:block;
}
.Box_B > a {
display: block;
height: 100%;
}
<div class="ParentDiv">
<div class="Box_A"><a href="">Box A</a></div>
<div class="Box_B"><a href="">Box B</a></div>
</div>
<!--End ParentDiv-->
答案 6 :(得分:0)
对于以下HTML:
<body>
<div class = "left" id = "left">
</div>
<div class = "right" id = "right">
</div>
</body>
CSS:
body {
background-color: #ffeead;
}
.left:hover,
.right:hover{
width: 60%;
}
.left:hover ~ #right {
width: 40%;
}
.left {
background-color: #ff6f69;
position: absolute;
left: 0px;
top: 0px;
height: 100%;
width: 50%;
transition: width 1s;
}
.right {
background-color: #ffeead;
position: absolute;
right: 0px;
top: 0px;
height: 100%;
width: 50%;
transition: width 1s;
}