简体   繁体   English

jQuery导航栏无法正常工作

[英]Jquery Navigation bar is not working properly

I am trying to make a navigation type of thing, in HTML and i've added following code for it HTML 我正在尝试用HTML制作导航类型的东西,并且为此添加了以下代码HTML

<div id="sign-wrap">    <div  id="click-box" class="welcome">    
    <p class="welcome" id="name">Welcome Mr. X</p>    </div>    <div id="dropdown">
       <ul>
            <li><a href="/">Insight Pro</a></li>
            <li><p>Mobiles</p></li>
            <li><p>Cameras</p></li>
            <li><p>Laptops</p></li>
            <li><p>Settings</p></li>
            <li><p>Logout</p></li>
       </ul>    </div> </div>

JQ: JQ:

$("#name, #click-box").hover(function(){
    if ($('#dropdown').is(":visible")) {
       $('#dropdown').slideUp(1000);
    }
    else{
        $('#dropdown').slideDown(1000);
    }
});

when I hover at Mr. X #dropdown slides down but when I take my mouse to #dropdown slides up... please help 当我将鼠标悬停在X先生#dropdown#dropdown滑下,但是当我将鼠标移到#dropdown滑下时...

This happens because you have given hover event on two different elements. 发生这种情况是因为您已经在两个不同的元素上提供了hover事件。 When one elements' hover event is invoked then it shows the menu and when you try to go to that menu the other elements' hover is fired which takes your menu bar up (slide up) [You are doing the slide up and down based on the visibility, so for the second firing the div is visible which makes it slide up] 当调用一个元素的悬停事件时,它将显示菜单,当您尝试转到该菜单时,将触发其他元素的悬停,这将使菜单栏向上(向上滑动) 。可见性,因此第二次触发div是可见的,从而使其向上滑动]

Use mouse enter and mouse leave events 使用mouse entermouse leave事件
On mouse enter slideDown and on mouse leave slideUp 在鼠标上输入slideDown,在鼠标上离开slideUp
(And if it's not required to give event on two elements then preferably apply event on one element to prevent flickering effect [if any] ) (并且,如果不需要在两个元素上进行事件传递,则最好在一个元素上应用事件以防止闪烁效果[如果有的话]

$("#name, #click-box").mouseenter(function(){
       $('#dropdown').slideDown(1000);
});

$("#name, #click-box").mouseleave(function(){
       $('#dropdown').slideUp(1000);
});

Refer mouse enter and mouse leave events 引用鼠标进入鼠标离开事件

Hope this solves the issue 希望这能解决问题

Use this... 用这个...

var hovered = false;

$("#name, #click-box").on('hover', function(e){
        $('#dropdown').slideDown(1000);
});

$("#options").on('hover', function(e){
        hovered = true;
});

$("#dropdown").on('mouseleave', function(e){
        hovered = false;
});

$("#sign-wrap").on('mouseleave', function(e){
    if(!hovered){
        $('#dropdown').slideUp(1000);
    }
});

And see this jSfiddle example. 并查看此jSfiddle示例。

Using a click event on the click-box element may make it a little easier for your users to click the menu items. 在click-box元素上使用click事件可能会使您的用户更轻松地单击菜单项。 For example you could use the following snippet 例如,您可以使用以下代码段

$("#click-box").click(function () { $("#dropdown").slideToggle("slow"); });

Hope this helps you some. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM