you can add the “display: none” property to everything that you’d like to collapse in the css media query. i recommend putting the menu list in a div tag. That way you’ll only have to hide the div tag.
Then in javascript you can use the click event on the Categories. When a user clicks the categories, “display: block” property gets added to to the menu list.
Heres some javascript that you can use:
// First get the categories element from html
const categories = document.getElementById('categories');
// Keep track if the menu is opened or not
let menuOpen = false;
// Also get the menu list that you should put in a div tag
const menuList = document.getElementById('menuList');
// Add event to th categories
categories.addEventListener('click', () => {
// check is the menu is open or not
if(!menuOpen) {
// Its not opened than add display block to menu list
menuList.style.display = 'block'
// Change menuOpen to true
menuOpen = true
} else {
// Menu is already open
menuList.style.display = 'none'
// Change menuOpen to false
menuOpen = false
}
});
I assume that you want to add some transitions to the menu list to make it look nice. I recommend using the transition css property.
i’ve never made a website before so i’m kind of winging it. i’m using html+css+java and i haven’t had a problem until now and i’m kind of stumped.
i have this menu for my desktop view (http://puu.sh/H6daI/f9c757571f.png) which functions exactly as i want it to. however in mobile view (http://puu.sh/H6dba/27b97bc952.png), i’d like everything under “categories” to collapse and use the pointer to expand the contents.
i don’t really know how to go about this and i’m not finding much help online. i’d imagine i’d have to incorporate javascript like i did for the sidebar nav in mobile view, but it’s not coming together.
here’s my html and css, if that helps any
<div class="sidemenu">
<ul>
<li class="init" > <embed src="images/pointer.svg" style="width: 100%;" ></embed></li>
<li><a style="font-weight: 500; pointer-events:none; font-size: 16px">Categories</a></li>
<span class="line"></span>
<li><a href="shop.html" >All Sets</a></li>
<li><a href="featured.html" >Featured Sets</a></li>
<li><a href="popular.html" >Popular Sets</a></li>
</li>
</ul>
</div>
.init {
width: 2.5%;
}
.sidemenu ul li.active a, .sidemenu ul li a:hover {
text-decoration:none;
color:rgb(82, 82, 82);
background:#ffcccc no-repeat center top;
}
.sidemenu a {
text-decoration:none;
color:rgb(0, 0, 0);
margin: 48px;
line-height: 48px;
font-weight: 400;
cursor: pointer;
font-size: 14px;
}
@media only screen and (min-width: 950px) {
.line {
border-bottom: 1.5px solid rgb(58, 58, 58);
display: block;
width: 140px;
margin-left: 46px;
margin-bottom: 12px;
}
.sidemenu a {
text-decoration:none;
color:rgb(0, 0, 0);
margin: 48px;
line-height: 48px;
font-weight: 400;
cursor: pointer;
}
}
yes using the div like that will make it easier to select the list in css and javascript. You should put the id like this:
ok that’s what i thought. the list is collapsed and all that is visible is the text “categories” and the arrow. in css is set “cursor: pointer” on the arrow, but nothing is happening when i click on it and there is nothing else clickable. i may be missing something? thanks for the help so far, i’m getting somewhere at least. edit: i took out “pointer events: none” and the categories text becomes clickable to expand the list, but i want the arrow to do it.
ok i got it working in mobile now! but when i switch to desktop view all the contents of class=menuList disappears. is there something in js i can set to make it so that it just displays the content of the list regardless in desktop view? i only want it to be able to toggle in mobile.
Ok, if you want just the arrow to do it then add the id=”categories” to the arrows Li tag. Make sue to add