HappyLittle MaskBoy
HappyLittle MaskBoy

Reputation: 11

Dropdown content automatically show and when i hover it hide by itself using jquery

At the first i am really sorry , because i am a new user.

Try to google but i can't find the same problem to me.

can anyone tell me why the navbar dropdown button suddenly .hide and when i unhover it , it become .show .

I know people would not understand. This is my problem.

$(document).ready(function(){
	$('.menubar ul li').hover(function(){
	$(this).find('.dropdown-content').slideToggle(400);
	});
});
ol, ul {
    margin-top: 0;
    margin-bottom: 10px;
    display: inline-block;
    list-style-type: none;
}

div {
	
	display:block;
}

center {
	display: block;
    text-align: -webkit-center;
}

.clearfix:after {
    display:block;
    clear:both;
	

}

.menubar {
    width:1100px;
	height:50px;
	text-align:center;
	background-color:#454545;
	margin-left:auto;
	margin-right:auto;
    list-style-type: none;
}

.menu {
    width:100%;
	text-decoration:none;
	
}

 
.menu li {

	float:center;
	display:inline;
    list-style:none;
    font-family:"Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
    font-weight:normal;
    font-style:normal;
}
 
.menu li.shoppingcart	{
	background:url("images/shoppingcart.png");
	width:61px;
}
 
.menu a {
    transition:all linear 0.2s;
    color:white;
	font-size:15px;
	font-weight:normal;
	font-style:normal;
}
 
.menu li:hover > a, .menu .current-item > a {
    color:#aa79da;
	height:100%;
	cursor:pointer;	
	text-shadow: 0 0 10px purple;
	background-color:#4c4b4b;	
}

.menu > ul > li {
    margin-left:auto;
	margin-right:auto;
    text-align: center;
    display: inline-block;
    position: relative;
    text-decoration: none;
	
}

.menu > ul > li > a {
    padding: 30px 30px;
    padding-top: 18px;
    padding-bottom: 10px;
    display: inline-block;
    text-shadow: 0px 1px 0px rgba(0,0,0,0.4);
    text-decoration: none;
	margin-top: 0px;


}



--------------------------------------------------------------



ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #333;
	display: inline-block;
    list-style-type: none;	
	
}

li {
    float: left;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
    /*background-color: red;*/
}


.activeNav {
	background-color:#5e4279;
	
}

.activeNav a{
	color:white;
	font-weight:bold;
	
}

.activeNav li:hover > a, .activeNav .current-item > a  {
	/*background-color: #707070;*/
	text-decoration:none;
	color:white;
	font-weight:bold;
}

li.dropdown {
    display: inline-block;
}

.dropdown-content {
	/*margin-top:6px;*/
	/*width: 100%;*/
    display: none;
    position: absolute;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
	width:auto;
	background-color:#4c4b4b;
	z-index:100;
	

	
}



.dropdown1 {
	margin-top:0px;
	width:200px;
	background-color:#4c4b4b;
	float:left;

}
.dropdown2 {
	width:200px;
	background-color:#4c4b4b;
	margin-left:200px;
	float:center;
}
.dropdown3 {
	width:200px;
	background-color:#4c4b4b;
	float:right;	
	margin-left:400px;
	margin-top:-269px;
}

.dropdown-content a {
    color: white;	
    padding: 12px 16px;
    text-decoration: none;
    display: block;
	font-size:12px;
    text-align: center;
	text-decoration:none;
	
}

.dropdown-content a:hover {
	/*background-color: #707070;*/
	text-decoration:none;
	text-shadow: 0 0 10px purple;
	color:#aa79da;
	font-weight:bold;

}

.dropdown:hover .dropdown-content {
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menubar">
		<nav class="menu">
			<ul class="clearfix">
				<li class="activeNav"><a class="home" href="home.php">Home</a></li>
				<li class="dropdown"><a class="dropbtn" title="Discover KTD"><span class="menu-title">Discover KTD</span></a>
					<div class="dropdown-content">
							<div class="dropdown1">
								<h4 style="margin-top:0px; padding-top:35px; color:white; text-shadow: 0px 1px 0px rgba(0,0,0,0.4); font-family:Helvetica Neue , Helvetica, Helvetica, Arial, sans-serif; text-decoration: underline;">WHY KTD?</h4>
								<a href="#">Message from CEO</a>
								<a href="#">Overview of KTD</a>
								<a href="#">Vision & Mission</a>
								<a href="#">Philosophy & Core Values</a>
								<a href="#">Testimonials</a>
								<a href="#">KTD Partners</a>
							</div>
							<div class="dropdown2">
								<h4 style="margin-top:0px; padding-top:35px; color:white; text-shadow: 0px 1px 0px rgba(0,0,0,0.4); font-family:Helvetica Neue , Helvetica, Helvetica, Arial, sans-serif; text-decoration: underline;">KTD Leadership</h4>
								<a href="#">Board of Directors</a>
								<a href="#">Key Management Team</a>
							</div>
					</div>
				</li>
			</ul>
		</nav>
	</div>

Upvotes: 1

Views: 713

Answers (2)

masif
masif

Reputation: 4140

Hope this helps.

$(document).ready(function() {
  $('.menubar ul li').mouseleave(function() {
    $(this).find('.dropdown-content').slideUp(4000);
  });
});
ol,
ul {
  margin-top: 0;
  margin-bottom: 10px;
  display: inline-block;
  list-style-type: none;
}
div {
  display: block;
}
center {
  display: block;
  text-align: -webkit-center;
}
.clearfix:after {
  display: block;
  clear: both;
}
.menubar {
  width: 1100px;
  height: 50px;
  text-align: center;
  background-color: #454545;
  margin-left: auto;
  margin-right: auto;
  list-style-type: none;
}
.menu {
  width: 100%;
  text-decoration: none;
}
.menu li {
  float: center;
  display: inline;
  list-style: none;
  font-family: "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
  font-weight: normal;
  font-style: normal;
}
.menu li.shoppingcart {
  background: url("images/shoppingcart.png");
  width: 61px;
}
.menu a {
  transition: all linear 0.2s;
  color: white;
  font-size: 15px;
  font-weight: normal;
  font-style: normal;
}
.menu li:hover > a,
.menu .current-item > a {
  color: #aa79da;
  height: 100%;
  cursor: pointer;
  text-shadow: 0 0 10px purple;
  background-color: #4c4b4b;
}
.menu > ul > li {
  margin-left: auto;
  margin-right: auto;
  text-align: center;
  display: inline-block;
  position: relative;
  text-decoration: none;
}
.menu > ul > li > a {
  padding: 30px 30px;
  padding-top: 18px;
  padding-bottom: 10px;
  display: inline-block;
  text-shadow: 0px 1px 0px rgba(0, 0, 0, 0.4);
  text-decoration: none;
  margin-top: 0px;
}
-------------------------------------------------------------- ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  display: inline-block;
  list-style-type: none;
}
li {
  float: left;
}
li a,
.dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
  /*background-color: red;*/
}
.activeNav {
  background-color: #5e4279;
}
.activeNav a {
  color: white;
  font-weight: bold;
}
.activeNav li:hover > a,
.activeNav .current-item > a {
  /*background-color: #707070;*/
  text-decoration: none;
  color: white;
  font-weight: bold;
}
li.dropdown {
  display: inline-block;
}
.dropdown-content {
  /*margin-top:6px;*/
  /*width: 100%;*/
  display: none;
  position: absolute;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  width: auto;
  background-color: #4c4b4b;
  z-index: 100;
}
.dropdown1 {
  margin-top: 0px;
  width: 200px;
  background-color: #4c4b4b;
  float: left;
}
.dropdown2 {
  width: 200px;
  background-color: #4c4b4b;
  margin-left: 200px;
  float: center;
}
.dropdown3 {
  width: 200px;
  background-color: #4c4b4b;
  float: right;
  margin-left: 400px;
  margin-top: -269px;
}
.dropdown-content a {
  color: white;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  font-size: 12px;
  text-align: center;
  text-decoration: none;
}
.dropdown-content a:hover {
  /*background-color: #707070;*/
  text-decoration: none;
  text-shadow: 0 0 10px purple;
  color: #aa79da;
  font-weight: bold;
}
.dropdown:hover .dropdown-content {
  display: block!important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menubar">
  <nav class="menu">
    <ul class="clearfix">
      <li class="activeNav"><a class="home" href="home.php">Home</a>
      </li>
      <li class="dropdown"><a class="dropbtn" title="Discover KTD"><span class="menu-title">Discover KTD</span></a>
        <div class="dropdown-content">
          <div class="dropdown1">
            <h4 style="margin-top:0px; padding-top:35px; color:white; text-shadow: 0px 1px 0px rgba(0,0,0,0.4); font-family:Helvetica Neue , Helvetica, Helvetica, Arial, sans-serif; text-decoration: underline;">WHY KTD?</h4>
            <a href="#">Message from CEO</a>
            <a href="#">Overview of KTD</a>
            <a href="#">Vision & Mission</a>
            <a href="#">Philosophy & Core Values</a>
            <a href="#">Testimonials</a>
            <a href="#">KTD Partners</a>
          </div>
          <div class="dropdown2">
            <h4 style="margin-top:0px; padding-top:35px; color:white; text-shadow: 0px 1px 0px rgba(0,0,0,0.4); font-family:Helvetica Neue , Helvetica, Helvetica, Arial, sans-serif; text-decoration: underline;">KTD Leadership</h4>
            <a href="#">Board of Directors</a>
            <a href="#">Key Management Team</a>
          </div>
        </div>
      </li>
    </ul>
  </nav>
</div>

Upvotes: 1

Aditya
Aditya

Reputation: 1261

I have not tested this code, however if you can try using mouseover and mouseout to check if this problem does not occur?

$(document).ready(function(){
  $('.menubar ul li.dropdown').mouseover(function(){
     $(this).find('.dropdown-content').slideDown(400);
  }).mouseout(function(){
     $(this).find('.dropdown-content').slideUp(400);
  });
});

Upvotes: 0

Related Questions