简体   繁体   中英

Jquery conditional dropdown menu toggling

I'm in pain. :)

I'm creating a calorie calculator which has a dropdown for certain dietary preferences. I have it set up so that when a user clicks on the button, the dropdown appears. When a user clicks outside the button, it disappears.

I want two things that I seem to be unable to achieve.

  1. Clicking on the button again makes the class "show" disappear from "#myDropdown"

  2. When the dropdown is "show" I want to associate the 'fa-plus-down' next to the button, when it's 'closed' I want the 'fa-plus-up' icon next to it.

// 1. is the most problematic, I can't see where the logic is help. Any input is appreciated. //

<div id="contenttable">
            <div class="maindish">
                <div class="dropdown">
                    <button onclick="selectDiet()" class="dropbtn main">Diet or Allergen Filter <i class="fa fa-sort-down" style=""></i></button>
                    <div id="myDropdown" class="dropdown-content">
                        <a id="veganDiet" class="dropbtn"><img src="http://www.padthai.decorolux.com/wp-content/uploads/2017/11/vegan2-1.png"  style="width:40px; display:inline; height:20px; vertical-align:middle;"> Vegan</a>
                        <a id="vegetarianDietButton" class="dropbtn"><img src="http://www.padthai.decorolux.com/wp-content/uploads/2017/11/vegetarian-1.png" style="width:40px; height:20px; display:inline; vertical-align:middle;"> Vegetarian</a>
                        <a id="noAddesSugarButton" class="dropbtn"><img src="http://www.padthai.decorolux.com/wp-content/uploads/2017/11/sugarfree-1.png" style="width:40px; height:20px; display:inline; vertical-align:middle;"> No Added Sugar</a>
                    </div>
                </div>

And the JS

function selectDiet() {
    document.getElementById("myDropdown").classList.add("show");
}

// Close the dropdown if the user clicks outside of it

window.onclick = function(event) {
    if (!event.target.matches(".dropbtn")) {
        var dropdowns = document.getElementsByClassName("dropdown-content");

        var i;
        for (i = 0; i < dropdowns.length; i++) {
            var openDropdown = dropdowns[i];
            if (openDropdown.classList.contains("show")) {
                openDropdown.classList.remove("show");



            }
        }
    }
};

// Close the dropdown if the user clicks on it again

if ($(".dropdown").hasClass('show')) {
    $("button.dropbtn.main").click(function() {
        $("#myDropdown").removeClass("show");
      })};








    // Dropdown menu animation

    $("button.dropbtn.main").click(function() {
        $(this).children().toggleClass('fa-sort-down');
        // $(this).children().removeClass('fa-times-circle');
        $(this).children().toggleClass('fa-sort-up');







    });

Thanks in advance, here is is viewable on codepen .

Since you are using jQuery, I wrote you the functions in it.

The first function opens and closes the dropdown menu on a click on .dropbtn using slideToggle() for the dropdown and toggleClass() for the icon (note that it toggles two classes).

$('.dropbtn').on('click', function() {
  $('#myDropdown').slideToggle();
  $(this).find('i').toggleClass('fa-sort-up fa-sort-down')
});

Then you want to close the dropdown when we click outside these element. Therefor I wrote this function. It closes the dropdown by using slideUp and removes and adds the classes of the icon.

$(document).mouseup(function(e) {
  var container = $("#myDropdown, .dropbtn");
  if (!container.is(e.target) 
        && container.has(e.target).length === 0
        && $("#myDropdown").is(':visible') ) {
    $("#myDropdown").slideUp();
    $('.dropbtn i').addClass('fa-sort-up').removeClass('fa-sort-down')
  }
});

Updated CODEPEN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM