gpt4 book ai didi

CSS反向过渡并选择 previous sibling

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:54 29 4
gpt4 key购买 nike

Bootstrap容器横向有两种形式。将鼠标悬停在 Form1 上将增加宽度并减少 form2 的宽度。但逆转并没有发生。请提出任何建议。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<style>
#row{
background-color: lightblue;
border-style: groove;
}

#form1{
background-color: lightgreen;
width: 50%;
transition: width 0.75s ease;
}

#form1:hover{
width: 75%;
}

#form1:hover + #form2{
width: 25%
}

#form2{
background-color: orange;
width: 50%;
transition: width 0.75s ease;
}

#form2:hover{
width: 75%;
}

#form2:hover ~ #form1{
width: 25%
}
</style>


</head>
<body>

<div class="container" >

<div id="row" class="row" >
<div id="form1" class="col-sm-6">
<!-- form1 -->
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="Enter email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Enter password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
<div id="form2" class="col-sm-6">
<!-- form 2 -->
<form class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="Enter email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" placeholder="Enter password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</div>

</div>

</body>
</html>

请看下图。当悬停在绿色表格上时,表格从左到右移动,橙色表格减小宽度,绿色表格增加宽度,但当悬停在橙色表格(反向)上时不会发生这种情况。

enter image description here

基本上我正在尝试实现像 THIS 这样的形式

最佳答案

您可以使用 flexbox 来做到这一点。正如 Selva 所提到的,CSS 只能选择下一个兄弟,而不能选择前一个。然而,使用 flexbox 这要容易得多:

.flexible{display: flex; flex-direction: row;}
#form1:hover{
width: 100%;
}
#form2:hover{
width: 100%;
}

https://jsfiddle.net/2g4krj0f/

关于CSS反向过渡并选择 previous sibling ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493217/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com