- Community · 2019-06-05 · 共同问题! Bootstrap((轮播图组件的 引用方式

23
<Web Components /> <!– the future of Web development ---> 陈本峰 2014 6

Transcript of - Community · 2019-06-05 · 共同问题! Bootstrap((轮播图组件的 引用方式

<Web Components /> <!– the future of Web development --->

陈本峰 2014-6

关于我

信仰

404

作品

关于我

关于我

⼀一⾏行JS代码将⺴⽹网⻚页移动化

前端框架  

共同问题  

Bootstrap    轮播图组件的引用方式  

如果可以这样…

<my-slider>

<img src=“1.jpg” />

<img src=“2.jpg” />

</my-slider>

Web Components

•  Template • Custom Element •  Shadow DOM • HTML Imports

Template  

⺫⽬目前的做法

<script id="indexTemplate" type="text/template">

<h1>Inside a template </h1>

<img src="/image/xyz.png"/>

</script>

Template  

<template id="indexTemplate”>

<h1>Inside a template</h1>

<img src="/image/xyz.png"/>

</template>

var template = document.getElementById('indexTemplate');

holder.appendChild(template.content.cloneNode(true));

使⽤用<template>元素

Template  

•  内容被解析,但是不显⽰示

•  图⽚片资源不会被下载

•  脚本资源不会被下载或者执⾏行

… 直到它被使⽤用

<template>元素特点

Custom  Element  

<my-slider> </my-slider>

Custom  Element  

<template>

<!– 关于my slider的所有代码 -->

</template>

<script>

var tmpl = document.querySelector('template');

var ImgSliderProto = Object.create(HTMLElement.prototype);

ImgSliderProto.createdCallback = function() {

var root = this.createShadowRoot();

root.appendChild(document.importNode(tmpl.content, true));

};

var ImgSlider = document.registerElement(’my-slider‘, { prototype: ImgSliderProto});

</script>

利⽤用Custom Element创建<my-slider>元素

Shadow  DOM  

Shadow  DOM  

Shadow  DOM  

Shadow  DOM  

•  挂载在某⼀一个元素下⾯面,但是不在DOM⾥里⾯面显⽰示

•  完全独⽴立当前⽂文档,不受当前⽂文档样式的影响

Shadow DOM 特点

HTML Imports

<link rel="import" href=”my-slider.html">

完整代码⽰示例

myslider.html 代码  

index.html 代码  

浏览器⽀支持

Web Components 开源项⺫⽬目

h;p://www.polymer-­‐project.org/  

http://x-tags.org/

http://amazeui.org

陈本峰 [email protected]    | 13552800186 | QQ: 85436