Home  >  Article  >  Web Front-end  >  Quickly create menus using the Menu and Menuitem elements in HTML 5_html/css_WEB-ITnose

Quickly create menus using the Menu and Menuitem elements in HTML 5_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:53:011416browse

Translation: Introduction to the elements of Menu and Menuitem in HTML 5

Translator: dwqs

Today I will introduce to you the elements of Menu and Menuitem in HTML 5 Two elements: Menu and Menuitem, these two elements are part of the W3C interactive elements. Now the evolution of the Web is not limited to links between documents. In APPs, pages have more and more behaviors. Therefore, it is time to form a standard for Web interaction.

Menu and Menuiitem are the two most talked about elements among developers, probably due to the lack of sufficient support for them by mainstream browsers. As I write this article, FireFox has implemented this element.

Comparison between Menu and Nav

When it comes to Menu, it is inevitable not to be confused with the Nav element. To differentiate between these two elements, document specifications are a good tool.

Nav element is an HTML navigation element, which represents the navigation block of a Web page. It generally contains a collection of links, allowing users to jump within the page or jump to other website pages.

Menu is a collection of menu commands, somewhat similar to desktop or mobile applications. Desktop applications typically use toolbar menus or context menus to present various tasks. The most fundamental difference between Nav and Menu is that the Nav element contains navigation links to help users jump between Web pages, while Menu should allow users to perform specific tasks.

Using the Menu element

In a container, the Menu element is used to create context, toolbars and pop-up menus. However, the latter two features have not yet been implemented in browsers, including FireFox. At the moment, it's hard to guess how browsing will implement them and what it will look like. However, it is also a good opportunity to make some changes to the specifications of toolbar and popup menu in the next generation of interaction design.

For now, we'll focus on context.

Context

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

JavaScript Options

Adding context menus on web pages is possible through JavaScript and JQuery plugins. The problem is that this approach requires additional markup and the script removes the browser's native menu, which if not handled properly will frustrate the user.

Local solution

Menu and Menuitem are used together to merge the new menu 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 above code snippet, the basic attributes included are id, type and contextmenu ?It specifies the menu type as context, and also specifies the area where the new menu item should be displayed.

In the example, when the mouse is right-clicked, the new menu item will appear anywhere in the document because we specified that its scope is the body.

Of course, you can limit the scope of the new menu item by assigning a value to contextmenu on specific elements, such as div, main, section, etc.

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

Add submenus and icons

A submenu consists of a group of similar or mutual menu items. Image Rotation in PS is a typical example. Adding submenus using Menu is very easy and intuitive. Check out the sample code below:

<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 run in a browser that supports the Menu element, you will see four submenus added to the new menu:

Icon

Introducing a new attribute: icon, use this attribute to add an icon next to the menu. It is worth mentioning that the icon attribute can only be used in menuitem elements. Sample 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>

The result is as you can see:

Add function in menu

We have built some examples that look like menus, but without any functionality. When clicking on a menu, users expect something to happen. For example, clicking Copy should copy text or links, and clicking New Folder should create a new folder. These functions can be implemented using JavaScript.

Note: Before you get started, I recommend checking out Jeremy McPeak’s course at JavaScript Fundamentals, which is a great place to start for anyone who wants to learn JavaScript.

Using the “Image Rotation” example above, let’s add a function that rotates the image when clicked. CSS 3's Transform and Transition can implement this function for us in the browser. The style that rotates an image 90 degrees is as follows:

.rotate-90 {    transform: rotate(90deg);}

In order to use this style, you need to write a function to apply it to the image.

function imageRotation(name) {    document.getElementById('image').className = name;}

把这个函数和每一个menuitem的onclick属性关联,并且传递一个参数: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>

 

完成这个之后,再创建将图片旋转180度和裁剪图片的样式,将每一个函数添加到独立的menuitem中,记得传参数 。查看效果:in the demo page.

查看更多关于Menu元素的信息:Interactive Element: The Menu Element

查看我的示例:http://jsfiddle.net/Web_Code/15pc5zfv/1/embedded/result/

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn