简体   繁体   English

拆分幻灯片兄弟,一个兄弟影响 hover 上的另一个

[英]Split slide siblings, one sibling affects the other on hover

I am trying to achieve a similar effect as the Navy.com header where you hover over one sibling and it slides to take up the majority of space while shrinking and hiding the content of the other sibling.我正在尝试实现与 Navy.com header 类似的效果,其中 hover 在一个兄弟姐妹上,它滑动以占用大部分空间,同时缩小和隐藏其他兄弟姐妹的内容。

Because of the CSS standard of not being able to select previous siblings, I am only able to achieve the effect with one side and not the other.由于 CSS 标准无法达到 select 以前的兄弟姐妹,我只能用一侧而不是另一侧来实现效果。 I understand this may not be possible with CSS alone.我知道仅使用 CSS 可能无法做到这一点。

Thank you in advance for your help!预先感谢您的帮助!

Here is the code I am currently using:这是我目前正在使用的代码:

 .container { display: flex; flex-direction: row-reverse; }.b1 { display: flex; order: 2; width: 50%; height: 150px; padding: 10px; background-color: rgb(40, 251, 202); transition: all 0.5s; }.b2 { display: flex; width: 50%; order: 1; height: 150px; padding: 10px; background-color: rgb(227, 29, 103); transition: all 0.5s; }.b1:hover { width: 80%; }.b2:hover { width: 80%; }.b1:hover~.b2 { background-color: rgba(227, 29, 103, 0.4); width: 20%; }.b2:hover~.b1 { background-color: rgba(40, 251, 202, 0.4); width: 20%; }.b1:hover~.b2 span { display: none; }.b2:hover~.b1 span { display: none; }
 <div class="container"> <div class="b2"> <span>This content will show up directly in its container.</span> </div> <div class="b1"> <span>This content will show up directly in its container.</span> </div> </div>

With a bit of restructuring, you would be able to achieve your desired effect with CSS only.通过一些重组,您将能够仅使用 CSS 达到您想要的效果。 In this implementation, I use the :not CSS selector, combined with some multi-level :hover selectors (which works because .container and .child take the same space)在这个实现中,我使用了:not CSS 选择器,并结合了一些多级:hover选择器(因为.container.child占用了相同的空间)

Although, just as a suggestion: take advantage of flex's auto-grow, and remove the width: 20%;虽然,只是作为一个建议:利用 flex 的自动增长,并删除width: 20%; logic entirely.完全符合逻辑。 That way, the unhovered component's children will take equal space, and a hovered child will still grow to be 80% width.这样,未悬停组件的子组件将占用相等的空间,悬停的子组件仍将增长到 80% 的宽度。 This will allow you to dynamically add more items without having to change the hardcoded CSS.这将允许您动态添加更多项目,而无需更改硬编码的 CSS。

 .container { display: flex; }.child { width: 50%; height: 150px; padding: 10px; transition: all 0.5s; }.child--red { background-color: rgba(227, 29, 103, 1); }.container:hover.child--red:not(:hover) { background-color: rgba(227, 29, 103, 0.4); }.child--green { background-color: rgba(40, 251, 202, 1); }.container:hover.child--green:not(:hover) { background-color: rgba(40, 251, 202, 0.4); }.container:hover.child:not(:hover) span { display: none; }.container:hover.child { width: 20%; }.container:hover.child:hover { width: 80%; }
 <div class="container"> <div class="child child--green"> <span>This content will show up directly in its container.</span> </div> <div class="child child--red"> <span>This content will show up directly in its container.</span> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM