悬停时可折叠图标/内容颜色更改
Posted
技术标签:
【中文标题】悬停时可折叠图标/内容颜色更改【英文标题】:Collapsible Icon/Content Color Change on Hover 【发布时间】:2021-11-23 11:22:31 【问题描述】:我正在处理“可折叠”内容(常见问题解答下拉菜单)。在预先单击的“按钮”上,我有一个“加号”图标(按下按钮后图标变为“负号”图标)。我想要的是当按钮悬停在预单击按钮“加号”图标上时改变颜色(即变成白色)。我不确定如何修改 CSS 以在悬停时更改图标。
我试过了:
.collapsible:after, .hover
color: $light;
但这只会扭转问题...在此先感谢您!代码如下。
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++)
coll[i].addEventListener("click", function()
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block")
content.style.display = "none";
else
content.style.display = "block";
);
/* TARGET ICON */
.collapsible:after
content: '\02795'; /* Unicode character for "plus" sign (+) */
color: black;
font-weight: bold;
float: right;
margin-left: 5px;
.active:after
content: "\2796"; /* Unicode character for "minus" sign (-) */
color: white;
/* Style the button that is used to open and close the collapsible content */
.collapsible
background-color: white;
color: black;
cursor: pointer;
padding: 10px 18px;
width: 100%;
border: solid;
border-width: thin;
border-radius: 5px;
margin-bottom: 5px;
text-align: left;
outline: none;
font-size: 24px;
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.active, .collapsible:hover
background-color: black;
color: white;
/* Style the collapsible content. Note: hidden by default */
.content-faq
padding: 0px 22px;
border-radius: 5px;
margin-bottom: 15px;
margin-top: -6px;
max-height: 0;
transition: max-height 0.2s ease-out;
background-color: #f1f1f1;
<button class="collapsible">Some Question</button>
<div class="content-faq">
<p>Some Answer</p>
</div>
【问题讨论】:
【参考方案1】:使用.collapsible:hover::after
选择hover
上使用::after
创建的内容
由于灰色是您使用的符号,它是继承的,因此将其更改为简单的
+
-
,仅用于演示以显示效果。 您可以使用任何符号,但不能继承颜色
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++)
coll[i].addEventListener("click", function()
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block")
content.style.display = "none";
else
content.style.display = "block";
);
/* TARGET ICON */
.collapsible::after
content: '+';
/* Unicode character for "plus" sign (+) */
color: black;
font-weight: bold;
float: right;
margin-left: 5px;
.active:after
content: "\2212";
/* Unicode character for "minus" sign (-) */
color: white;
/* Style the button that is used to open and close the collapsible content */
.collapsible
background-color: white;
color: black;
cursor: pointer;
padding: 10px 18px;
width: 100%;
border: solid;
border-width: thin;
border-radius: 5px;
margin-bottom: 5px;
text-align: left;
outline: none;
font-size: 24px;
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.active,
.collapsible:hover
background-color: black;
color: white;
/* Style the collapsible content. Note: hidden by default */
.content-faq
padding: 0px 22px;
border-radius: 5px;
margin-bottom: 15px;
margin-top: -6px;
max-height: 0;
transition: max-height 0.2s ease-out;
background-color: #f1f1f1;
.collapsible:hover::after
color: white;
<button class="collapsible">Some Question</button>
<div class="content-faq">
<p>Some Answer</p>
</div>
【讨论】:
以上是关于悬停时可折叠图标/内容颜色更改的主要内容,如果未能解决你的问题,请参考以下文章