Footer's nav submenu won't stay open after I mouse out unless I mouse over quickly and hover far left/right of my ul open submenu

爷,独闯天下 提交于 2019-12-13 03:23:31

问题


I am building a website that will be responsive and I need a fixed footer nav centered in the middle of the screen that when hovered over activates a drop-up menu. I am using only percentages for everything (important for lining things up width-wise with my header in the long run) which is making everything a little confusing for me.

Whenever I hover over INFO (see my JSFiddle) and attempt to move my mouse upwards toward the submenu, the submenu drops as soon as I leave INFO. When I remove the css attributes: position, width, bottom, and margin-bottom from my #footer-nav ul li:hover > ul (essentially turning the drop-up menu into a drop-down menu), the submenu stays open even when I move the mouse onto the submenu. I can even change the margin-bottom to 20% and the submenu will still stay open going over that empty space. What is making it close when I make it a drop-up rather than a drop-down?

I also have an issue with where the hovering is being triggered. I can move the mouse far to the left/right of INFO and my submenu still pops up. How can I fix this using my current div, set up with the percentages and centering?

Here is the relevant code:

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" 
                target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" 
                target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li> 
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>
#background {
    background-color: #FFFFFF;
    margin: 0px;
    padding: 0px;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: -1;
}

#footer {
    position: fixed;
    bottom: 0;
    width: 25%;
    text-align: center;
    margin-bottom: 1%;
    margin-left: -15%;
    left: 50%;
    margin-left: -12.5%;
}

a {
    text-decoration: none;
    color: inherit;
}

#footer-nav ul li:hover > ul {
    display: block;
    position: absolute;
    width: 100%;
    bottom: 100%;
    margin-bottom: 2%;
}

#footer-nav {
    width: 100%;
}

#footer-nav ul {
    font-family: Arial;
    font-size: 100%;
    font-weight: bold;
    color: #000000;
    list-style-type: none;
    text-align: center;
    margin: auto;
    padding-top: 0px;
    padding-right: 0px;
    padding-bottom: 0px;
    padding-left: 0px;
}

#footer-nav ul ul {
    font-family: Arial;
    font-size: 100%;
    color: #000000;
    list-style-type: none;
    font-weight: normal;
    display: none;
}

#email {
    padding-top: 2%;
    padding-bottom: 2%;
    border: 1px solid black;
    color: #000000;
}

#instagram {
    padding-top: 2%;
    padding-bottom: 2%;
    border: 1px solid black;
    color: #000000;
}

#twitter {
    padding-top: 2%;
    padding-bottom: 2%;
    border: 1px solid black;
    color: #000000;
}

回答1:


The problem comes from margin-bottom: 2% on #footer-nav ul li:hover > ul. As you move off of your Info button into this margin, you're no longer hovering over the element in question. To correct this, simply replace it with padding-bottom: 2%:

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("background-color", "black");
  }, function() {
    $("#email").css("background-color", "white");
  });
});

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("color", "white");
  }, function() {
    $("#email").css("color", "black");
  });
});

$(document).ready(function() {    
  $("#twitter").hover(function() {        
    $("#twitter").css("background-color", "black");
  }, function() {
    $("#twitter").css("background-color", "white");
  });
});

$(document).ready(function() {    
  $("#twitter").hover(function() {        
    $("#twitter").css("color", "white");
  }, function() {
    $("#twitter").css("color", "black");
  });
});

$(document).ready(function() {    
  $("#instagram").hover(function() {        
    $("#instagram").css("background-color", "black");
  }, function() {
    $("#instagram").css("background-color", "white");
  });
});

$(document).ready(function() {    
  $("#instagram").hover(function() {        
    $("#instagram").css("color", "white");
  }, function() {
    $("#instagram").css("color", "black");
  });
});
#background {
  background-color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
}

#footer {
  position: fixed;
  bottom: 0;
  width: 25%;
  text-align: center;
  margin-bottom: 1%;
  margin-left: -15%;
  left: 50%;
  margin-left: -12.5%;
}

a {
  text-decoration: none;
  color: inherit;
}

#footer-nav ul li:hover>ul {
  display: block;
  position: absolute;
  width: 100%;
  bottom: 100%;
  padding-bottom: 2%;
}

#footer-nav {
  width: 100%;
}

#footer-nav ul {
  font-family: Arial;
  font-size: 100%;
  font-weight: bold;
  color: #000000;
  list-style-type: none;
  text-align: center;
  margin: auto;
  padding-top: 0px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 0px;
}

#footer-nav ul ul {
  font-family: Arial;
  font-size: 100%;
  color: #000000;
  list-style-type: none;
  font-weight: normal;
  display: none;
}

#email {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#instagram {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#twitter {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>

Also note that you can combine all of your functions into one $(document).ready, along with combining the .css() rules as well. This will significantly cut down on lines, improving readability. I've done this in the following example:

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("background-color", "black");
    $("#email").css("color", "white");
  }, function() {
    $("#email").css("background-color", "white");
    $("#email").css("color", "black");
  });

  $("#twitter").hover(function() {        
    $("#twitter").css("background-color", "black");
    $("#twitter").css("color", "white");
  }, function() {
    $("#twitter").css("background-color", "white");
    $("#twitter").css("color", "black");
  });

  $("#instagram").hover(function() {        
    $("#instagram").css("background-color", "black");
    $("#instagram").css("color", "white");
  }, function() {
    $("#instagram").css("background-color", "white");
    $("#instagram").css("color", "black");
  }); 
});
#background {
  background-color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
}

#footer {
  position: fixed;
  bottom: 0;
  width: 25%;
  text-align: center;
  margin-bottom: 1%;
  margin-left: -15%;
  left: 50%;
  margin-left: -12.5%;
}

a {
  text-decoration: none;
  color: inherit;
}

#footer-nav ul li:hover>ul {
  display: block;
  position: absolute;
  width: 100%;
  bottom: 100%;
  padding-bottom: 2%;
}

#footer-nav {
  width: 100%;
}

#footer-nav ul {
  font-family: Arial;
  font-size: 100%;
  font-weight: bold;
  color: #000000;
  list-style-type: none;
  text-align: center;
  margin: auto;
  padding-top: 0px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 0px;
}

#footer-nav ul ul {
  font-family: Arial;
  font-size: 100%;
  color: #000000;
  list-style-type: none;
  font-weight: normal;
  display: none;
}

#email {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#instagram {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#twitter {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>

By default, the #info element will expand to fit the size of its container, as it is a block-level element. In order to prevent it from being able to be hovered over past the edge of the text, you'll want to set #info to display: inline-block.

This shrinks the width of the element, and causes the popup to come in further to the right. To counteract this, you'll want to set margin-left on #info > ul of calc(-50% + 18.67px). The -50% is the inherit left-alignment, and the 18.67px is the default width of the text INFO. If you set a width on #info, the value in calc() should be updated to match.

This can be seen in the following:

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("background-color", "black");
    $("#email").css("color", "white");
  }, function() {
    $("#email").css("background-color", "white");
    $("#email").css("color", "black");
  });

  $("#twitter").hover(function() {        
    $("#twitter").css("background-color", "black");
    $("#twitter").css("color", "white");
  }, function() {
    $("#twitter").css("background-color", "white");
    $("#twitter").css("color", "black");
  });

  $("#instagram").hover(function() {        
    $("#instagram").css("background-color", "black");
    $("#instagram").css("color", "white");
  }, function() {
    $("#instagram").css("background-color", "white");
    $("#instagram").css("color", "black");
  }); 
});
#background {
  background-color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
}

#footer {
  position: fixed;
  bottom: 0;
  width: 25%;
  text-align: center;
  margin-bottom: 1%;
  margin-left: -15%;
  left: 50%;
  margin-left: -12.5%;
}

a {
  text-decoration: none;
  color: inherit;
}

#footer-nav ul li:hover>ul {
  display: block;
  position: absolute;
  width: 100%;
  bottom: 100%;
  padding-bottom: 2%;
}

#footer-nav {
  width: 100%;
}

#footer-nav ul {
  font-family: Arial;
  font-size: 100%;
  font-weight: bold;
  color: #000000;
  list-style-type: none;
  text-align: center;
  margin: auto;
  padding-top: 0px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 0px;
}

#footer-nav ul ul {
  font-family: Arial;
  font-size: 100%;
  color: #000000;
  list-style-type: none;
  font-weight: normal;
  display: none;
}

#email {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#instagram {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#twitter {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#info {
  display: inline-block;
}

#info > ul {
  margin-left: calc(-50% + 18.67px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>


来源:https://stackoverflow.com/questions/51758002/footers-nav-submenu-wont-stay-open-after-i-mouse-out-unless-i-mouse-over-quick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!