Is there a better way to add/remove classes for menu with Javascript











up vote
0
down vote

favorite












This is what I have so far..



icons.addEventListener('click', (e)=>{
if(e.target.className==="skate"){
navigation.classList.remove('slideIn');
navigation.classList.add('slideOut');
skateboard.classList.add('skateOff');
x.classList.add('xslide');
}else{
navigation.classList.remove('slideOut');
navigation.classList.add('slideIn');
skateboard.classList.remove('skateOff');
x.classList.remove('xslide');
}
})

@keyframes skateOff{
0%{
transform:rotate(0);
}
50%{
transform:rotate(49deg);
}
100%{
transform: translateX(-300px);
}
}

.skateOff{
animation: skateOff ease-in 1s forwards;
}

.x{
visibility: hidden;
}

.xslide{
animation:slideOut .8s ease 2s forwards;
}


.menu{
visibility: hidden;
color:black;
width:10em;
background-color:white;
border-radius: 4px;
font-family: 'Raleway';
background: linear-gradient(to right,
rgba(249,107,142,1),
rgba(218,103,230,1),
rgba(130,125,253,1));
}
.slideOut{
animation: slideOut 1s forwards 1.2s;
}

@keyframes slideOut{
0%{
transform: translateX(-50%);
}

100%{
visibility: visible;
transform: translateX(0);
}
}

.slideIn{
animation: slideIn 2s ease forwards;

}

@keyframes slideIn{
0%{
visibility: visible;
transform: translateX(0);
}

100%{
transform: translateX(-150%);
}
}


The functionality of the JS is this,
when "skateboard" is clicked, it animates out, to the left and is no longer visible (the skateOff keyframes makes that happen, and i added a class that has that animation implemented also called ".skateOff")



(would it be better to not have a separate class and just add



skateboard.style.animation="animation: skateOff ease-in 1s forwards"?)



..anyway
then after "skateboard" animates out the "navigation"(which is a sidebar menu) adds the "slideOut" class which makes it slide out from the left, along with this the "X" to close the menu slide out, when that is clicked the "navigation"'s class of "slideOut" gets removed and the class of "slideIn" gets added.



This way of doing things seems inefficient, and like a lot of code, I was wondering if there's a simpler way of doing this? Toggling maybe? I've looked into toggling but i'm not sure it will work since the "navigation" element's initial state doesn't have the "slideIn" or "slideOut" class.



ANY tips will be greatly appreciated, thank you for reading and have a great day.










share|improve this question







New contributor




m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Hey! Welcome! You may find that codereview.stackexchange.com is a better place for this, as Stack Overflow is for definable answers. An answer to a question should be ‘right’ or ‘wrong’, but there’s more than one way of doing this. I would look into using a JavaScript framework if I were you, something like VueJS. Good luck.
    – Thomas Edwards
    Nov 12 at 16:28

















up vote
0
down vote

favorite












This is what I have so far..



icons.addEventListener('click', (e)=>{
if(e.target.className==="skate"){
navigation.classList.remove('slideIn');
navigation.classList.add('slideOut');
skateboard.classList.add('skateOff');
x.classList.add('xslide');
}else{
navigation.classList.remove('slideOut');
navigation.classList.add('slideIn');
skateboard.classList.remove('skateOff');
x.classList.remove('xslide');
}
})

@keyframes skateOff{
0%{
transform:rotate(0);
}
50%{
transform:rotate(49deg);
}
100%{
transform: translateX(-300px);
}
}

.skateOff{
animation: skateOff ease-in 1s forwards;
}

.x{
visibility: hidden;
}

.xslide{
animation:slideOut .8s ease 2s forwards;
}


.menu{
visibility: hidden;
color:black;
width:10em;
background-color:white;
border-radius: 4px;
font-family: 'Raleway';
background: linear-gradient(to right,
rgba(249,107,142,1),
rgba(218,103,230,1),
rgba(130,125,253,1));
}
.slideOut{
animation: slideOut 1s forwards 1.2s;
}

@keyframes slideOut{
0%{
transform: translateX(-50%);
}

100%{
visibility: visible;
transform: translateX(0);
}
}

