2个div盒子并排,50%宽度和100%高度,在每个盒子上悬停时扩展到100%填满整个宽度

问题描述 投票:3回答:7

我想要有2个div盒子,每个盒子宽度为50%,高度为100%..在div盒子里面,它就像一个锚链接,我们将鼠标悬停在第一个盒子上(方框A),方框A将从从左到右,宽度为50%,宽度为100%。如果我们将鼠标悬停在方框B上,它也会从右向左过渡并将宽度填满100%。

也许一张图片可以更好地解释..这是最初的状态:Initial State (without hover)

然后当鼠标悬停在左边的方框A上时:box a with hover

然后当鼠标悬停在右边的方框B上时:Box b with hover

我不确定如何使用CSS或javascript / jquery或两者兼而有之?如果有人能帮助我,我将不胜感激。

谢谢 :)

jquery css css3 css-transitions
7个回答
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>

4
投票

这是一个使用flexbox的方法。

为了让悬停效果能够在之前的兄弟上工作,我已经颠倒了HTML中的顺序。可能有一种更简单的方法可以做到这一点,但我无法解决它..

.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;
}
<div class="container">
  <div class="block b">B</div>
  <div class="block a">A</div>
</div>

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>

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%");
});
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;
  }  
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="a"></div>
<div class="b"></div>

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


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-->

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.