0

i have a on resize add class jQuery function which help me show an element on a certain window size:

<div class="v-menu"></div>
<div id="menu">
    <a href="#">a</a>
    <a href="#">b</a>
    <a href="#">c</a>
</div>

 

(function($) {
        var $window = $(window),
            $html = $('#menu');
        function resize() {
            if ($window.width() < 612) {
                return $html.addClass('close');
            }
            else{
            $html.removeClass('close');
            $html.removeClass('open');
            $('*[class=""]').removeAttr('class');
            }
        }
        $window
            .resize(resize)
            .trigger('resize');
    })(jQuery);

and i can toggle it with another function:

$(".v-menu").click(function(){
$("#menu").toggleClass("open");
$("#menu").toggleClass("close");
});

the problem is if i toggle the element to open and then resize the window jQuery keep adding the close class to the element:

<div id="menu" class="open close">
    <a href="#">a</a>
    <a href="#">b</a>
    <a href="#">c</a>
</div>

how can i solve that?

6
  • What is it that you want to accomplish? Right now when you resize the window and the window width is lower than 612, .close will always be added Commented Feb 27, 2017 at 12:14
  • Seems likes something you could do with an @media query in css. Show/hide an element based on the window size. developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/… Commented Feb 27, 2017 at 12:14
  • @NiekNijland i dont want that close class add when the open class added. Commented Feb 27, 2017 at 12:16
  • 1
    Try: if ($window.width() < 612 && !$html.hassClass('open')) { Commented Feb 27, 2017 at 12:18
  • If you can sketch us an use case of what you are trying to accomplish maybe we can point you in another direction, because this doesn't really feel like a good solution of your problem :/ Commented Feb 27, 2017 at 12:19

1 Answer 1

1

Based on your comment

i dont want that close class add when the open class added.

https://api.jquery.com/hasclass/

 if ($window.width() < 612){
         if(!$html.hasClass('open')) {
                  return $html.addClass('close');
         }
 }
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.