jquery:附加一个div会导致鼠标悬停/鼠标悬停循环

发布于 2024-10-31 22:22:55 字数 3634 浏览 0 评论 0原文

我有一个用于导航菜单的 ul。当用户将鼠标悬停在子 li 上时,我会向 li 附加一个类为“nav-about”的 div(或 nav-无论该 li 的类是什么),然后向下滑动附加的 div。我遇到一个问题,我的代码陷入循环,并且 li 的鼠标悬停/鼠标移出来回切换,直到我将鼠标移出 li。感谢您提供的所有帮助。

html

<div id="nav" class="nav">
    <ul id="header-menu" class="mainnav ">
        <li class="about">
            <a class="" id="about" href="http://www.example.com/projects/joanroot/about/">About</a>
        </li>
        <li class="paintings">
            <a class="" id="paintings" href="http://www.example.com/projects/joanroot/paintings/">Paintings</a>
        </li>
        <li class="worksonpaper">
            <a class="" id="worksonpaper" href="http://www.example.com/projects/joanroot/works-on-paper/">Works On Paper</a>
        </li>
        <li class="lectureseries">
            <a class="" id="lectureseries" href="http://www.example.com/projects/joanroot/lecture-series/">Lecture Series</a>
        </li>
        <li class="contact">
            <a class="" id="contact" href="http://www.example.com/projects/joanroot/contact/">Contact</a>
        </li>
    </ul>
</div>

css

div#nav {
    overflow: hidden;
}
.mainnav, #nav > ul {
    background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 0 transparent;
    float: left;
    list-style: none outside none;
    margin: 0;
    padding: 0;
    width: 973px;
}
#nav > ul > li {
    display: inline;
}
#nav > ul > li > a {
    display: block;
    float: left;
    height: 54px;
    overflow: hidden;
    text-indent: -9999px;
    z-index: 100;
}
#nav ul li a#about {
    width: 154px;
}
div.nav-about {
    background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 -55px transparent;
    height: 54px;
    overflow: hidden;
    position: absolute;
    width: 154px;
}

jquery

InitilizeMenu('#header-menu',400,'slide','slide');

function InitilizeMenu(parent,moveSpeed,inStyle,outStyle) {
    $(parent).children('li').each(function() {
        var thisClass = $(this).attr('class');
        AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle);
    });
}

function AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle) {
    $(parent+' .'+thisClass).mouseover(function() {
        $(this).append('<div class="nav-'+thisClass+'"></div>')
        switch(inStyle) {
            case 'slide':
                $('div.nav-'+thisClass).css({display:'none'}).slideDown(moveSpeed);
                break;
            case 'fade':
                $('div.nav-'+thisClass).css({display:'none'}).fadeIn(moveSpeed);
                break;
            case 'none':
                $('div.nav-'+thisClass);
                break;
            default:
                $('div.nav-'+thisClass);
                break;
        }
    }).mouseout(function(){
        switch(outStyle) {
            case 'slide':
                $('div.nav-'+thisClass).slideUp(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'fade':
                $('div.nav-'+thisClass).fadeOut(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'none':
                $('div.nav-'+thisClass).remove();
                break;
            default:
                $('div.nav-'+thisClass).remove();
                break;
        }
    });
}

I have a ul that I'm using for a nav menu. When a user mouses over the child li, I append to the li a div with the class of "nav-about" (or nav-whatever the class is for that li), and slideDown the appended div. I'm having a problem where my code get's caught in a loop and the li's mouseover/mouseout toggle back and forth until I move the mouse out of the li. Thank you for any and all help.

html

<div id="nav" class="nav">
    <ul id="header-menu" class="mainnav ">
        <li class="about">
            <a class="" id="about" href="http://www.example.com/projects/joanroot/about/">About</a>
        </li>
        <li class="paintings">
            <a class="" id="paintings" href="http://www.example.com/projects/joanroot/paintings/">Paintings</a>
        </li>
        <li class="worksonpaper">
            <a class="" id="worksonpaper" href="http://www.example.com/projects/joanroot/works-on-paper/">Works On Paper</a>
        </li>
        <li class="lectureseries">
            <a class="" id="lectureseries" href="http://www.example.com/projects/joanroot/lecture-series/">Lecture Series</a>
        </li>
        <li class="contact">
            <a class="" id="contact" href="http://www.example.com/projects/joanroot/contact/">Contact</a>
        </li>
    </ul>
</div>

css

div#nav {
    overflow: hidden;
}
.mainnav, #nav > ul {
    background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 0 transparent;
    float: left;
    list-style: none outside none;
    margin: 0;
    padding: 0;
    width: 973px;
}
#nav > ul > li {
    display: inline;
}
#nav > ul > li > a {
    display: block;
    float: left;
    height: 54px;
    overflow: hidden;
    text-indent: -9999px;
    z-index: 100;
}
#nav ul li a#about {
    width: 154px;
}
div.nav-about {
    background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 -55px transparent;
    height: 54px;
    overflow: hidden;
    position: absolute;
    width: 154px;
}

