我希望在单击按钮时旋转/翻转按钮内的背景图像。背景图像是向下箭头,我希望它翻转为向上箭头。按钮用于可折叠抽屉,因此我希望在抽屉关闭时箭头翻转回来。按钮打开和关闭抽屉,背景图像通过CSS应用。
document.querySelectorAll(".collapsible").forEach((coll) => {
coll.addEventListener("click", () => {
coll.classList.toggle("active");
const content = coll.nextElementSibling;
if (content.style.maxHeight) {
content.style.maxHeight = null;
// Add this:
content.style.borderWidth = 0;
} else {
content.style.maxHeight = content.scrollHeight + "px";
// And this:
content.style.borderWidth = "1px";
}
});
});
function myFunction() {
var element = document.getElementById("toggling");
element.classList.toggle("toggled");
};
.content {
padding: 0 18px;
max-height: 0;
overflow: hidden;
transition:
border 0.2s ease-out,
max-height 0.2s ease-out;
background-color: white;
border: 0px solid #D1D3D4;
border-radius: 3px;
}
.collapsible {
background-color: white;
color: #021032;
cursor: pointer;
padding: 14px;
width: 100%;
border: solid 1px #D1D3D4;
border-radius: 6px;
text-align: left;
outline: none;
font-size: 15px;
margin: 5px 0px;
}
button {
background-image: url("../images/upArrow.svg");
background-repeat: no-repeat;
background-position: right;
background-size: 25px;
font-weight: 900;
}
.toggled {
background-image: url("../images/downArrow.svg");
background-repeat: no-repeat;
background-position: right;
background-size: 25px;
font-weight: 900;
}
<button type="button" class="collapsible" onclick="myFunction()" id="toggling"> Consult Logs </button>
<div class="content">
<div class="column">
<p>Ensure the disc strength is not at “0”.</p>
</div>
<div class="column">
<img src="../images/discStrength.png" alt="Picture of Logs">
</div>
</div>
<button type="button" class="collapsible" disabled> Ensure All Connectors to Nebuliser Boards are Fully Connected </button>
<button type="button" class="collapsible" onclick="myFunction()" id="toggling"> Loose Crimp Issue </button>
<div class="content">
<div class="column">
<p>Check that the wires and crimps are fully inserted into J1, J4, and trandsducer connectors.</p>
</div>
<div class="column">
<img src="../images/looseCrimp.png" alt="Picture of Connectors">
</div>
</div>
1条答案
按热度按时间efzxgjgh1#
我不想打断你的学习之旅,但我想我应该指出,你已经在按钮上设置了一个活动类,这意味着你可以点击那个活动类来旋转你的图标。
下面我创建了一个单独的div,这样当活动类应用于按钮时,我就可以旋转那个div。你也可以用
::after
伪元素来做这件事,并保持你的标记干净。如果你有两个单独的图像,你可以只交换背景图像。希望这能激励你尝试所有这些选项来不断学习新技术。