javascript - Jquery hover function show and hide element -
i have hover function shows , hides sub menus until person hovers on them.
problem when try move mouse down menu shows disappears again.
can point out im doing wrong here.?
$(document).ready(function() { $('.menu-link > a').hover(function() { $(this).next('.menu').show(); }, function() { $(this).next('.menu').hide(); }); });
nav { position: fixed; top: 0; left: 0; width: 100%; /*background-color: #333333;*/ background-color: transparent; transition: 0.2s; } nav ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: transparent; display: inline-block; } nav ul li { float: left; } nav ul li a.link { display: block; color: black; text-align: center; padding: 24px 16px; text-decoration: none; transition: 0.2s; font-weight: bold; text-transform: uppercase; position: relative; } nav ul li div.arrow-down { float: right; width: 0; height: 0; margin-left: 5px; border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 5px solid #fff; margin-top: 10px; transition: 0.2s; } nav ul li div.menu { display: none; z-index: 999; position: relative; background-color: #fff; box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2); padding: 15px 0; margin-top: -15px; } nav ul li div.menu { color: #333333; padding-left: 15px; } nav ul li a.link:hover { color: #808080; text-decoration: none; transition: 0.2s; } nav ul li a.link:hover div.arrow-down { border-top: 5px solid #808080; transition: 0.2s; } nav ul li:first-child { padding-left: 100px; } nav ul.right { float: right; } nav ul.right li:last-child { padding-right: 100px; } nav.color { background-color: #333333; transition: 0.2s; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <ul class="left"> <li class="menu-link"> <a href="" class="link">home</a> <div class="menu"> <a href="">subcat.title</a> </div> </li> <li class="menu-link"> <a href="" class="link">test</a> <div class="menu"> <a href="">subcat.title</a> </div> </li> </ul> <ul class="right"> <li><a href="/">basket</a> </li> </ul> </nav>
the issue because event attached a
element, therefore when user tries mouse on menu appears mouseout
part of hover called on a
, js code tries hide menu again. fix this, attach hover()
event element holds both a
, menu. in case, .menu-link
element itself. need change next()
find()
cater change. try this:
$(document).ready(function() { $('.menu-link').hover(function() { $(this).find('.menu').show(); }, function() { $(this).find('.menu').hide(); }); });
nav { position: fixed; top: 0; left: 0; width: 100%; /*background-color: #333333;*/ background-color: transparent; transition: 0.2s; } nav ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: transparent; display: inline-block; } nav ul li { float: left; } nav ul li a.link { display: block; color: black; text-align: center; padding: 24px 16px; text-decoration: none; transition: 0.2s; font-weight: bold; text-transform: uppercase; position: relative; } nav ul li div.arrow-down { float: right; width: 0; height: 0; margin-left: 5px; border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 5px solid #fff; margin-top: 10px; transition: 0.2s; } nav ul li div.menu { display: none; z-index: 999; position: relative; background-color: #fff; box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2); padding: 15px 0; margin-top: -15px; } nav ul li div.menu { color: #333333; padding-left: 15px; } nav ul li a.link:hover { color: #808080; text-decoration: none; transition: 0.2s; } nav ul li a.link:hover div.arrow-down { border-top: 5px solid #808080; transition: 0.2s; } nav ul li:first-child { padding-left: 100px; } nav ul.right { float: right; } nav ul.right li:last-child { padding-right: 100px; } nav.color { background-color: #333333; transition: 0.2s; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <ul class="left"> <li class="menu-link"> <a href="" class="link">home</a> <div class="menu"> <a href="">subcat.title</a> </div> </li> <li class="menu-link"> <a href="" class="link">test</a> <div class="menu"> <a href="">subcat.title</a> </div> </li> </ul> <ul class="right"> <li><a href="/">basket</a> </li> </ul> </nav>
also note can achieve in css alone without need js code, remove hover()
handler , add line css:
nav ul li:hover div.menu { display: block; }
Comments
Post a Comment