Example of Menu creation by Menu and Menuitem elements in HTML 5

Source: Internet
Author: User
Menu and MenuItem are talking about the hottest two elements in the developer, probably because the mainstream browsers lack enough support for them. When I wrote this article, Firefox had already implemented this element.

Comparison of menu and Nav

When it comes to menu, it's hard to confuse the nav element. The document specification is a good tool to differentiate these two elements.

The nav element is an HTML navigation element that represents the navigation block of a Web page and typically contains a links collection that allows users to jump or jump to other site pages within a page.
Menus are a collection of menu commands that are similar to desktop or mobile apps. Desktop applications usually use a toolbar menu or context menu to show a variety of tasks. The fundamental difference between NAV and menu is that the Nav element contains navigation links to help users jump between Web pages, and menu should allow users to perform specific tasks.

Using the menu Element

In a container, the menu element is used to create contexts, toolbars, and pop-up menus. However, the following two features do not have a browser implementation, including Firefox. At the moment, it's hard to guess how the browsing will achieve them and what it will look like. However, it is also a good opportunity for the toolbar and popup menus to make some changes in the next generation interaction design.

For now, we will focus on the context.
Context

When we right-click an application, a context menu appears. The options displayed depend on where the user clicks.

JavaScript Options

Adding a context menu to a Web page through JavaScript and jquery plug-ins is achievable. The problem is that this method requires additional markup, and the script deletes the local menu of the browser, which, if handled improperly, will disappoint the user.

Local Solutions

Menu and MenuItem are used together to merge new menus into the local context menu. For example, add a menu called "Hello World" to the body

<body contextmenu= "New-context-menu" >
<menu id= "New-context-menu" type= "context" >
    < Menuitem>hello world</menuitem>
</menu>
</body>
In the preceding code fragment, the basic properties contained are Id,type and contextmenu-that specify the menu type as the context, and also specify the area where the new menu item should be displayed.
In the example, when you right-click the mouse, the new menu item appears anywhere in the document, because we specify that its action area is body.
Of course, you can limit the range of new menu items by assigning values to ContextMenu on specific elements, such as div,main,section, and so on.
<body>
    <div contextmenu= "New-context-menu" >
    <!--content-->
    </div>
    < Menu id= "New-context-menu" type= "context" >
        <menuitem>hello world</menuitem>
    </menu>
</body>

When viewed in Firefox, you will find that the newly added menu item is added to the top of the list.

Add submenus and icons

A submenu consists of a set of similar or mutual menu items. The image rotation in PS is a typical example. It is easy and intuitive to add submenus using the menu. Look at the following sample code:

<menu id= "Demo-image" type= "context" >
    <menu label= "image rotation" >
        <menuitem>rotate 90 </menuitem>
        <menuitem>rotate 180</menuitem>
        <menuitem>flip horizontally</ menuitem>
        <menuitem>flip vertically</menuitem>
    </menu>
</menu>

When running in a browser that supports the menu element, you will see four submenus added to the new menu:

Icon
The code is as follows Copy Code
&NBSP;&N Bsp Introduce a new attribute: icon, use this property to add an icon next to the menu. It is worth mentioning that the Icon property can only be used in MenuItem elements. Example code:
<menu id= "Demo-image" type= "context" >
        <menu label= "Image rotation" > <menuitem icon= "img/arrow-return-090.png" >rotate 90</menuitem> <menuitem icon= "Img/arrow-return-180.png" >rotate 180</menuitem> <menuitem icon= "Img/arrow-stop -180.png ">flip horizontally</menuitem> <menuitem icon=" Img/arrow-stop-270.png ">Flip Vertically</ 
menuitem> </menu> </menu> 

Results as you can see:

Add features to the menu

We've built some examples that look like menus, but there's no functionality at all. When the menu is clicked, the user expects something to happen. For example click Copy should copy text or link, click New folder should create a new one. You can implement these features in JavaScript.

Note: Before I start, I suggest you take a look at Jeremy McPeak's course in JavaScript fundamentals, a good start for anyone who wants to learn JavaScript.
Using the above "image rotation" for example, let's add a function that rotates the image when clicked. CSS 3 Transform and transition can be implemented for us in the browser. Rotate the image to a 90-degree style as follows:
. rotate-90 {
    transform:rotate (90deg);
}
To use this style, you need to write a function to apply it to the image.
function Imagerotation (name) {
    document.getElementById (' image '). ClassName = name;
Associate this function with each MenuItem onclick property and pass an argument: rotate-90
<menu id= "Demo-image" type= "context" >
    <menu label= "image rotation" >
        <menuitem onclick= " Imagerotation (' rotate-90 ') "icon=" Img/arrow-return-090.png ">rotate 90</menuitem> <menuitem icon=
        " Img/arrow-return-180.png ">rotate 180</menuitem> <menuitem icon=" img/arrow-stop-180.png ">Flip"
        horizontally</menuitem>
        <menuitem icon= "Img/arrow-stop-270.png" >flip Vertically</menuitem>
    </menu>
</menu>
After that, create the style to rotate the picture 180 degrees and crop the picture, add each function to the standalone MenuItem, and remember to pass the parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.