在悬停时将 flex children 扩展到全宽
expand flex children to full width on hover
在下面的示例中,是否可以仅使用 css 将 flex 子项在悬停时展开为全宽,或者我应该使用 javascript 来实现?
注意:在这种情况下使用 flexbox 很重要。
如果无法使用 css 创建它,您能否建议针对此问题的 javascript 功能?
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'sans-serif';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*, *:before, *:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
position: relative;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255,255,255,0.1);
color: white;
text-align: center;
align-items: center;
transition:
transform 0.2s,
flex 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.panel a{
color:#fff;
font-weight: bold;
font-size: 20px;
text-decoration: none;
text-shadow: 1px 2px rgba(0,0,0,.7);
}
.panel:hover{
flex:2
}
.panel1 { background-image:url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500); }
.panel2 { background-image:url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500); }
.panel3 { background-image:url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d); }
.panel4 { background-image:url(https://source.unsplash.com/ITjiVXcwVng/1500x1500); }
.panel5 { background-image:url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500); }
<div class="panels">
<div class="panel panel1"><a href="">link1</a></div>
<div class="panel panel2"><a href="">link2</a></div>
<div class="panel panel3"><a href="">link3</a></div>
<div class="panel panel4"><a href="">link4</a></div>
<div class="panel panel5"><a href="">link5</a></div>
</div>
你可以做到
.panel:hover{ flex-basis: 100%; }
它会把剩下的缩小到最小,把悬停的扩大。检查这是否有帮助。
解决方法:
隐藏悬停时的所有子项,鼠标指针下方的子项除外
可以用纯css:
.panels:hover .panel:not(:hover) {
display: none;
}
这是更新后的代码段:
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'sans-serif';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*, *:before, *:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
position: relative;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255,255,255,0.1);
color: white;
text-align: center;
align-items: center;
transition:
transform 0.2s,
flex 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.panel a{
color:#fff;
font-weight: bold;
font-size: 20px;
text-decoration: none;
text-shadow: 1px 2px rgba(0,0,0,.7);
}
.panel:hover{
flex: 2;
}
.panel1 { background-image:url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500); }
.panel2 { background-image:url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500); }
.panel3 { background-image:url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d); }
.panel4 { background-image:url(https://source.unsplash.com/ITjiVXcwVng/1500x1500); }
.panel5 { background-image:url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500); }
.panels:hover .panel:not(:hover) {
display: none;
}
<div class="panels">
<div class="panel panel1"><a href="">link1</a></div>
<div class="panel panel2"><a href="">link2</a></div>
<div class="panel panel3"><a href="">link3</a></div>
<div class="panel panel4"><a href="">link4</a></div>
<div class="panel panel5"><a href="">link5</a></div>
</div>
将 min-width:0;overflow:hidden;
添加到所有面板,然后在悬停时使用 flex-basis:100%
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'sans-serif';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
position: relative;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
color: white;
text-align: center;
align-items: center;
transition: transform 0.2s, flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11), background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
min-width:0;
overflow:hidden;
display: flex;
flex-direction: column;
justify-content: center;
}
.panel a {
color: #fff;
font-weight: bold;
font-size: 20px;
text-decoration: none;
text-shadow: 1px 2px rgba(0, 0, 0, .7);
}
.panel:hover {
flex-basis: 100%;
}
.panel1 {
background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}
.panel2 {
background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}
.panel3 {
background-image: url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d);
}
.panel4 {
background-image: url(https://source.unsplash.com/ITjiVXcwVng/1500x1500);
}
.panel5 {
background-image: url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500);
}
<div class="panels">
<div class="panel panel1"><a href="">link1</a></div>
<div class="panel panel2"><a href="">link2</a></div>
<div class="panel panel3"><a href="">link3</a></div>
<div class="panel panel4"><a href="">link4</a></div>
<div class="panel panel5"><a href="">link5</a></div>
</div>
在下面的示例中,是否可以仅使用 css 将 flex 子项在悬停时展开为全宽,或者我应该使用 javascript 来实现? 注意:在这种情况下使用 flexbox 很重要。 如果无法使用 css 创建它,您能否建议针对此问题的 javascript 功能?
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'sans-serif';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*, *:before, *:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
position: relative;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255,255,255,0.1);
color: white;
text-align: center;
align-items: center;
transition:
transform 0.2s,
flex 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.panel a{
color:#fff;
font-weight: bold;
font-size: 20px;
text-decoration: none;
text-shadow: 1px 2px rgba(0,0,0,.7);
}
.panel:hover{
flex:2
}
.panel1 { background-image:url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500); }
.panel2 { background-image:url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500); }
.panel3 { background-image:url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d); }
.panel4 { background-image:url(https://source.unsplash.com/ITjiVXcwVng/1500x1500); }
.panel5 { background-image:url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500); }
<div class="panels">
<div class="panel panel1"><a href="">link1</a></div>
<div class="panel panel2"><a href="">link2</a></div>
<div class="panel panel3"><a href="">link3</a></div>
<div class="panel panel4"><a href="">link4</a></div>
<div class="panel panel5"><a href="">link5</a></div>
</div>
你可以做到
.panel:hover{ flex-basis: 100%; }
它会把剩下的缩小到最小,把悬停的扩大。检查这是否有帮助。
解决方法: 隐藏悬停时的所有子项,鼠标指针下方的子项除外
可以用纯css:
.panels:hover .panel:not(:hover) {
display: none;
}
这是更新后的代码段:
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'sans-serif';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*, *:before, *:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
position: relative;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255,255,255,0.1);
color: white;
text-align: center;
align-items: center;
transition:
transform 0.2s,
flex 0.7s cubic-bezier(0.61,-0.19, 0.7,-0.11),
background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.panel a{
color:#fff;
font-weight: bold;
font-size: 20px;
text-decoration: none;
text-shadow: 1px 2px rgba(0,0,0,.7);
}
.panel:hover{
flex: 2;
}
.panel1 { background-image:url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500); }
.panel2 { background-image:url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500); }
.panel3 { background-image:url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d); }
.panel4 { background-image:url(https://source.unsplash.com/ITjiVXcwVng/1500x1500); }
.panel5 { background-image:url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500); }
.panels:hover .panel:not(:hover) {
display: none;
}
<div class="panels">
<div class="panel panel1"><a href="">link1</a></div>
<div class="panel panel2"><a href="">link2</a></div>
<div class="panel panel3"><a href="">link3</a></div>
<div class="panel panel4"><a href="">link4</a></div>
<div class="panel panel5"><a href="">link5</a></div>
</div>
将 min-width:0;overflow:hidden;
添加到所有面板,然后在悬停时使用 flex-basis:100%
html {
box-sizing: border-box;
background: #ffc600;
font-family: 'sans-serif';
font-size: 20px;
font-weight: 200;
}
body {
margin: 0;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.panels {
min-height: 100vh;
overflow: hidden;
display: flex;
position: relative;
}
.panel {
background: #6B0F9C;
box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.1);
color: white;
text-align: center;
align-items: center;
transition: transform 0.2s, flex 0.7s cubic-bezier(0.61, -0.19, 0.7, -0.11), background 0.2s;
font-size: 20px;
background-size: cover;
background-position: center;
flex: 1;
min-width:0;
overflow:hidden;
display: flex;
flex-direction: column;
justify-content: center;
}
.panel a {
color: #fff;
font-weight: bold;
font-size: 20px;
text-decoration: none;
text-shadow: 1px 2px rgba(0, 0, 0, .7);
}
.panel:hover {
flex-basis: 100%;
}
.panel1 {
background-image: url(https://source.unsplash.com/gYl-UtwNg_I/1500x1500);
}
.panel2 {
background-image: url(https://source.unsplash.com/rFKUFzjPYiQ/1500x1500);
}
.panel3 {
background-image: url(https://images.unsplash.com/photo-1465188162913-8fb5709d6d57?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&w=1500&h=1500&fit=crop&s=967e8a713a4e395260793fc8c802901d);
}
.panel4 {
background-image: url(https://source.unsplash.com/ITjiVXcwVng/1500x1500);
}
.panel5 {
background-image: url(https://source.unsplash.com/3MNzGlQM7qs/1500x1500);
}
<div class="panels">
<div class="panel panel1"><a href="">link1</a></div>
<div class="panel panel2"><a href="">link2</a></div>
<div class="panel panel3"><a href="">link3</a></div>
<div class="panel panel4"><a href="">link4</a></div>
<div class="panel panel5"><a href="">link5</a></div>
</div>