<div class="hamburger menu btn2">
<div class="top"></div>
<div class="middle"></div>
<div class="bottom"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$('.menu').click (function(){
// $(this).toggleClass('clicked');
$(".top").toggleClass('clicked');
$(".middle").toggleClass('clicked');
$(".bottom").toggleClass('clicked');
});
</script>
This code is working 100% fine, but is any room left to minimize this code?
I tried one posisbility it worked:
$('.menu').click (function(){
$(".hamburger div").toggleClass('clicked');
});
2 Likes
PaulOB
April 25, 2021, 12:12pm
3
You donāt need to change the class on every element in that hamburger. Just toggle a single class on the hamburger itself and then css can use that class to change the children.
e.g.
$('.menu').click (function(){
$(".hamburger").toggleClass('clicked');
});
.clicked .middle {
transition-duration: 0.5s;
background: transparent;
}
.clicked .top {
transform: rotateZ(45deg) scaleX(1.25) translate(13px, 13px);
}
.clicked .bottom {
transform: rotateZ(-45deg) scaleX(1.25) translate(12px, -12px);
}
1 Like
I was transitioning from JQuery to JS, but getting a very fundamental error in the console:
<script>
$('.menu').click (function(){
$(".hamburger").toggleClass('clicked');
$("article").toggleClass('hide');
$(".mobilemenu").toggleClass('hide');
});
</script>
<script>
var mobMenu = document.getElementsByClassName("menu");
mobMenu.addEventListener("click", hamBurger);
function hamBurger() {
var hamBurger = document.getElementsByClassName("hamburger");
var article = document.querySelector("article");
var mobileMenu = document.getElementsByClassName("mobilemenu");
hamBurger.classList.toggle('clicked');
article.classList.toggle('hide');
mobileMenu.classList.toggle('hide');
}
</script>
PaulOB
April 25, 2021, 3:21pm
9
codeispoetry:
getElementsByClassName
getElements ByClassName
is a collection of all the elements. If you just have one element with the classname you could say this.
var mobMenu = document.getElementsByClassName("menu")[0];
Or instead just use querySelector for āmodernishā browsers
document.querySelector(".menu");
1 Like
PaulOB
April 25, 2021, 3:27pm
12
codeispoetry:
I already tried that code, but still, I have the same issue
You still have this:
document.getElementsByClassName("menu");
1 Like
PaulOB
April 25, 2021, 3:31pm
13
Referring to my earlier post do you need to keep toggling all those classes to other elements?
If you were for example to add the class to the wrapper or to the body element you can control all those elements (and the hamburger) with the single class (just as I showed for the hamburger).
body.menuOpen .article{display:none}
body.menuOpen .mobileNav{display:block}
etc...
@PaulOB Code has gone slightly far ahead than Hamburger.
Let me explain. Hamburger we are on the same page.
We are using HTML:
only hamburger/menu class will have an additional class on click:
and then the CSS:
But there is a larger picture now. There are two additives:
Part 1
Those hamburger toggling should have two more features associated. On toggling the mobile many should appear for that we need to remove hide class.
Part 2
The content/article part should either be made transparent or hidden. I am using hidden. for that class hide need to go there.
JQuery version will go like this.
$('.menu').click (function(){
$(".hamburger").toggleClass('clicked');
$("article").toggleClass('hide');
$(".mobilemenu").toggleClass('hide');
});
</script>
$(āarticleā).toggleClass(āhideā); will hide article content
$(ā.mobilemenuā).toggleClass(āhideā); will remove hide class and make menu visible.
PaulOB
April 25, 2021, 4:37pm
15
Yes thatās why I suggested you do it all with one class rather than toggling classes all over the place.
Add the clicked class to the body element then thatās all you need to do in JS. Css can handle the hiding and showing of all those things using the clicked class.
body.clicked .article{display:none;}
body.clicked .mobileNav{display:block;}
body.clicked .middle {background:transparent;}
etc....
$('.menu').click (function(){
$("body").toggleClass('clicked');
});
1 Like
Confused! how will you modify this part:
<script>
var mobMenu = document.querySelector(".menu");
mobMenu.addEventListener("click", hamBurger);
function hamBurger() {
var hamBurger = document.querySelector(".hamburger");
var article = document.querySelector("article");
var mobileMenu = document.querySelector(".mobilemenu");
hamBurger.classList.toggle('clicked');
article.classList.toggle('hide');
mobileMenu.classList.toggle('hide');
}
</script>
May be like this:
<script>
var mobMenu = document.querySelector(".menu");
mobMenu.addEventListener("click", hamBurger);
function hamBurger() {
var bodySelect = document.querySelector("body");
bodySelect.classList.toggle('clicked');
}
</script>
1 Like
PaulOB
April 25, 2021, 4:45pm
18
codeispoetry:
May be like this:
Yes much simpler :).
Maybe use const instead of var for the menu as its constant.
const mobMenu = document.querySelector(".menu");
1 Like
+
This one ā Finaly worked.
body.clicked .middle {
background: transparent;
}
body.clicked .top {
transform: rotateZ(45deg) scaleX(1.25) translate(13px, 13px);
}
body.clicked .bottom {
transform: rotateZ(-45deg) scaleX(1.25) translate(7px, -7px);
}
.hamburger:hover {
cursor: pointer;
}
body.clicked .mobilemenu {
display: block;
}
body.clicked .article {
display: none;
}
But the menu and article ON/OFF is very fast and sharp. Not so soothing to eye.
1 Like
PaulOB
April 25, 2021, 5:20pm
20
It should be no different to what you had before (unless you were talking about the jquery version of hide and show).
Iād need to see a fuller demo to work out how apply a transition to the showing and hiding.
1 Like
system
Closed
July 26, 2021, 12:21am
21
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.