【问题标题】:jQuery - How can I create a sub-menu indicator arrow image with my menu code (also using cufon)?jQuery - 如何使用我的菜单代码(也使用 cufon)创建子菜单指示箭头图像?
【发布时间】:2012-03-01 02:41:23
【问题描述】:

我只想在包含子菜单的菜单链接旁边添加一个箭头。我希望图像正常显示为向下箭头,然后在悬停时变成向上箭头图像。

我设法让“+”符号仅显示在带有子菜单的菜单上,但我不确定如何处理悬停更改部分或如何添加图像而不是文本符号。

HTML:

<div id="menu">
    <ul class="topnav">
      <li><a href="index.html"><img src="images/home.gif" width="75" height="50" alt="Home Page" /></a></li>
      <li><h6><a href="services.html">Top Menu</a></h6>to help you
        <ul class="subnav">
            <li><a href="link.html">Link</a></li>
            <li><a href="link2.html">Link2</a></li>
        </ul>
      </li>
   </ul>

jQuery:

//menu sub navigation
$(document).ready(function(){


$("#menu ul li h6").hover(function() { //When trigger is hovered...

    //Following events are applied to the subnav itself (moving subnav up and down)

    $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

    $(this).parent().hover(function() {
    }, function(){  
        $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
    });


    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() { 
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){  //On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
});

});

CSS:

#menu ul { height:50px; color:#FFF; }
#menu ul li { float:left; padding: 0 36px 0 0; position: relative;  }

#menu li ul.subnav { position: absolute; left: 0; top: 44px; background-     color:#505050;     
margin: 0; padding:0; display: none; float: left; width: 120px; z-index:100;  -moz-border-radius-bottomleft:8px; -moz-border-radius-bottomright:8px; -webkit-border-  bottom-left-radius:8px; -webkit-border-bottom-right-radius:8px; border-top:none; border-bottom:1px solid #444444; border-left:1px solid #444444; border-right:1px solid #444444; height:auto; }
#menu li ul.subnav li { margin: 0; padding: 0; clear: both; width: 120px; z-index:5; border-top:1px solid #444444; }
#menu li ul.subnav li a { margin: 0; padding: 0 10px; float: left; width: 120px; z-index:135; line-height:30px; text-align:left; }
#menu li.current li a { color:#3f3f3f; }

.width_280 { width:280px; }
.margin_0_20_0_0 { margin:0 20px 0 0; }

感谢您的帮助。

【问题讨论】:

    标签: jquery css cufon


    【解决方案1】:

    我认为最传统的答案是在您想要的不同方向上创建小三角形。然后,您可以通过使它们成为背景图像来将它们合并到菜单项中,例如:

    .menu_item {
      padding-left: 20px; /* make room for image */
      background: left middle url(triangle.png) no-repeat;
    }
    

    然后,正如您所做的那样,在“打开”菜单时应用类,并使用 CSS 更改图像。也许:

    .subhover .menu_item {
      background: left middle url(other_triangle.png) no-repeat;
    }
    

    这是基础...您还可以在状态之间制作动画、精灵图像、使用 CSS 构建三角形...

    【讨论】:

    • 谢谢,这就是我最初尝试的方法,但它错位了我的菜单(当然我可以很容易地修复它)。也许是想多了。我试图使用 jQuery 使用“.find”自动添加它,而不是手动使用 css。我将如何使用“.find”来使用我的 jQuery 代码定位子菜单并为箭头添加一个类而不弄乱“subhover”类?
    • 至少在你开始的时候,最简单的方法就是让 jQuery 在悬停时添加类,并使用 CSS 进行样式设置。我可能会尝试保持概念(和命名)的不同:我会添加一个“悬停”类和一个“打开”类(用于具有打开子菜单的菜单项)。您可能还有一个“has_submenu”类。这样,您就可以按照您想要的方式获得所有样式。在你让它工作之后,你也许可以去除一些这种粒度——但如果你在开发过程中将所有这些不同的概念混为一谈,那将会更加混乱。
    猜你喜欢
    • 2014-09-29
    • 2012-08-21
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2019-10-20
    • 2018-11-23
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多