Search code examples
javascriptcsssuperfish

Superfish broken when <a> tag is added


I'm using the code for the latest Superfish menu and I can get it working only when I use lists without the <a> tag. I'm probably missing something obvious but I've been staring at this so long I have no idea what's wrong.

Here is my html:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="http://vbpmonitor.com/templates/vbpmonitor/js/superfish.js"></script>
<script src="http://vbpmonitor.com/templates/vbpmonitor/js/hoverIntent.js">    </script>
<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('ul.sf-menu').superfish({
        delay:         800,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing
        animation:     {opacity:'show'},   // an object equivalent to first parameter of jQuery’s .animate() method. Used to animate the submenu open
        animationOut:  {opacity:'hide'},   // an object equivalent to first parameter of jQuery’s .animate() method Used to animate the submenu closed
        speed:         'normal',           // speed of the opening animation. Equivalent to second parameter of jQuery’s .animate() method
        speedOut:      'fast', 
        disableHI:     true,       
    });
});
</script>
</head>
<body>

<nav class="navbar">
<ul class="sf-menu">
<li><a href="#">Link 1</a>
  <ul>
    <li><a href="#">Sublink 1</a><li>
    <li><a href="#">Sublink 2</a></li>
    <li><a href="#">Sublink 3</a></li>
  </ul>
</li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
</nav>

</body>
</html>

CSS:

.navbar {
  float: left;
  width: 100%;
  height: 50px;
  background: #cc3333;
}
nav > ul {
  margin: 0;
  padding: 0;
  list-style-type: none;
}
nav > ul > li {
  float: left;
  margin: 0;
}
nav > ul > li > a {
  display: block;
  padding: 17px 20px;
  color: #fff;
  text-decoration: none;
}
nav ul ul li a {
  width: 200px;
  display: block;
  padding: 10px 15px;
  text-decoration: none;
  color: #fff;
}

/*** ESSENTIAL STYLES ***/
.sf-menu li {
  position: relative;
}
.sf-menu ul {
  position: absolute;
  display: none;
  top: 100%;
  left: 0;
  z-index: 99;
  list-style-type:none;
}
.sf-menu > li {
  float: left;
}
.sf-menu li:hover ul,
.sf-menu li.sfHover ul {
  display: block;
}

.sf-menu a {
  display: block;
  position: relative;
}
.sf-menu ul ul {
  top: 0;
  left: 100%;
}

And here's the link to the JSbin where I tested this: http://jsbin.com/mituwuxupi/1/


Solution

  • I tried your jsbin and it works OK, but submenus can't be seen, just try to add some background to your CSS, for example:

    nav ul ul li a {
      width: 200px;
      display: block;
      padding: 10px 15px;
      text-decoration: none;
      color: #FFF;
      background-color: red;
    }
    

    And you will see your menu working.