jquery

InitilizeMenu('#header-menu',400,'slide','slide');

function InitilizeMenu(parent,moveSpeed,inStyle,outStyle) {
    $(parent).children('li').each(function() {
        var thisClass = $(this).attr('class');
        AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle);
    });
}

function AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle) {
    $(parent+' .'+thisClass).mouseover(function() {
        $(this).append('<div class="nav-'+thisClass+'"></div>')
        switch(inStyle) {
            case 'slide':
                $('div.nav-'+thisClass).css({display:'none'}).slideDown(moveSpeed);
                break;
            case 'fade':
                $('div.nav-'+thisClass).css({display:'none'}).fadeIn(moveSpeed);
                break;
            case 'none':
                $('div.nav-'+thisClass);
                break;
            default:
                $('div.nav-'+thisClass);
                break;
        }
    }).mouseout(function(){
        switch(outStyle) {
            case 'slide':
                $('div.nav-'+thisClass).slideUp(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'fade':
                $('div.nav-'+thisClass).fadeOut(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'none':
                $('div.nav-'+thisClass).remove();
                break;
            default:
                $('div.nav-'+thisClass).remove();
                break;
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2024-11-07 22:22:55

为什么不使用 .hover() 代替:

function AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle) {
    $(parent+' .'+thisClass).hover(function() {
        $(this).append('<div class="nav-'+thisClass+'"></div>')
        switch(inStyle) {
            case 'slide':
                $('div.nav-'+thisClass).css({display:'none'}).slideDown(moveSpeed);
                break;
            case 'fade':
                $('div.nav-'+thisClass).css({display:'none'}).fadeIn(moveSpeed);
                break;
            case 'none':
                $('div.nav-'+thisClass);
                break;
            default:
                $('div.nav-'+thisClass);
                break;
        }
    }, function() {
        switch(outStyle) {
            case 'slide':
                $('div.nav-'+thisClass).slideUp(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'fade':
                $('div.nav-'+thisClass).fadeOut(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'none':
                $('div.nav-'+thisClass).remove();
                break;
            default:
                $('div.nav-'+thisClass).remove();
                break;
        }
    });
}

Why not use .hover() instead:

function AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle) {
    $(parent+' .'+thisClass).hover(function() {
        $(this).append('<div class="nav-'+thisClass+'"></div>')
        switch(inStyle) {
            case 'slide':
                $('div.nav-'+thisClass).css({display:'none'}).slideDown(moveSpeed);
                break;
            case 'fade':
                $('div.nav-'+thisClass).css({display:'none'}).fadeIn(moveSpeed);
                break;
            case 'none':
                $('div.nav-'+thisClass);
                break;
            default:
                $('div.nav-'+thisClass);
                break;
        }
    }, function() {
        switch(outStyle) {
            case 'slide':
                $('div.nav-'+thisClass).slideUp(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'fade':
                $('div.nav-'+thisClass).fadeOut(moveSpeed,function(){
                    $(this).remove();
                });
                break;
            case 'none':
                $('div.nav-'+thisClass).remove();
                break;
            default:
                $('div.nav-'+thisClass).remove();
                break;
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文