.slideIn{
animation: slideIn 2s ease forwards;

}

@keyframes slideIn{
0%{
visibility: visible;
transform: translateX(0);
}

100%{
transform: translateX(-150%);
}
}


The functionality of the JS is this,
when "skateboard" is clicked, it animates out, to the left and is no longer visible (the skateOff keyframes makes that happen, and i added a class that has that animation implemented also called ".skateOff")



(would it be better to not have a separate class and just add



skateboard.style.animation="animation: skateOff ease-in 1s forwards"?)



..anyway
then after "skateboard" animates out the "navigation"(which is a sidebar menu) adds the "slideOut" class which makes it slide out from the left, along with this the "X" to close the menu slide out, when that is clicked the "navigation"'s class of "slideOut" gets removed and the class of "slideIn" gets added.



This way of doing things seems inefficient, and like a lot of code, I was wondering if there's a simpler way of doing this? Toggling maybe? I've looked into toggling but i'm not sure it will work since the "navigation" element's initial state doesn't have the "slideIn" or "slideOut" class.



ANY tips will be greatly appreciated, thank you for reading and have a great day.










share|improve this question







New contributor




m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Hey! Welcome! You may find that codereview.stackexchange.com is a better place for this, as Stack Overflow is for definable answers. An answer to a question should be ‘right’ or ‘wrong’, but there’s more than one way of doing this. I would look into using a JavaScript framework if I were you, something like VueJS. Good luck.
    – Thomas Edwards
    Nov 12 at 16:28















up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is what I have so far..



icons.addEventListener('click', (e)=>{
if(e.target.className==="skate"){
navigation.classList.remove('slideIn');
navigation.classList.add('slideOut');
skateboard.classList.add('skateOff');
x.classList.add('xslide');
}else{
navigation.classList.remove('slideOut');
navigation.classList.add('slideIn');
skateboard.classList.remove('skateOff');
x.classList.remove('xslide');
}
})

@keyframes skateOff{
0%{
transform:rotate(0);
}
50%{
transform:rotate(49deg);
}
100%{
transform: translateX(-300px);
}
}

.skateOff{
animation: skateOff ease-in 1s forwards;
}

.x{
visibility: hidden;
}

.xslide{
animation:slideOut .8s ease 2s forwards;
}


.menu{
visibility: hidden;
color:black;
width:10em;
background-color:white;
border-radius: 4px;
font-family: 'Raleway';
background: linear-gradient(to right,
rgba(249,107,142,1),
rgba(218,103,230,1),
rgba(130,125,253,1));
}
.slideOut{
animation: slideOut 1s forwards 1.2s;
}

@keyframes slideOut{
0%{
transform: translateX(-50%);
}

100%{
visibility: visible;
transform: translateX(0);
}
}

.slideIn{
animation: slideIn 2s ease forwards;

}

@keyframes slideIn{
0%{
visibility: visible;
transform: translateX(0);
}

100%{
transform: translateX(-150%);
}
}


The functionality of the JS is this,
when "skateboard" is clicked, it animates out, to the left and is no longer visible (the skateOff keyframes makes that happen, and i added a class that has that animation implemented also called ".skateOff")



(would it be better to not have a separate class and just add



skateboard.style.animation="animation: skateOff ease-in 1s forwards"?)



..anyway
then after "skateboard" animates out the "navigation"(which is a sidebar menu) adds the "slideOut" class which makes it slide out from the left, along with this the "X" to close the menu slide out, when that is clicked the "navigation"'s class of "slideOut" gets removed and the class of "slideIn" gets added.



This way of doing things seems inefficient, and like a lot of code, I was wondering if there's a simpler way of doing this? Toggling maybe? I've looked into toggling but i'm not sure it will work since the "navigation" element's initial state doesn't have the "slideIn" or "slideOut" class.



ANY tips will be greatly appreciated, thank you for reading and have a great day.










share|improve this question







New contributor




m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











This is what I have so far..



icons.addEventListener('click', (e)=>{
if(e.target.className==="skate"){
navigation.classList.remove('slideIn');
navigation.classList.add('slideOut');
skateboard.classList.add('skateOff');
x.classList.add('xslide');
}else{
navigation.classList.remove('slideOut');
navigation.classList.add('slideIn');
skateboard.classList.remove('skateOff');
x.classList.remove('xslide');
}
})

@keyframes skateOff{
0%{
transform:rotate(0);
}
50%{
transform:rotate(49deg);
}
100%{
transform: translateX(-300px);
}
}

.skateOff{
animation: skateOff ease-in 1s forwards;
}

.x{
visibility: hidden;
}

.xslide{
animation:slideOut .8s ease 2s forwards;
}


.menu{
visibility: hidden;
color:black;
width:10em;
background-color:white;
border-radius: 4px;
font-family: 'Raleway';
background: linear-gradient(to right,
rgba(249,107,142,1),
rgba(218,103,230,1),
rgba(130,125,253,1));
}
.slideOut{
animation: slideOut 1s forwards 1.2s;
}

@keyframes slideOut{
0%{
transform: translateX(-50%);
}

100%{
visibility: visible;
transform: translateX(0);
}
}

.slideIn{
animation: slideIn 2s ease forwards;

}

@keyframes slideIn{
0%{
visibility: visible;
transform: translateX(0);
}

100%{
transform: translateX(-150%);
}
}


The functionality of the JS is this,
when "skateboard" is clicked, it animates out, to the left and is no longer visible (the skateOff keyframes makes that happen, and i added a class that has that animation implemented also called ".skateOff")



(would it be better to not have a separate class and just add



skateboard.style.animation="animation: skateOff ease-in 1s forwards"?)



..anyway
then after "skateboard" animates out the "navigation"(which is a sidebar menu) adds the "slideOut" class which makes it slide out from the left, along with this the "X" to close the menu slide out, when that is clicked the "navigation"'s class of "slideOut" gets removed and the class of "slideIn" gets added.



This way of doing things seems inefficient, and like a lot of code, I was wondering if there's a simpler way of doing this? Toggling maybe? I've looked into toggling but i'm not sure it will work since the "navigation" element's initial state doesn't have the "slideIn" or "slideOut" class.



ANY tips will be greatly appreciated, thank you for reading and have a great day.







javascript html css






share|improve this question







New contributor




m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 12 at 16:20









m4rsib4r

32




32




New contributor




m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






m4rsib4r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Hey! Welcome! You may find that codereview.stackexchange.com is a better place for this, as Stack Overflow is for definable answers. An answer to a question should be ‘right’ or ‘wrong’, but there’s more than one way of doing this. I would look into using a JavaScript framework if I were you, something like VueJS. Good luck.
    – Thomas Edwards
    Nov 12 at 16:28




















  • Hey! Welcome! You may find that codereview.stackexchange.com is a better place for this, as Stack Overflow is for definable answers. An answer to a question should be ‘right’ or ‘wrong’, but there’s more than one way of doing this. I would look into using a JavaScript framework if I were you, something like VueJS. Good luck.
    – Thomas Edwards
    Nov 12 at 16:28


















Hey! Welcome! You may find that codereview.stackexchange.com is a better place for this, as Stack Overflow is for definable answers. An answer to a question should be ‘right’ or ‘wrong’, but there’s more than one way of doing this. I would look into using a JavaScript framework if I were you, something like VueJS. Good luck.
– Thomas Edwards
Nov 12 at 16:28






Hey! Welcome! You may find that codereview.stackexchange.com is a better place for this, as Stack Overflow is for definable answers. An answer to a question should be ‘right’ or ‘wrong’, but there’s more than one way of doing this. I would look into using a JavaScript framework if I were you, something like VueJS. Good luck.
– Thomas Edwards
Nov 12 at 16:28














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










first of all, welcome on Stack Overflow :)



Your code may benefit from classList.toggle (https://developer.mozilla.org/pl/docs/Web/API/Element/classList).



You can have conditional statements there, meaning classList.toggle("string", boolean), like this:



icons.addEventListener('click', (e)=> {
const isSkate = e.target.className === "skate"; // this could also be altered using classList.contains()

navigation.classList.toggle('slideIn', !isSkate);
navigation.classList.toggle('slideOut', isSkate);
skateboard.classList.toggle('skateOff', isSkate);
x.classList.toggle('xslide', isSkate);
});


A little PoC can be found here: https://codepen.io/tomekbuszewski/pen/XyNzqG



If you need more help, please post your code to CodePen or JSFiddle, it would be easier to discuss then.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    m4rsib4r is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266176%2fis-there-a-better-way-to-add-remove-classes-for-menu-with-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    first of all, welcome on Stack Overflow :)



    Your code may benefit from classList.toggle (https://developer.mozilla.org/pl/docs/Web/API/Element/classList).



    You can have conditional statements there, meaning classList.toggle("string", boolean), like this:



    icons.addEventListener('click', (e)=> {
    const isSkate = e.target.className === "skate"; // this could also be altered using classList.contains()

    navigation.classList.toggle('slideIn', !isSkate);
    navigation.classList.toggle('slideOut', isSkate);
    skateboard.classList.toggle('skateOff', isSkate);
    x.classList.toggle('xslide', isSkate);
    });


    A little PoC can be found here: https://codepen.io/tomekbuszewski/pen/XyNzqG



    If you need more help, please post your code to CodePen or JSFiddle, it would be easier to discuss then.






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      first of all, welcome on Stack Overflow :)



      Your code may benefit from classList.toggle (https://developer.mozilla.org/pl/docs/Web/API/Element/classList).



      You can have conditional statements there, meaning classList.toggle("string", boolean), like this:



      icons.addEventListener('click', (e)=> {
      const isSkate = e.target.className === "skate"; // this could also be altered using classList.contains()

      navigation.classList.toggle('slideIn', !isSkate);
      navigation.classList.toggle('slideOut', isSkate);
      skateboard.classList.toggle('skateOff', isSkate);
      x.classList.toggle('xslide', isSkate);
      });


      A little PoC can be found here: https://codepen.io/tomekbuszewski/pen/XyNzqG



      If you need more help, please post your code to CodePen or JSFiddle, it would be easier to discuss then.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        first of all, welcome on Stack Overflow :)



        Your code may benefit from classList.toggle (https://developer.mozilla.org/pl/docs/Web/API/Element/classList).



        You can have conditional statements there, meaning classList.toggle("string", boolean), like this:



        icons.addEventListener('click', (e)=> {
        const isSkate = e.target.className === "skate"; // this could also be altered using classList.contains()

        navigation.classList.toggle('slideIn', !isSkate);
        navigation.classList.toggle('slideOut', isSkate);
        skateboard.classList.toggle('skateOff', isSkate);
        x.classList.toggle('xslide', isSkate);
        });


        A little PoC can be found here: https://codepen.io/tomekbuszewski/pen/XyNzqG



        If you need more help, please post your code to CodePen or JSFiddle, it would be easier to discuss then.






        share|improve this answer












        first of all, welcome on Stack Overflow :)



        Your code may benefit from classList.toggle (https://developer.mozilla.org/pl/docs/Web/API/Element/classList).



        You can have conditional statements there, meaning classList.toggle("string", boolean), like this:



        icons.addEventListener('click', (e)=> {
        const isSkate = e.target.className === "skate"; // this could also be altered using classList.contains()

        navigation.classList.toggle('slideIn', !isSkate);
        navigation.classList.toggle('slideOut', isSkate);
        skateboard.classList.toggle('skateOff', isSkate);
        x.classList.toggle('xslide', isSkate);
        });


        A little PoC can be found here: https://codepen.io/tomekbuszewski/pen/XyNzqG



        If you need more help, please post your code to CodePen or JSFiddle, it would be easier to discuss then.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 16:31









        Tomek Buszewski

        3,08374286




        3,08374286






















            m4rsib4r is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            m4rsib4r is a new contributor. Be nice, and check out our Code of Conduct.













            m4rsib4r is a new contributor. Be nice, and check out our Code of Conduct.












            m4rsib4r is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266176%2fis-there-a-better-way-to-add-remove-classes-for-menu-with-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?