html - CSS OnHover - 改变另一个元素的属性以及它本身
问题描述
我正在处理侧边栏菜单并希望它部分折叠,这意味着我必须在悬停时显示文本并在不悬停时隐藏文本。我知道另一个问题是关于在悬停时更改另一个元素的属性,但我在更改自身和另一个属性时遇到了麻烦。
一般 HTML 布局:
.sidebar {
position: fixed;
top: 0;
left: 0;
background-color: #1d326b;
height: 100%;
width: 60px;
transition: 0.3s;
border-radius: 0 5px 5px 0;
font-family: 'Open Sans', sans-serif;
overflow: hidden;
}
.sidebar:hover > .text {
display: block; /*Supposed to display text*/
width: 150px; /*Expands the sidebar*/
}
<div class="sidebar">
<!--more containers...-->
<!--text below is deeply nested-->
<p class="text">Displayed Text</p>
</div>
这个问题有纯css解决方案吗?任何帮助,将不胜感激!
解决方案
我认为您要实现的是宽度动画,如果您想要> .text
从悬停选择器中删除它:
.sidebar {
position: fixed;
top: 0;
left: 0;
background-color: #1d326b;
height: 100%;
width: 60px;
transition: 0.3s;
border-radius: 0 5px 5px 0;
font-family: 'Open Sans', sans-serif;
overflow: hidden;
color: #FFF;
}
.sidebar:hover {
display: block; /*Supposed to display text*/
width: 150px; /*Expands the sidebar*/
}
.text {
width: 150px;
display: none;
}
.sidebar:hover .text {
display: block;
}
<div class="sidebar">
<!--more containers...-->
<!--text below is deeply nested-->
<p class="text">Displayed Text</p>
</div>
推荐阅读
- mongodb - 猫鼬地理定位+距离查询
- typescript - 如何声明 TS 类型,它是一个对象数组,其属性的值类型为数字或字符串?
- php - PHP命名空间函数冲突
- javascript - Mongoose 在插入文档时为字段调用函数
- javascript - 以与另一个数组 JavaScript 相同的方式对一个数组进行排序
- java - Mockito @Mock 使用单个注释的多个对象
- javascript - 如何使用 Scroll Magic 插件延迟 GSAP 动画
- javascript - 未定义值时如何捕获 Json 数据错误?
- flutter - 断言失败:布尔表达式不能为空,范围模型在颤振
- xcode - Scenekit:仅在初始接触时运行操作