Polymer教程:Shadow DOM and Styling

Shadow DOM是用于构建组件的DOM的新属性。

例子

在下面的代码中,标题组件包括页面标题和菜单按钮。

<header-demo>
   <header>
      <h1>
         <button>

Shadow DOM允许在有作用域的子树(称为shadow tree)中定位子级。

<header-demo>
   #shadow-root
   <header>
      <h1>
      <button>

影子根的根称为影子树的顶部,连接到树的元素称为影子主机(header-demo)。该影子主机包含一个名为shadowRoot的属性,该属性指定影子根。影子根使用host属性标识host元素。

Shadow DOM和合成

如果阴影DOM中有一个元素,则可以渲染阴影树而不是元素的子元素。可以通过将<slot>元素添加到阴影树来渲染该元素的子元素。

例如,对<header-demo>使用以下阴影树。

<header>
   <h1><slot></slot></h1>
   <button>Menu</button>
</header>

将子级添加到<my-header>元素中,作为:

<header-demo>Shadow DOM</header-demo>

标头将</ slot>元素替换为上述指定的子元素-

<header-demo>
   <header>
      <h1>Shadow DOM</h1>
      <button>Menu</button>
   </header>
</header-demo>

后备内容

当没有节点分配给该插槽时,可以显示回退内容。例如-

<my-element>
   #shadow-root
   <slot id = "myimgicon">
      <img src = "img-demo.png">
   </slot>
   <slot></slot>
<my-element>

您可以为元素提供自己的图标,如下所示:

<my-element>
   <img slot = "myimgicon" src = "warning.png">
<my-element>

多层次分配

您可以将slot元素分配给一个插槽,这称为多级分发。

例如,采用两个级别的影子树,如下所示-

<parent-element>
   #shadow-root
      <child-element>
      <!-- display the light DOM children of parent-element inside child-element -->
      <slot id = "parent-slot">
	  
   <child-element>
      #shadow-root
         <div>
            <!-- Render the light DOM children inside div by using child-element -->
            <slot id = "child-slot">

考虑以下代码:

<parent-element>
   <p>This is light DOM</p>
<parent-element>

扁平树的结构如下所示。

<parent-element>
   <child-element>
      <div>
         <slot id = "child-slot">
            <slot id = "parent-slot">
               <p>This is light DOM</p>

Shadow DOM使用以下Slot API来检查分发-

  • HTMLElement.assignedSlot-如果没有为插槽分配元素,则为元素分配插槽并返回null。
  • HTMLSlotElement.assignedNodes-当您将flatten选项设置为true时,它提供节点列表以及插槽并返回分布式节点。
  • HTMLSlotElement.slotchange-插槽的分布式节点发生更改时触发此事件。

事件重新定位

它指定事件的目标,可以在其中将元素表示为与侦听元素相同的作用域。它提供了来自自定义元素的事件,该事件看起来像来自自定义元素标签,而不是其中的元素。

例子

以下示例显示了Polymer.js中事件重定位的用法。创建一个名为index.html的文件,并将以下代码放入其中。

<!doctype html>
<html>
   <head>
      <title>Polymer Example</title>
      <script src = "bower_components/webcomponentsjs/webcomponents-lite.js"></script>
      <link rel = "import" href = "bower_components/polymer/polymer.html">
      <link rel = "import" href = "retarget-event.html">
   </head>
   
   <body>
      <template id = "myapp" is = "dom-bind">
         <retarget-event on-tap = "clicky"></retarget-event>
      </template>
      
      <script>
         var myval = document.querySelector('#myapp');
         myval.clicky = function(e) {
            console.log("The retargeted result:", Polymer.dom(myval));
            console.log("Normal result:", e);
         };
      </script>
   </body>
</html>

现在,创建另一个名为retarget-event.html的文件,并包含以下代码。

<link rel = "import" href = "bower_components/polymer/polymer/element.html">

//it specifies the start of an element's local DOM
<dom-module id = "retarget-event">

   <template>
      <span>Click on this text to see result in console...</span>
   </template>

   <script>
      Polymer ({
         is: 'retarget-event',
      });
   </script>
</dom-module>

输出

要运行该应用程序,请导航到创建的项目目录并运行以下命令。

polymer serve

现在打开浏览器并导航到http://127.0.0.1:8081/。以下将是输出:

retarget-event
Click on this text to see result in console...

单击上面的文本,然后打开控制台以查看重新定位的事件。

shadow DOM样式

您可以使用样式属性设置阴影DOM的样式,样式属性从主机继承到阴影树。

例子

<style>
   .mydemo { background-color: grey; }
</style>

<my-element>
#shadow-root
   <style>
      //this div will have blue background color
      div { background-color: orange; }
   </style>
   <div class = "mydemo">Demo</div>

DOM模板

可以使用DOM模板为元素创建DOM子树。您可以为元素创建影子根,并通过将DOM模板添加到元素来将模板复制到影子树中。

可以通过两种方式指定DOM模板-

  • 创建一个<dom-module>元素,该元素应与元素名称以及id属性匹配。
  • 在<dom-module>中定义一个<template>元素。

例子

<dom-module id = "my-template">
   <template>I am in my template!!!</template>

   <script>
      class MyTemplate extends Polymer.Element {
         static get is() { return  'my-template' }
      }
      customElements.define(MyTemplate.is, MyTemplate);
   </script>
</dom-module>

设置元素的Shadow DOM的样式

Shadow DOM允许使用样式属性(如字体,文本颜色和类)对自定义元素进行样式设置,而无需将其应用于元素范围之外。

让我们使用:host选择器设置host元素的样式(连接到影子DOM的元素称为host)。创建一个名为polymer-app.html的文件,并在其中添加以下代码。

<link rel = "import" href = "../../bower_components/polymer/polymer/element.html">

<dom-module id = "polymer-app">
   <template>
      <style>
         :host {
            color:#33ACC9;
         }
      </style>
      <h2>Hello...[[myval]]!</h2>	
  </template>

  <script>
      class PolymerApp extends Polymer.Element {
         static get is() { return 'polymer-app'; }
         static get properties() {
            return {
               myval: {
                  type: String, value: 'Welcome to Web176教程网!!!'
               }
            };
         }
      }

      window.customElements.define(PolymerApp.is, PolymerApp);
   </script>
</dom-module>

运行该应用程序,然后导航至http://127.0.0.1:8000/。以下将是输出:

Hello...Welcome to Web176教程网!!!

样式版位内容

可以在元素的模板中创建插槽,这些插槽在运行时被占用。

例子

以下示例描述了元素模板中带槽内容的用法。创建一个index.html文件,并在其中添加以下代码。

<!doctype html>
<html>
   <head>
      <title>Polymer Example</title>
      <link rel = "import" href = "bower_components/polymer/polymer.html">
      <link rel = "import" href = "slotted-content.html">
   </head>
   
   <body>
      <slotted-content>
         <div slot = "text">This is Polymer.JS Slotted Content Example</div>
      </slotted-content> 
   </body>
</html>

现在创建另一个名为slotted-content.html的文件,并包含以下代码。

<link rel = "import" href = "bower_components/polymer/polymer/element.html">

<dom-module id = "slotted-content">
   <template>
      <style>
         ::slotted(*) {
            font-family: sans-serif;
            color:#E94A9D;
         }
      </style>
      
      <h2>Hello...[[prop1]]</h2>
      <h3>
         <div><slot name='text'></slot></div>
      </h3>
   </template>
   
   <script>
      Polymer ({
         is: 'slotted-content', properties: {
            prop1: {
               type: String,
               value: 'Welcome to Web176教程网!!!',
            },
         },
      });
   </script>
</dom-module>

运行上一个示例中所示的应用程序,然后导航到http://127.0.0.1:8081/。以下将是输出:

Hello...Welcome to Web176教程网!!!
This is Polymer.JS Slotted Content Example

使用样式模块

您可以在元素之间以及样式模块之间共享样式。在样式模块中指定样式,然后在元素之间共享它们。

例子

下面的示例演示如何在元素之间使用样式模块。创建一个index.html文件,并在其中添加以下代码。

<!doctype html>
<html>
   <head>
      <title>Polymer Example</title>
      <link rel = "import" href = "bower_components/polymer/polymer.html">
      <link rel = "import" href = "style-module.html">
   </head>
   
   <body>
      <style-module></style-module> 
   </body>
</html>

使用以下代码创建另一个名为style-module.html的文件。

<link rel = "import" href = "bower_components/polymer/polymer/element.html">

<dom-module id = "style-module">
   <template>
      <!-- here, including the imported styles from colors-module page -->
      <style include="colors-module"></style>
      <style>
         :host {
            font-family: sans-serif;
            color: green;    
         }
      </style>
      
      <h2>Hello...[[prop1]]</h2>
      <p class = "color1">Sharing styles with style modules 1...</p>
      <p class = "color2">Sharing styles with style modules 2...</p>
      <p class = "color3">Sharing styles with style modules 3...</p>
   </template>
   
   <script>
      Polymer ({
         is: 'style-module', properties: {
            prop1: {
               type: String, value: 'Welcome to Web176教程网!!!',
            },
         },
      });
   </script>
</dom-module>

现在,再创建一个名为colors-module.html的文件,该文件为元素提供样式模块,如以下代码所示。

<link rel = "import" href = "bower_components/polymer/polymer/element.html">

<dom-module id = 'colors-module'>
   <template>
      <style>
         p.color1 {
            color: #EA5AA5;
         }
         p.color2 {
            color: #4B61EA;
         }
         p.color3 {
            color: #D3AA0A;
         }
      </style>
   </template>
</dom-module>

运行该应用程序,然后导航到http://127.0.0.1:8081/,看下效果。

使用自定义属性

自定义CSS属性可用于使用Polymer元素来设置应用程序中元素的外观样式。自定义属性提供了级联CSS变量,可以在自定义元素的环境之外使用这些变量,以免通过样式表分散样式数据。

可以类似于自定义的DOM树继承的标准CSS属性那样定义自定义属性。在上一个示例中,您可以看到为元素定义的自定义CSS属性。

在CSS继承下,如果没有为元素定义样式,则它将从其父项继承样式,如以下代码所示。

<link rel = "import" href = "components/polymer/myelement-style.html">
<myelement-style>
   <style is = "myelement-style">
      p {
         color: var(--paper-red-900);
      }
      paper-checkbox {
         --paper-checkbox-checked-color: var(--paper-red-900);
      }
   </style>
</myelement-style>

<body>
   <p><paper-checkbox>Check Here</paper-checkbox></p>
</body>

作者:terry,如若转载,请注明出处:https://www.web176.com/polymer/2279.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年4月2日 下午3:20
下一篇 2021年4月2日 下午4:39

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注