Text link: Step 1:creating the app structure
Translation Date: July 5, 2014
Translators: Anchor
In this section, you will use some pre-built polymer elements to create a basic application structure, including a toolbar (toolbar) and some tabs (tabs).
Edit index.html File
Go to the root directory belowStarterdirectory, open with your favorite text editorindex.htmlfile, the initial content of the file is roughly as follows:
<!doctype html>
Description:
- This document defines some basic styles (styles) and embeds the platform.js script file, which can provide all the missing platform features.
- Element link rel= "Import" is a new way of introducing an HTML file as a resource in HTML, called HTML import.
Skip the middle of those styles fresh (style) at the bottom of the file and you'll see something fresh:
... <body unresolved touch-action= "Auto" ></body> ...
Description:
- The unresolved attribute of the <body> Element (attribute) is designed to prevent flicker (flash) of custom, non-style elements (FOUC) that are not supported by native (native) in the browser. Please refer to the Polymer style reference documentation for details.
- The touch-action= "Auto" property is intended to ensure that touch events are handled properly in some browsers.
-------------------------------------------------------------------------------------
Next we'll add some HTML import link in the right place to introduce<core-header-panel>,<core-toolbar>And<paper-tabs>Elements:
<script src= ". /components/platform/platform.js "> <link rel=" import " href=". /components/font-roboto/roboto.html "> <!--below is the element that needs to be added, if garbled, delete the Chinese content, or save as UTF-8 encoding- <link rel= "Import" href= ". /components/core-header-panel/core-header-panel.html "> <link rel=" import " href=". /components/core-toolbar/core-toolbar.html "> <link rel=" import " href=". /components/paper-tabs/paper-tabs.html ">
Description:
- Polymer uses HTML imports technology to load components. HTML Imports provides dependency management to ensure that custom elements and all of their dependencies are loaded before they are used.
- In the original tutorial, the code that needs to be added is displayed in black text (because the CSDN pre is limited, so the reader is asked to identify it).
-------------------------------------------------------------------------------------
To add a toolbar (toolbar), the following code can be added inside the body tag:
<core-header-panel> <core-toolbar> </core-toolbar> <!--The main page content will be here-- </core-header-panel>
After saving/After deployment refresh the page, you will see a blue toolbar at the top.
Description:
- The <core-header-panel> element is a simple container that contains a header (in this case, the <core-toolbar> Element) and some content. By default, the header remains at the top of the screen, but it can also be set to scroll with the content.
- <core-toolbar> elements as containers, which can hold tabs (tab), menu buttons, and other controls.
-------------------------------------------------------------------------------------
add tab (tab)
The app uses tabs (tab, sometimes called tags) to switch between two different interfaces (view, tab, view), including a message list (messages), and a Favorites/watchlist (favorites).<paper-tabs>Elements are implemented in a manner similar to<select>element, but it is displayed as a collection of some tabs.
<core-header-panel> <core-toolbar><!--Add some tabs, paper, slips, cards--><paper-tabs valueattr= " Name ' selected= ' all ' self-end> <paper-tab name= ' All ' > All </paper-tab> <paper-tab name= ' Favorites "> Favorites </paper-tab> </paper-tabs> </core-toolbar> <!--The main page content will be placed here- </core-header-panel>
Refresh the page after save/deploy, you will see the top toolbar and two tabs, switch to try?
Description:
- By default, <paper-tabs> identifies which child element is selected by index value. valueattr= "name" specifies which should be selected based on the child element's Name property.
- selected= "All" selects the first tab (tab) As the Initial tab (because the first name= "all").
- Here, the child element is the <paper-tab> element, and when you touch, these elements change style and display the "Ink Ripple" animation effect.
-------------------------------------------------------------------------------------
Add a <script> tag to the end of the file (inside the body) to handle the tab switch event:
<script> //Get tab DOM element paper-tabs var tabs = document.queryselector (' paper-tabs '); Add Event listener, it is obvious that you need Chrome browser to run //Here each switch will trigger 2 times, the previous tab is unchecked, and the New tab is selected tabs.addeventlistener (' Core-select ', Function (e) { // var detail = e["Detail"] | | {}; var item = detail["Item"] | | {}; var isSelected = detail["isSelected"]; Console.log ( "tab (\" + item["InnerText"] + "\") Changeto: "+ isSelected +"; ["+ tabs.selected +"] isSelected " ); }); </script>
Description:
- The <paper-tab> element triggers the core-select event When you select a tab. You can interact with him like a built-in element.
- Now there is nothing to switch, and later in the section you will accomplish this.
Save (it is recommended to save at any time during the editing process, this is a good coding habit) file, deploy, and then use Chrome to open the access link, for example
Http://localhost:8080/polymer-tutorial-master/starter/index.html
Figure first polymer app run effect.
If there is any error, you canstep-1Under the directoryindex.htmlFile for comparison, of course, you can also directly access the file to try.
In this section, you introduce some custom elements through HTML imports and use them to create a simple app layout.
Think: Can I use other sub-elements in <paper-tabs>, tryOr<span>, huh?
next section
Step 2: Create your own elements