polymer初探_html/css_WEB-ITnose
最近找了几个模块化的方案,posthtml还不是很成熟,css module需要和react一起比较好用,于是尝试了下polymer。
polymer是基于web component规范的,hello-world-polymer可以让我们快速的熟悉polymer。
polymer模块html,css,js都是写一起的, hello-word.html 代码如下
<!-- Imports polymer --><link rel="import" href="../../polymer/polymer.html"><!-- Defines element markup --><dom-module id="hello-world"> <template> <p>Hello <strong>{{who}}</strong> :)</p> </template></dom-module><!-- Registers custom element --><script>Polymer({ is: 'hello-world', properties: { who: { type: String, value: 'World' } }});</script>
定义好模块后,只要在 index.html 文件引入模块,然后用
<!doctype html><html><head> <meta charset="utf-8"> <title><hello-world></title> <!-- Imports polyfill --> <script src="../webcomponentsjs/webcomponents-lite.min.js"></script> <!-- Imports custom element --> <link rel="import" href="build/hello-world.html"></head><body> <!-- Runs custom element --> <hello-world who="world"></hello-world></body></html>
多模块也是没问题的,我们新建一个 hello-module.html ,并且给她一点样式
<!-- Imports polymer --><link rel="import" href="../../polymer/polymer.html"><!-- Defines element markup --><dom-module id="hello-module"> <style> p{ color: red; display: flex; } strong{ color: black; } </style> <template> <p>Hello <strong>{{who}}</strong> :)</p> </template></dom-module><!-- Registers custom element --><script>Polymer({ is: 'hello-module', properties: { who: { type: String, value: 'Module' } }});</script>
然后在 index.html 引入
<!doctype html><html><head> <meta charset="utf-8"> <title><hello-world></title> <!-- Imports polyfill --> <script src="../webcomponentsjs/webcomponents-lite.min.js"></script> <!-- Imports custom element --> <link rel="import" href="build/hello-module.html"> <link rel="import" href="build/hello-world.html"></head><body> <!-- Runs custom element --> <hello-module who="module"></hello-module> <hello-world who="world"></hello-world></body></html>
浏览器显示是这样的,polymer已经帮我们加好命名空间,样式是不会相互影响的。
但是一些css3属性怎么办呢,我们还需要autoprefixer或者cssnext。需要三个插件支持,在命令行输入
npm i --save gulp-posthtml posthtml-postcss postcss-cssnext
然后修改 gulpfile.js 文件
var gulp = require('gulp'), postcssPlugins = [require('postcss-cssnext')({ browsers: ['last 10 versions'] })]gulp.task('html', function() { var posthtml = require('gulp-posthtml'); return gulp.src('modules/*.html') .pipe(posthtml([ require('posthtml-postcss')(postcssPlugins) ]/*, options */)) .pipe(gulp.dest('build/'));});gulp.task('watch', function() { gulp.watch("modules/**.html",["html"]);});gulp.task('default', ['html', 'watch']);
在命令行输入 gulp 就会实时帮我们编译了。生成的模块代码如下
<!-- Imports polymer --><link rel="import" href="../../polymer/polymer.html"><!-- Defines element markup --><dom-module id="hello-module"> <style> p{ color: red; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; } strong{ color: black; } </style> <template> <p>Hello <strong>{{who}}</strong> :)</p> </template></dom-module><!-- Registers custom element --><script>Polymer({ is: 'hello-module', properties: { who: { type: String, value: 'Module' } }});</script>
这样浏览器就支持了,测试了一下,polymer支持安卓4.1,如果测试没什么问题,就可以愉快的用上了。

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

HTML sesuai untuk pemula kerana mudah dan mudah dipelajari dan dapat melihat hasilnya dengan cepat. 1) Keluk pembelajaran HTML adalah lancar dan mudah dimulakan. 2) Hanya menguasai tag asas untuk mula membuat laman web. 3) Fleksibiliti yang tinggi dan boleh digunakan dalam kombinasi dengan CSS dan JavaScript. 4) Sumber pembelajaran yang kaya dan alat moden menyokong proses pembelajaran.

HTML mentakrifkan struktur web, CSS bertanggungjawab untuk gaya dan susun atur, dan JavaScript memberikan interaksi dinamik. Ketiga melaksanakan tugas mereka dalam pembangunan web dan bersama -sama membina laman web yang berwarna -warni.

Anexampleofastartartingtaginhtmlis, yangbeginsaparagraph.startingtagsareessentialinhtmlasttheyinitiateelements, definetheirtypes, andarecrucialforstructuringwebpagesandconstructionthedom.

WebDevelopmentReliesOnhtml, CSS, andjavascript: 1) HtmlStructuresContent, 2) CSSStylesit, dan3) JavaScriptaddsInteractivity, Formingthebasisofmodernwebexperiences.

Algoritma Adaptif Kedudukan Y-Axis untuk Fungsi Anotasi Web Artikel ini akan meneroka cara melaksanakan fungsi anotasi yang serupa dengan dokumen perkataan, terutama bagaimana menangani selang antara anotasi ...

Giteepages Statik Laman Web Penggunaan Gagal: 404 Penyelesaian Masalah dan Resolusi Ralat Semasa Menggunakan Gitee ...

Untuk mencapai kesan penyebaran dan membesarkan imej sekitarnya selepas mengklik pada imej, banyak reka bentuk web perlu mencapai kesan interaktif: klik pada imej tertentu untuk membuat sekitar ...

HTML, CSS dan JavaScript adalah tiga tiang pembangunan web. 1. HTML mentakrifkan struktur laman web dan menggunakan tag seperti, dan sebagainya. 2. CSS mengawal gaya laman web, menggunakan pemilih dan atribut seperti warna, saiz font, dan lain-lain.
