0

I have a sidemenu in HTML like this:

_Layout.cshtml

<div class="col nav-left h-100">
<p>Menu</p>
</div>

In my Site.js I have made a generic JQuery function for the toggle:

var eventHandler = {
    addToggle: function addToggle(btnElem, openEvent, closeEvent) {
        const isClosedClass = 'this-is-closed';
        btnElem.click(function () {
            if ($(this).hasClass(isClosedClass)) {
                openEvent();
            } else {
                closeEvent();
            }
        });
    }
};

In my _Layout.cshtml I call this function like this:

 <script>
        eventHandler.addToggle($(".btn-toggle-something"),
            function () {
                // Slide down
            },
            function () {
                // Slide up
            });

 </script>

Now I getting stuck how to write the logic for the actual toggle on a div.

How can I toggle my side menu with a generic function?

Sources I looked:

https://api.jquery.com/toggle/

How to toggle (hide / show) sidebar div using jQuery

5
  • Have you tried .slideUp() and .slideDown()? Commented Jul 18, 2019 at 9:53
  • Do you have an example to achieve this? Commented Jul 18, 2019 at 9:59
  • How's this? jsfiddle.net/0ubhkfLq/6 Commented Jul 18, 2019 at 10:11
  • Yes that is what I am looking! Can you post your code in the answers so I can accept it? Commented Jul 18, 2019 at 10:14
  • Will do, one sec. Commented Jul 18, 2019 at 10:15

1 Answer 1

1

.slideUp() and .slideDown() in combination with .toggleClass() will work - please see below:

var eventHandler = {
    addToggle: function addToggle(btnElem, openEvent, closeEvent) {
        const isClosedClass = 'this-is-closed';
        btnElem.click(function () {
            if ($(this).hasClass(isClosedClass)) {
                openEvent();
            } else {
                closeEvent();
            }

            // Toggle `isClosedClass` for next time this is clicked.
            btnElem.toggleClass(isClosedClass);
        });
    }
};

// Bind slideDown/slideUp.
var leftNav = $('.nav-left');

eventHandler.addToggle($('.btn-toggle-something'),
    function () {
        leftNav.slideDown();
    },
    function () {
        leftNav.slideUp();
    }
);
body {
  margin: 0;
  padding: 0;
}

p {
  margin: 0;
}

.nav-left {
  background-color: lightskyblue;
  width: 200px;
  height: 400px;
}

.btn-toggle-something {
  cursor: pointer;
  position: fixed;
  top: 0;
  left: 250px;
  height: 50px;
  width: 50px;
  background-color: salmon;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col nav-left h-100">
  <p>Menu</p>
</div>

<div class="btn-toggle-something">Toggle</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.