本文转自:http://pietschsoft.com/post/2010/11/17/HTML5-Day-4-Add-DropDown-Menu-ASPNET-MVC-HTML5-Template-using-CSS-and-jQuery

Today I was playing around with using jQuery to create a drop down menu. I thought I’d share an example of adding a drop down menu to the new ASP.NET MVC HTML5 Template that I created on Day 2.

Before we begin, you’ll need to create a new site using the code from Day 2. You can either copy/paste the code from the article, or download the full source code. You’ll find the download link at the bottom of the post for Day 2.

Here’s a screenshot of the finished dropdown menu:

Here’s another screenshot of the menu in Chrome:

Add Sub-Menu Items

First, modify the <nav> item in the Site.Master file to include sub-menu items as <ul> child elements added to the “nav ul” element. Below is an example:

<nav>
<ul>
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<li>
<a href="#">Customers</a>
<ul>
<li><a href="#">Create New</a></li>
<li><a href="#">Latest</a></li>
<li><a href="#">View All</a></li>
</ul>
</li>
<li>
<a href="#">Orders</a>
<ul>
<li><a href="#">Create New</a></li>
<li><a href="#">Latest: Last 30 Days</a></li>
<li><a href="#">Search: Date Range</a></li>
<li><a href="#">Search: Customer</a></li>
<li><a href="#">View All</a></li>
</ul>
</li>
<li><%: Html.ActionLink("About", "About", "Home")%></li>
</ul>
</nav>

Of course don’t forget to replace the “<a href” tags with “Html.ActionLink” calls to wire up the Views in your application. For this article, we’ll just leave the code like this.

Add CSS Styles to the Sub-Menus

We do not need to modify the styles in the “Site.css” file for this. We just need to add some CSS to style the sub-menus appropriately.

Here’s the minimum styles necessary for the sub-menu to display correctly:

<style>
body nav ul ul {
display: none;
position:absolute;
}
</style>

Even though the above style will make the sub-menus display correctly (hide on page load, and absolutely positioned), we still need to style them to match the overall style of the ASP.NET MVC site template.

Here’s the full CSS necessary to style the sub-menus in the new ASP.NET MVC HTML5 Template:

<style>
body nav ul ul {
display: none;
position:absolute;
width: 100%; background: #5C87B2;
border-top: 5px solid #5C87B2;;
filter:alpha(opacity=80);
-moz-opacity:0.8;
-khtml-opacity: 0.8;
opacity: 0.8;
}
body nav ul ul li a{
line-height: 3.1em;
}
</style>

This CSS colors the background of the sub-menu area the same color blue as the page, but it also makes it semi-transparent. So any content behind the sub-menu will show through just a little bit.

Let’s wire things up with jQuery

Now we just need a little jQuery code to wire up our sub-menus to display when the use hovers the mouse over their parents. The jQuery code I’m using for this was inspired by Dan Wellman’s “How to Create a Drop-down Nav Menu with HTML5, CSS3 and jQuery” post.

$(function () {
$("body nav li").each(function () {
if ($(this).find("ul").length > 0) { //show subnav on hover
$(this).mouseenter(function () {
$(this).find("ul").stop(true, true).slideDown();
}); //hide submenus on exit
$(this).mouseleave(function () {
$(this).find("ul").stop(true, true).slideUp();
}); $(this).find("ul").mousemove(function () {
$(this).stop(true, true).show();
});
}
});
});

As you can see, with jQuery the javascript code necessary to wire this up is rather short. Oh, and did I mention that the sub-menus slide down into place?

Conclusion

I know this post isn’t so much HTML5 as it is jQuery and CSS. But, after all, isn’t HTML5 really about the same thing as jQuery? RIA.

I know, yesterday, I said that today I would move on to adding some “patch” code to add some HTML5 support to older browsers. I decided to take a detour back to the ASP.NET MVC HTML5 template. We shall see, again, where tomorrow takes us.

[转]HTML5 Day 4: Add Drop Down Menu to ASP.NET MVC HTML5 Template using CSS and jQuery的更多相关文章

  1. add a path cgi-bin to asp.net mvc

    1.简单,但是会丢失请求数据 protected void Application_BeginRequest() { string url = HttpContext.Current.Request. ...

  2. 基于HTML5的Drag and Drop生成图片Base64信息

    HTML5的Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过Drag ...

  3. show drop down menu within/from action bar

    show drop down menu within/from action bar */--> pre { background-color: #2f4f4f;line-height: 1.6 ...

  4. atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结

    atitit.D&D drag&drop拖拽文件到界面功能 html5 web 跟个java swing c#.net c++ 的总结 1. DND的操作流程 1 2. Html5 注 ...

  5. [转]Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC - Part 4

    本文转自:http://www.asp.net/mvc/overview/older-versions/using-the-html5-and-jquery-ui-datepicker-popup-c ...

  6. Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor

    Webservice WCF WebApi   注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...

  7. html5shiv 是一个针对 IE 浏览器的 HTML5 JavaScript 补丁,目的是让 IE 识别并支持 HTML5 元素。

    html5shiv 是一个针对 IE 浏览器的 HTML5 JavaScript 补丁,目的是让 IE 识别并支持 HTML5 元素. 各版本html5shiv.js CDN网址:https://ww ...

  8. 通过HTML5的Drag and Drop生成拓扑图片Base64信息

    HTML5 原生的 Drag and Drop是很不错的功能,网上使用例子较多如 http://html5demos.com/drag ,但这些例子大部分没实际用途,本文将搞个有点使用价值的例子,通过 ...

  9. 炫酷的html5(Drag 和 drop)拖放

    在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放.也是一种常见的特性,即抓取对象以后拖到另一个位置. 浏览器支持 Internet Explorer 9.Firefox.Opera 12.C ...

随机推荐

  1. 多校1010 Taotao Picks Apples

    >>点击进入原题<< 思路:题解很有意思,适合线段树进阶 #include<cstdio> #include<cmath> #include<cs ...

  2. idea 获取当前git最新分支

    菜单栏VCS->选中Git 选择Fetch 获取最新分支

  3. Spring使用DriverManagerDataSource和C3P0分别配置MySql6.0.6数据源

    首先,看一下项目路径 先说spring配置文件吧,这个比较重要 <?xml version="1.0" encoding="UTF-8"?> < ...

  4. 百练4152:最佳加法表达式(dp+高精度)

    描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放1个加号,最好的摆法就是12+34,和为36 输入有不超 ...

  5. 猎豹CEO傅盛:与周鸿祎、雷军、马化腾、马云的的相爱相杀

    百度百科:傅盛,男,1978年3月6日出生在江西景德镇,毕业于山东工商学院信息管理与信息系统专业. 2003年加入3721公司.2005年加入奇虎360,带领团队打造了安全类软件360安全卫士.200 ...

  6. [luoguP1586] 四方定理(DP 背包)

    传送门 相当于背包, f[i][j] 表示当前数为 i,能分解成 j 个数的平方的和的数量 那么就是统计背包装物品的数量 ——代码 #include <cmath> #include &l ...

  7. 撸呀撸的左手(KMP+DP)

    题目描述 撸呀撸很迷茫,因为他的左手总是不受控制,做一些不雅的事情.于是撸呀撸一狠心,决定戒撸.没想到,他的左手受不了寂寞,一闲下来就在键盘上各种乱敲. 唔,神奇的左手表示,safasfasaafaf ...

  8. noip模拟赛 花

    [问题描述]商店里出售n种不同品种的花.为了装饰桌面,你打算买m支花回家.你觉得放两支一样的花很难看,因此每种品种的花最多买1支.求总共有几种不同的买花的方案?答案可能很大,输出答案mod p的值. ...

  9. Problem 2669

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  10. Ubuntu 16.04下UML建模PowerDesigner的替代ERMaster和MySQL Workbench

    ERMaster是Eclipse的一个插件,小巧,支持连接各种数据库,还能生成代码等.安装参考:http://www.cnblogs.com/EasonJim/p/6170686.html 当然还有一 ...