• [转]HTML5 Day 4: Add Drop Down Menu to ASP.NET MVC HTML5 Template using CSS and jQuery


    本文转自: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:

    ASPNETMVCHTML5DropDownMenu_IE8

    Here’s another screenshot of the menu in Chrome:

    ASPNETMVCHTML5DropDownMenu_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;
         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.

     

  • 相关阅读:
    匿名变量
    Vue父子组件传值与非父子传值
    TCP三次握手分析
    @media screen 响应式布局
    H5新增多媒体标签
    npm+node+vue配置一套带走
    vue+echarts全国疫情地图
    js本地时间格式化
    vue iview分页
    Vue打包后访问静态资源路径问题
  • 原文地址:https://www.cnblogs.com/freeliver54/p/3607549.html
Copyright © 2020-2023  润新知