Design comparison
Solution retrospective
Quite Challenging. I couldn't get the little @ cube to display properly on the desktop version. I also would have liked for only one accordion item to be open at a time, but I am VERY new to implementing Javascript into web pages.
Community feedback
- @amalkarimPosted about 2 years ago
Hi, Dan 👋
Personally, I will recommend using jQuery instead of vanilla JavaScript as it is generally easier to use. But for now, let's tweak existing JavaScript a little bit so it will work according to your favor.
These are a little explanation about the code:
- Instead of using
var
for the variable, let's useconst
for variables that are immutable, andlet
for the mutable variables display
property cannot be animated using CSS. We better useopacity
to make the transition works both when the element shows and hides. In style.css, let's changedisplay: none;
toopacity: 0;
forpanel
class.- We have a few things to consider,
active
class for<div class="accordion">
,maxHeight
andopacity
properties for<div class="panel">
.
const acc = document.getElementsByClassName("accordion"); const panels = document.getElementsByClassName("panel"); for (let i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function() { this.classList.toggle("active"); const panel = this.nextElementSibling; if (panel.style.opacity == 1) { this.classList.remove("active"); panel.style.opacity = 0; panel.style.maxHeight = 0; } else { for (let j = 0; j < panels.length; j++) { acc[j].classList.remove("active"); panels[j].style.opacity = 0; panels[j].style.maxHeight = 0; } this.classList.add("active"); panel.style.opacity = 1; panel.style.maxHeight = panel.scrollHeight + "px"; } }); }
For positioning the cube image, first you have to give the parent element this:
position: relative;
, in this case it's<div class="main-left">
. After that, you can place cube image anywhere inside it, and give itposition: absolute;
and other necessary positioning declarations.Hope this helps
0 - Instead of using
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord