更快的 Jquery 下拉菜单代码?

发布于 2024-10-15 09:40:06 字数 5138 浏览 1 评论 0原文

我用 jQuery 代码制作了以下 ul 菜单,并想知道 jQuery 代码是否可以变得更快和/或更高效。我正在使用 hoverintend 插件来实现鼠标悬停时下拉菜单的延迟。所以我不想让菜单的下降速度更快,而只是想让 Jquery 代码更高效(如果可能的话)。

更新: 好吧,我又清理了一些 css/jQuery 代码。感谢 Sime Vidas,放弃了悬停功能。但我仍然想知道 jQuery 代码是否可以变得更加高效。它的菜单代码适用于 chrome、FF 和 IE8。所以人们可以将它用于自己的项目。

HTML 示例:

<div id="top_navbar">
    <ul id="topmenu1" class="topmenu">
      <li class="menuchild"><a title="Home" href="#" class="mselected"> <span>Home</span></a>
            <ul class="subnav">
                <li><a title="Historie" href="#"><span>option 1</span></a></li>
                <li class="mselected"><a title="Routebeschrijving" href="#"><span>option 2</span></a></li>
                <li><a title="User Area" href="#"><span>option 3</span></a></li>
                <li><a title="Disclaimer" href="#"><span>option 4</span></a></li>
            </ul>
       </li>    
       <li class="menuchild"><a title="Testmenu" href="#"><span>Testmenu</span></a>
             <ul class="subnav">
                <li><a href="#"><span>option 1</span></a></li>
                <li><a href="#"><span>option 2</span></a></li>
                <li><a href="#"><span>option 3</span></a></li>
                <li><a href="#"><span>option 4</span></a></li>
                <li><a href="#"><span>option 5</span></a></li>
                <li><a href="#"><span>option 6</span></a></li>
                <li><a href="#"><span>option 7</span></a></li>
              </ul>
        </li>
        <li><a title="empty menu" href="#"><span>Empty menu</span></a></li>
   </ul>
</div>

jQuery 代码:

var $DropdownMenu = $("#topmenu1");
var $DivSubNav= $DropdownMenu.find(".subnav");
var $MenuHover= $DivSubNav.find("li");

$DropdownMenu.find(".menuchild ul").parent().append("<span class=\"has_child\">&or;</span>");
$DropdownMenu.find("ul.mselected").parents("li").children("a").addClass("childselected");
$DivSubNav.each(function () {
    $(this).parent().eq(0).hoverIntent({
        timeout: 100,over: function () {
            var current = $(".subnav:eq(0)", this);current.slideDown(100);
        },out: function () {
            var current = $(".subnav:eq(0)", this);current.slideUp(200);
        }
    });
});  

样式代码:

#top_navbar{ 
position:absolute;
z-index:3000;
top:93px;
left:15px;
width:865px;
margin:0;
padding:0;
background-image:none;
background-color:#f9f9f9;
border:2px solid #dfdfdf;
height:26px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
}

/* DROP DOWN MENU */
#topmenu1{
list-style:none;
padding:0;
margin:0;
float:left;
font-size:12pt;
border:0 solid red;
background:transparent;
}

#topmenu1 li{
float:left;
margin:0;
padding:0 5px 0 5px;
position:relative;
margin-left:4px;
border-right:0 solid #EFEFEF;
cursor:pointer;
}

#topmenu1 li a{
padding: 2px;
color: #223147;
display:block;
text-decoration: none;
float: left;
cursor:pointer;
font-size:12pt;
}

#topmenu1 li a:hover{
color:#0aaafd;
}

#topmenu1 .has_child{
width: 10px;
height: 24px;
float: left;
background-image: url(/images/menu/arrow_down_blue2.gif); /* down arrow image */
background-repeat:no-repeat;
background-position:center;
border:0 solid red;
padding-left:3px;
}

#topmenu1 .subnav {
list-style: none;
position: absolute; /* Keeps subnav from affecting main navigation flow */
left: -1px; top: 26px;
margin: 0; padding: 0;
display:none;
float: left;
border: 2px solid #cfcfcf;
border-top:2px solid #fafafa;
z-index:20000;
width:190px;
background-color:#f9f9f9;
border-radius: 0px 0px 5px 5px;
-moz-border-radius-bottomleft:5px;
-moz-border-radius-bottomright:5px;
-webkit-border-bottom-left-radius:5px;
-webkit-border-bottom-right-radius:5px;
}

#topmenu1 .subnav li{
margin: 0; padding: 0;
clear: both;
background-color:transparent;
border:0 solid green;
width:190px;
position:relative;   
}

html #topmenu1 .subnav li a {
padding:0;
display:block;
font-size:11pt;
width:inherit;
text-align:left;
color:#223147;
cursor:pointer;
width:100%;
}

html #topmenu1 .subnav li a span{
padding-left:10px;
}

/* Hover effect for subnav links */
html #topmenu1 .subnav li a:hover {
color:#fff;
background-color:#0aaafd;
border:0 solid red;
margin-right:-5px;
}

/* Hover effect for subnav links */
#topmenu1 .mselected span{ 
color:#0aaafd;
}

#topmenu1 .subnav .mselected a:hover span{ 
color:#FFF;
}
#topmenu1 .childselected span{
color:#0aaafd;
}

I have made the following ul menu with jQuery code and want to know if it the jQuery code can be made faster and/or more efficient. I'm using the hoverintend plugin for the delay in the dropdown on mouseover. So I do not want to make the menu's drop faster but only the Jquery code more efficient if it is possible.

Update:
Ok I cleaned the css/jQuery code some more. Ditching the hover function thanks to Sime Vidas. But I still wonder if the jQuery code can be made more efficient. It menu code works chrome, FF and IE8. So people can use it for their own projects.

The HTML example:

<div id="top_navbar">
    <ul id="topmenu1" class="topmenu">
      <li class="menuchild"><a title="Home" href="#" class="mselected"> <span>Home</span></a>
            <ul class="subnav">
                <li><a title="Historie" href="#"><span>option 1</span></a></li>
                <li class="mselected"><a title="Routebeschrijving" href="#"><span>option 2</span></a></li>
                <li><a title="User Area" href="#"><span>option 3</span></a></li>
                <li><a title="Disclaimer" href="#"><span>option 4</span></a></li>
            </ul>
       </li>    
       <li class="menuchild"><a title="Testmenu" href="#"><span>Testmenu</span></a>
             <ul class="subnav">
                <li><a href="#"><span>option 1</span></a></li>
                <li><a href="#"><span>option 2</span></a></li>
                <li><a href="#"><span>option 3</span></a></li>
                <li><a href="#"><span>option 4</span></a></li>
                <li><a href="#"><span>option 5</span></a></li>
                <li><a href="#"><span>option 6</span></a></li>
                <li><a href="#"><span>option 7</span></a></li>
              </ul>
        </li>
        <li><a title="empty menu" href="#"><span>Empty menu</span></a></li>
   </ul>
</div>

jQuery Code:

var $DropdownMenu = $("#topmenu1");
var $DivSubNav= $DropdownMenu.find(".subnav");
var $MenuHover= $DivSubNav.find("li");

$DropdownMenu.find(".menuchild ul").parent().append("<span class=\"has_child\">∨</span>");
$DropdownMenu.find("ul.mselected").parents("li").children("a").addClass("childselected");
$DivSubNav.each(function () {
    $(this).parent().eq(0).hoverIntent({
        timeout: 100,over: function () {
            var current = $(".subnav:eq(0)", this);current.slideDown(100);
        },out: function () {
            var current = $(".subnav:eq(0)", this);current.slideUp(200);
        }
    });
});  

Style code:

#top_navbar{ 
position:absolute;
z-index:3000;
top:93px;
left:15px;
width:865px;
margin:0;
padding:0;
background-image:none;
background-color:#f9f9f9;
border:2px solid #dfdfdf;
height:26px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
}

/* DROP DOWN MENU */
#topmenu1{
list-style:none;
padding:0;
margin:0;
float:left;
font-size:12pt;
border:0 solid red;
background:transparent;
}

#topmenu1 li{
float:left;
margin:0;
padding:0 5px 0 5px;
position:relative;
margin-left:4px;
border-right:0 solid #EFEFEF;
cursor:pointer;
}

#topmenu1 li a{
padding: 2px;
color: #223147;
display:block;
text-decoration: none;
float: left;
cursor:pointer;
font-size:12pt;
}

#topmenu1 li a:hover{
color:#0aaafd;
}

#topmenu1 .has_child{
width: 10px;
height: 24px;
float: left;
background-image: url(/images/menu/arrow_down_blue2.gif); /* down arrow image */
background-repeat:no-repeat;
background-position:center;
border:0 solid red;
padding-left:3px;
}

#topmenu1 .subnav {
list-style: none;
position: absolute; /* Keeps subnav from affecting main navigation flow */
left: -1px; top: 26px;
margin: 0; padding: 0;
display:none;
float: left;
border: 2px solid #cfcfcf;
border-top:2px solid #fafafa;
z-index:20000;
width:190px;
background-color:#f9f9f9;
border-radius: 0px 0px 5px 5px;
-moz-border-radius-bottomleft:5px;
-moz-border-radius-bottomright:5px;
-webkit-border-bottom-left-radius:5px;
-webkit-border-bottom-right-radius:5px;
}

#topmenu1 .subnav li{
margin: 0; padding: 0;
clear: both;
background-color:transparent;
border:0 solid green;
width:190px;
position:relative;   
}

html #topmenu1 .subnav li a {
padding:0;
display:block;
font-size:11pt;
width:inherit;
text-align:left;
color:#223147;
cursor:pointer;
width:100%;
}

html #topmenu1 .subnav li a span{
padding-left:10px;
}

/* Hover effect for subnav links */
html #topmenu1 .subnav li a:hover {
color:#fff;
background-color:#0aaafd;
border:0 solid red;
margin-right:-5px;
}

/* Hover effect for subnav links */
#topmenu1 .mselected span{ 
color:#0aaafd;
}

#topmenu1 .subnav .mselected a:hover span{ 
color:#FFF;
}
#topmenu1 .childselected span{
color:#0aaafd;
}

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

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

发布评论

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

评论(1

岁月无声 2024-10-22 09:40:06

悬停部分可以优化:

$MenuHover.hover(function () {
    var c = $(this).hasClass('mselected') ? '#acb3ba' : '#0aaafd';        
    $(this).stop(true).css({'background-color':c,'cursor':'pointer'}).children('a').css({'color':'#fff'});
}, function () {
    $(this).stop(true).css({'background-color':'transparent'}).children('a').css({'color':'#223147'});
});

Tho hover part can be optimized:

$MenuHover.hover(function () {
    var c = $(this).hasClass('mselected') ? '#acb3ba' : '#0aaafd';        
    $(this).stop(true).css({'background-color':c,'cursor':'pointer'}).children('a').css({'color':'#fff'});
}, function () {
    $(this).stop(true).css({'background-color':'transparent'}).children('a').css({'color':'#223147'});
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文