I am making a website for my master’s course, and I wanted to make a vertical clickable accordion mechanism. The ideia is to click in every section and it expands with more information.
So far, I’ve just made it with the hover mechanism using CSS. How can I make it clickable and use JS? If someone could help me, I would really appreciate it.
body {
width : 100vw;
height : 100vh;
margin : 0;
}
ul,
li {
margin : 0;
padding : 0;
list-style : none;
}
ul {
display : flex;
overflow : hidden;
height : 100vh;
width : 100vw;
}
li {
overflow-y : scroll;
overflow : auto;
-webkit-box-flex : 1;
-webkit-flex : 1;
-ms-flex : 1;
flex : 1;
width : 8.3vw;
height : calc(100% - 1%);
-webkit-transition : -webkit-box-flex 500ms ease-out;
-webkit-transition : -webkit-flex 500ms ease-out;
transition : -webkit-box-flex 500ms ease-out;
transition : -ms-flex 500ms ease-out;
transition : flex 500ms ease-out;
padding : 20px;
-webkit-box-shadow : 2px 0px 4px -1px rgba(0, 0, 0, 0.66);
-moz-box-shadow : 2px 0px 4px -1px rgba(0, 0, 0, 0.66);
box-shadow : 2px 0px 4px -1px rgba(0, 0, 0, 0.66);
}
li:hover {
-webkit-box-flex : 30vw;
-webkit-flex : 30vw;
-ms-flex : 30vw;
flex : 30vw;
}
.projects {
width : 30vw;
height : auto;
margin-bottom : 5vh;
}
/* ------- PARAGRAPH STYLES ------- */
a {
text-decoration : none;
color : black;
}
a:hover {
opacity : 0.5;
}
.maintitle {
width : 100%;
font-size : 30px;
text-transform : uppercase;
font-family : Arial, Helvetica, sans-serif;
}
.subtitle {
width : 150vw;
font-size : 20px;
font-family : Arial, Helvetica, sans-serif;
}
img {
padding-top : 20px;
width : 25vh;
height : auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title> MDC 22-23 </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<ul>
<!-- MICRO-1-->
<li ">
<div class="projects ">
<a href="matilde/matilde.html ">
<div class="maintitle "> Networked Infrastructures</div>
<div class="subtitle "> Three geographies (environmental, economic, political)</div>
<img src="gif5.gif "></div>
</a>
</div>
<div class="projects "></div>
<a href="matilde/matilde.html ">
<div class="maintitle "> Networked Infrastructures</div>
<div class="subtitle "> Three geographies (environmental, economic, political)</div>
<img src="gif5.gif "></div>
</a>
</div>
<div class="projects "></div>
<a href="matilde/matilde.html ">
<div class="maintitle "> Networked Infrastructures</div>
<div class="subtitle "> Three geographies (environmental, economic, political)</div>
<img src="gif5.gif "></div>
</a>
</div>
</li>
<!-- PROJECT 2-->
<li>
<div class="projects ">
<a href="matilde/matilde.html ">
<div class="maintitle "> Networked Infrastructures</div>
<div class="subtitle "> Three geographies (environmental, economic, political)</div>
<img src="gif5.gif "></div>
</a>
</div>
<div class="projects "></div>
<a href="matilde/matilde.html ">
<div class="maintitle "> Networked Infrastructures</div>
<div class="subtitle "> Three geographies (environmental, economic, political)</div>
<img src="gif5.gif "></div>
</a>
</div>
<div class="projects "></div>
<a href="matilde/matilde.html ">
<div class="maintitle "> Networked Infrastructures</div>
<div class="subtitle "> Three geographies (environmental, economic, political)</div>
<img src="gif5.gif "></div>
</a>
</div>
</li>
<!-- PROJECT 3 -->
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
</ul>
</body>
</html>
So far, I’ve just made it with the hover mechanism using CSS. How can I make it clickable and use JS? Also could i make every with infinite auto scroll? If someone could help me, I would really appreciate it.
2
Answers
I think this should work. Basically I have just replaced your
li:hover
selector withli.selected
. Now it just uses some simple JS to add/remove thatselected
class when you click on an<li>
tag.NOTE: I dorrected quite a few syntax errors that commentors above have mentioned. I also removed all of those
-webkit-
,-moz-
and-ms-
prefixed items as those are largely no longer needed.your question is vague.
If you have to use a click to select a pane of your accordion, what happens to the hover on the others elements?
Here is an answer that combines the two
the hover has priority over the click, but the pane opened with a click remains open outside of any hovering of the set.
you just have to put as a selector:
Optionnaly you can add
to check the current selected element during any over.
For the JS side, do not neglect the particularities of the method
.classList.toggle()
1- it returns a boolean value
2- it allows optionally to force or not the assignment of a class
full code: