JQuery Navigation Menu Problems












0















I have 2 problems:




  1. I'm trying to create an click-off event, where a click off of the "nav" menu is detected, which triggers the deselection of the open list element and retracts its respective dropdown content.


  2. My current code doesn't allow another list element/navigation item without dropdown content to be added, as doing so hinders the entirety of the code from working. I'd like to add another "nav" list element that doesn't have dropdown contents, without hindering the functionality of the other list elements and their respective dropdown content.



JSFiddle



HTML



<div id="nav">
<ul class="nav-ul">
<li class="" rel="pay1">Color</li>
<li rel="pay2">Shape</li>
<li rel="pay3">Size</li>
</ul>
<div class="substitute">
<div id="pay1" class="sub-child">
<ul>
<li>Red</li>
<li>Blue</li>
<li>Green</li>
</ul>
</div>
<div id="pay2" class="sub-child">
<ul>
<li>Square</li>
<li>Circle</li>
<li>Triangle</li>
<li>Diamond</li>
</ul>
</div>
<div id="pay3" class="sub-child">
<ul>
<li>Small</li>
<li>Medium</li>
<li>Large</li>
</ul>
</div>
</div>
</div>


CSS



* {
margin: 0;
padding: 0;
}
#nav {
background-color: /*blue*/;
float: right;
}
#nav .nav-ul {
list-style: none;
float: right;
background-color: /*yellow*/;
border-left: solid 2px #000000;
border-right: solid 2px #000000;
}
#nav .nav-ul li {
float: left;
padding: 4px;
font-weight: bold;
color: #000000;
}
#nav .nav-ul li:hover {
cursor: pointer;
text-decoration: underline;
color: #E51D27;
}
#nav .nav-ul li.detected {
color: #E51D27;
}

#nav .substitute {
float: right;
background-color: /*pink*/;
margin-right: 4px;
}
#nav .substitute .sub-child {
float: left;
display: none;
}
#nav .substitute .sub-child.active {
display: block;
}


#nav .substitute .sub-child ul {
list-style: none;
}
#nav .substitute .sub-child ul li {
float: left;
padding: 4px;
}


JQuery



$(function() {

function animate() {
$('#nav .nav-ul li').off('click', animate)
var $detected = $(this).closest('.nav-ul');
$detected.find('li.detected').removeClass('detected');
$(this).addClass('detected');

//figure out which rel to show
var ulToShow = $(this).attr('rel');

//hide current rel
if ($('.substitute .sub-child.active').length > 0) {
$('.substitute .sub-child.active').hide(700, function() {
$(this).removeClass('active');
$('#'+ulToShow).fadeIn(528, function() {
$(this).addClass('active');
$('#nav .nav-ul li').on('click', animate)

});
});
} else {
$('#'+ulToShow).fadeIn(528, function() {
$(this).addClass('active');
$('#nav .nav-ul li').on('click', animate)
});
}
}

$('#nav .nav-ul li').on('click', animate);
});









share|improve this question



























    0















    I have 2 problems:




    1. I'm trying to create an click-off event, where a click off of the "nav" menu is detected, which triggers the deselection of the open list element and retracts its respective dropdown content.


    2. My current code doesn't allow another list element/navigation item without dropdown content to be added, as doing so hinders the entirety of the code from working. I'd like to add another "nav" list element that doesn't have dropdown contents, without hindering the functionality of the other list elements and their respective dropdown content.



    JSFiddle



    HTML



    <div id="nav">
    <ul class="nav-ul">
    <li class="" rel="pay1">Color</li>
    <li rel="pay2">Shape</li>
    <li rel="pay3">Size</li>
    </ul>
    <div class="substitute">
    <div id="pay1" class="sub-child">
    <ul>
    <li>Red</li>
    <li>Blue</li>
    <li>Green</li>
    </ul>
    </div>
    <div id="pay2" class="sub-child">
    <ul>
    <li>Square</li>
    <li>Circle</li>
    <li>Triangle</li>
    <li>Diamond</li>
    </ul>
    </div>
    <div id="pay3" class="sub-child">
    <ul>
    <li>Small</li>
    <li>Medium</li>
    <li>Large</li>
    </ul>
    </div>
    </div>
    </div>


    CSS



    * {
    margin: 0;
    padding: 0;
    }
    #nav {
    background-color: /*blue*/;
    float: right;
    }
    #nav .nav-ul {
    list-style: none;
    float: right;
    background-color: /*yellow*/;
    border-left: solid 2px #000000;
    border-right: solid 2px #000000;
    }
    #nav .nav-ul li {
    float: left;
    padding: 4px;
    font-weight: bold;
    color: #000000;
    }
    #nav .nav-ul li:hover {
    cursor: pointer;
    text-decoration: underline;
    color: #E51D27;
    }
    #nav .nav-ul li.detected {
    color: #E51D27;
    }

    #nav .substitute {
    float: right;
    background-color: /*pink*/;
    margin-right: 4px;
    }
    #nav .substitute .sub-child {
    float: left;
    display: none;
    }
    #nav .substitute .sub-child.active {
    display: block;
    }


    #nav .substitute .sub-child ul {
    list-style: none;
    }
    #nav .substitute .sub-child ul li {
    float: left;
    padding: 4px;
    }


    JQuery



    $(function() {

    function animate() {
    $('#nav .nav-ul li').off('click', animate)
    var $detected = $(this).closest('.nav-ul');
    $detected.find('li.detected').removeClass('detected');
    $(this).addClass('detected');

    //figure out which rel to show
    var ulToShow = $(this).attr('rel');

    //hide current rel
    if ($('.substitute .sub-child.active').length > 0) {
    $('.substitute .sub-child.active').hide(700, function() {
    $(this).removeClass('active');
    $('#'+ulToShow).fadeIn(528, function() {
    $(this).addClass('active');
    $('#nav .nav-ul li').on('click', animate)

    });
    });
    } else {
    $('#'+ulToShow).fadeIn(528, function() {
    $(this).addClass('active');
    $('#nav .nav-ul li').on('click', animate)
    });
    }
    }

    $('#nav .nav-ul li').on('click', animate);
    });









    share|improve this question

























      0












      0








      0








      I have 2 problems:




      1. I'm trying to create an click-off event, where a click off of the "nav" menu is detected, which triggers the deselection of the open list element and retracts its respective dropdown content.


      2. My current code doesn't allow another list element/navigation item without dropdown content to be added, as doing so hinders the entirety of the code from working. I'd like to add another "nav" list element that doesn't have dropdown contents, without hindering the functionality of the other list elements and their respective dropdown content.



      JSFiddle



      HTML



      <div id="nav">
      <ul class="nav-ul">
      <li class="" rel="pay1">Color</li>
      <li rel="pay2">Shape</li>
      <li rel="pay3">Size</li>
      </ul>
      <div class="substitute">
      <div id="pay1" class="sub-child">
      <ul>
      <li>Red</li>
      <li>Blue</li>
      <li>Green</li>
      </ul>
      </div>
      <div id="pay2" class="sub-child">
      <ul>
      <li>Square</li>
      <li>Circle</li>
      <li>Triangle</li>
      <li>Diamond</li>
      </ul>
      </div>
      <div id="pay3" class="sub-child">
      <ul>
      <li>Small</li>
      <li>Medium</li>
      <li>Large</li>
      </ul>
      </div>
      </div>
      </div>


      CSS



      * {
      margin: 0;
      padding: 0;
      }
      #nav {
      background-color: /*blue*/;
      float: right;
      }
      #nav .nav-ul {
      list-style: none;
      float: right;
      background-color: /*yellow*/;
      border-left: solid 2px #000000;
      border-right: solid 2px #000000;
      }
      #nav .nav-ul li {
      float: left;
      padding: 4px;
      font-weight: bold;
      color: #000000;
      }
      #nav .nav-ul li:hover {
      cursor: pointer;
      text-decoration: underline;
      color: #E51D27;
      }
      #nav .nav-ul li.detected {
      color: #E51D27;
      }

      #nav .substitute {
      float: right;
      background-color: /*pink*/;
      margin-right: 4px;
      }
      #nav .substitute .sub-child {
      float: left;
      display: none;
      }
      #nav .substitute .sub-child.active {
      display: block;
      }


      #nav .substitute .sub-child ul {
      list-style: none;
      }
      #nav .substitute .sub-child ul li {
      float: left;
      padding: 4px;
      }


      JQuery



      $(function() {

      function animate() {
      $('#nav .nav-ul li').off('click', animate)
      var $detected = $(this).closest('.nav-ul');
      $detected.find('li.detected').removeClass('detected');
      $(this).addClass('detected');

      //figure out which rel to show
      var ulToShow = $(this).attr('rel');

      //hide current rel
      if ($('.substitute .sub-child.active').length > 0) {
      $('.substitute .sub-child.active').hide(700, function() {
      $(this).removeClass('active');
      $('#'+ulToShow).fadeIn(528, function() {
      $(this).addClass('active');
      $('#nav .nav-ul li').on('click', animate)

      });
      });
      } else {
      $('#'+ulToShow).fadeIn(528, function() {
      $(this).addClass('active');
      $('#nav .nav-ul li').on('click', animate)
      });
      }
      }

      $('#nav .nav-ul li').on('click', animate);
      });









      share|improve this question














      I have 2 problems:




      1. I'm trying to create an click-off event, where a click off of the "nav" menu is detected, which triggers the deselection of the open list element and retracts its respective dropdown content.


      2. My current code doesn't allow another list element/navigation item without dropdown content to be added, as doing so hinders the entirety of the code from working. I'd like to add another "nav" list element that doesn't have dropdown contents, without hindering the functionality of the other list elements and their respective dropdown content.



      JSFiddle



      HTML



      <div id="nav">
      <ul class="nav-ul">
      <li class="" rel="pay1">Color</li>
      <li rel="pay2">Shape</li>
      <li rel="pay3">Size</li>
      </ul>
      <div class="substitute">
      <div id="pay1" class="sub-child">
      <ul>
      <li>Red</li>
      <li>Blue</li>
      <li>Green</li>
      </ul>
      </div>
      <div id="pay2" class="sub-child">
      <ul>
      <li>Square</li>
      <li>Circle</li>
      <li>Triangle</li>
      <li>Diamond</li>
      </ul>
      </div>
      <div id="pay3" class="sub-child">
      <ul>
      <li>Small</li>
      <li>Medium</li>
      <li>Large</li>
      </ul>
      </div>
      </div>
      </div>


      CSS



      * {
      margin: 0;
      padding: 0;
      }
      #nav {
      background-color: /*blue*/;
      float: right;
      }
      #nav .nav-ul {
      list-style: none;
      float: right;
      background-color: /*yellow*/;
      border-left: solid 2px #000000;
      border-right: solid 2px #000000;
      }
      #nav .nav-ul li {
      float: left;
      padding: 4px;
      font-weight: bold;
      color: #000000;
      }
      #nav .nav-ul li:hover {
      cursor: pointer;
      text-decoration: underline;
      color: #E51D27;
      }
      #nav .nav-ul li.detected {
      color: #E51D27;
      }

      #nav .substitute {
      float: right;
      background-color: /*pink*/;
      margin-right: 4px;
      }
      #nav .substitute .sub-child {
      float: left;
      display: none;
      }
      #nav .substitute .sub-child.active {
      display: block;
      }


      #nav .substitute .sub-child ul {
      list-style: none;
      }
      #nav .substitute .sub-child ul li {
      float: left;
      padding: 4px;
      }


      JQuery



      $(function() {

      function animate() {
      $('#nav .nav-ul li').off('click', animate)
      var $detected = $(this).closest('.nav-ul');
      $detected.find('li.detected').removeClass('detected');
      $(this).addClass('detected');

      //figure out which rel to show
      var ulToShow = $(this).attr('rel');

      //hide current rel
      if ($('.substitute .sub-child.active').length > 0) {
      $('.substitute .sub-child.active').hide(700, function() {
      $(this).removeClass('active');
      $('#'+ulToShow).fadeIn(528, function() {
      $(this).addClass('active');
      $('#nav .nav-ul li').on('click', animate)

      });
      });
      } else {
      $('#'+ulToShow).fadeIn(528, function() {
      $(this).addClass('active');
      $('#nav .nav-ul li').on('click', animate)
      });
      }
      }

      $('#nav .nav-ul li').on('click', animate);
      });






      javascript jquery html css






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 3:32









      Daniel JohnsonDaniel Johnson

      455




      455
























          0






          active

          oldest

          votes











          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',
          autoActivateHeartbeat: false,
          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385818%2fjquery-navigation-menu-problems%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385818%2fjquery-navigation-menu-problems%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini