The difference between the component usage of WeChat applet and traditional HTML5 tags

The difference between applet and traditional HTML5

The mini program has just been opened for public beta, and various interpretations and speculations have begun in the Internet circle. Some of them believe that Mini Programs are closely related to HTML5, and even Mini Programs are developed based on HTML5.

After careful study of documentation and code development, from the perspective of the view layer, Applets are still significantly different from traditional HTML5. The main differences are:

Development tools are different.

Different from the H5 development tool + browser Device Mode preview mode, the development of the applet is based on its own developer tool, which can realize a complete set of processes such as synchronizing local files + developing and debugging + compiling + previewing + uploading + publishing.

Development languages ​​are different.

Mini Program has developed a set of WXML markup language and WXSS style language, instead of directly using standard HTML5+CSS3.

Component packaging is different.

The applet is independent of many native APP components, and the functions that need to be simulated in HTML5 can be directly called in the applet.

Mini Program Developer Tools

The development tool of WeChat applet is based on the MINA framework (the name has now been cancelled), and the official tool is now called the WeChat web developer tool. The applet development tool is a framework based on the Native System system layer. Since it does not run in the browser, some JavaScript methods such as Document and Window cannot be used in the web.

Traditional HTML5 is limited by the network environment when loading. It needs to load HTML, CSS, and JS sequentially, then return the data, and finally render the page and display it in the browser. Users often have to wait a long time and experience suffers.

In contrast, the two threads of the applet: Appservice Thread and View Thread will be loaded at the same time and in parallel, and even Appservice Thread will be executed earlier. When the view thread is loaded, Appservice will be notified, and Appservice will use setData to prepare the data. The method returns to the view thread.

This optimization strategy of the applet can reduce the user's waiting time and speed up the response speed of the applet.

WXML

1. Label

WXML is closer to XML in syntax and follows the SGML specification. It is different from the arbitrary tag closure method of HTML language. WXML language must include a start tag and an end tag. Take the image tag as an example, the following two writing methods are supported:

or

这里需要注意的是:

所有组件与属性都是小写,以连字符-连接。

2、文件引入

WXML提供两种文件引入方式,import和include。区别在于:import可以引入定义好的template模板,模板是有作用域的;而include就是拷贝一个公用的代码片段到目标文件中,适合做公共页面片的拆分。

文件引入在小程序做模块化拆分的过程中非常重要。

WXSS

1、尺寸单位

WXSS支持的单位有px、rem和rpx,其中rem和rpx可以针对屏幕容器进行适配,px则为固定尺寸。

其中1rpx=0.5px,在WXSS和WXML中定义的rpx单位最终会转换为在手机端可以识别的rem单位。

建议:开发微信小程序时设计师可以用 iPhone6 作为视觉稿的标准。

所以工程师拿到750的设计稿,在PS中量取的容器大小,可以直接定义为rpx,不需要进行2倍尺寸的换算。

view{

font-size:26rpx;

width:400rpx;

height:400rpx;

}

备注:rpx的单位不光在样式中会自适应,写在WXML的style里也会根据屏幕自适应。

2、样式引入

看到很多文章说小程序不支持样式的@import,其实官方公布的第一个正式开发者工具就已经支持了。

import "../../wxss/common.wxss";

3、选择器

小程序支持的选择器在官方公布的文档中包括.class、#id、 element、element,element、::after(注意是双冒号)、::before这6种选择器。

经过测试,小程序对于:first-child、:last-child、.class-a .class-b{},甚至更多层级的嵌套都是支持的。

不过官方并不推荐级联的这种写法,因为考虑到后面切Native的扩展可能,会没办法支持级联选择。

所以保险起见,不建议.class-a .class-b{}这种级联的写法,以免后期工具过滤导致页面错乱。

组件

小程序在0.10.102800的版本中加入了 textarea,并即将废弃操作反馈的系列组件。

下面一一来分析下:

1、view

div和view都是盒模型,默认display:block。

盒模型在布局过程中,一般推荐display:flex的写法,配合justify-content:center;align-items:center;的定义实现盒模型在横向和纵向的居中。

2、text

除了text文本节点以外的其他节点都无法长按选中。。

截止到0.10.102800的开发者工具text支持嵌套text,不过有class的text会被面板过滤,样式不影响。

* 友情提示!

3、 icon

icon可以直接用微信组件默认的图标,默认是iconfont格式的,从WeUI那边沿袭过来的一种做法。

自定义的icon推荐svg sprite格式或者iconfont。

目前来看,市面上还没有很好的自动合并单个svg为svg sprite的工具,需要手动拼图。

4、input

input 的类型,有效值:text, number, idcard, digit, time, date 。

input不需要设置line-height或padding来纵向居中,默认placeholder的文字是居中的。

小程序把checkbox和radio都单独做成了组件,默认的input只支持输入文本。

上传文件在小程序里需要调用chooseImage事件完成;

小程序CSS里的 :focus 不生效,需要修改placehoder的样式,通过placeholder-class=”class”来定义。

.login .input-group input::-webkit-input-placeholder {

color: #c0c0c0;

}

.login .input-group input:focus::-webkit-input-placeholder {

color: transparent;

}

5、picker

picker默认支持普通、日期和时间三种选择器。

picker通过bindchange事件来调取range中自定义的数据数据,并展示到页面中,调用的是系统原生的select。

这里小程序废弃了select组件,考虑到的是这个组件的交互不适合移动场景,最终用picker代替了。

选择 北京 上海

{{area[index]}}

Page({

data: {

area: ['中国', '美国', '巴西', '日本'],

}

})

6、 navigator

navigator支持相对路径和绝对路径的跳转,默认是打开新页面,当前页面打开需要加redirect;

navigator仅支持5级页面的跳转;

navigator不可跳转到小程序外的链接地址;

登录页

在小程序开发工具里,默认打开新页面,工具左上角有返回按钮。加上redirect,当前页打开,不出现返回按钮。

7、image

小程序的image与HTML5的img最大的区别在于:小程序的image是按照background-image来实现的。

默认image的高宽是320*240。必须通过样式定义去覆盖这个默认高宽,auto在这里不生效。(开发者说这样设置的原因是:如果设置 auto ,页面布局会因为图片加载的过程有一个闪的现象(例如高度从 0 到 height ),所以要求一定要设置一个宽度和高度。)

最新的api支持获取图片的高宽。不过这里返回的高宽是px单位,不支持屏幕自适应;

图片包括三种缩放模式scaleToFill、aspectFit、aspectFill和9种裁剪模式,三种缩放模式的实现原理对应如下:

scaleToFill{

background-size:100% 100%;//不保持纵横比缩放图片,使图片的宽高完全拉伸至填满 image 元素

}

aspectFit{

background-size:contain;//保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。

}

aspectFill{

background-size:cover;//保持纵横比缩放图片,只保证图片的短边能完全显示出来。也就是说,图片通常只在水平或垂直方向是完整的,另一个方向将会发生截取。

}

8、button

额外补充下button的实现方式,button的边框是用:after方式实现的,用户如果在button上定义边框会出现两条线,需用:after的方式去覆盖默认值。不过这个应该会在最近的开发者工具中修复。

button::after {

content:" ";

width:200%;

height:200%;

border:1px solid rgba(0, 0, 0, 0.2);

}

小程序不支持button:active这种样式的写法,button的点击态通过.button-hover{}的样式覆盖,也可修改hover-class为自定义的样式名。

9、css3动画

最新版的开发工具已经支持transition和keyframes动画,不用js苦哈哈的写动画队列了。

除了官方公布的小程序的组件之外,有一些标签比如,span、em、strong、b也是支持的,只是官方并不推荐使用。

浏览器内核

在iOS平台上,微信的浏览器渲染内核是wkwebview;

而在Android平台上,微信则采用了腾讯QQ浏览器2016年4月份发布的X5内核(blink内核)作为渲染引擎。

在小程序的开发工具上,小程序的JavaScript是运行在chrome内核(nwjs)中。

autoprefixer

小程序会在接下来的版本中加入自动补全css前缀,使用的插件是postcss的autoprefixer,设置的兼容级别是> ios 8及> android 4.1。

const browserOptions = {

browsers: [

'last 3 versions',

'ios >= 8',

'android >= 4.1',

]

}

也就是说,我们在写css的时候只需要写没有前缀的写法。比如:display:flex,工具自动编译为display:flex;display:-webkit-flex。

OM小程序实战

上图为OM小程序的开发界面。下面我们从布局、智能裁图和loading动画几个方面简单说下OM小程序具体的UI开发经验。

1、flex布局

以上图om的文章列表为例,文章的形态包括纯文字的和图文混合的。图文混合的文字不管是1行还是2行都需要相对于图片纵向居中。用flex的布局,就可以实现这3种状态不修改CSS文件,只按需隐藏DOM结构就搞定。

.media {

display: flex;

justify-content:center;//设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式。

align-items:center;//定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式。

}

.media .content {

flex: 1;

}

在做传统H5的时候,为了兼容各种低端设备的机型,通常不太敢轻易尝试flex,但在小程序里就可以大胆的使用了。

2、智能裁图

正是由于小程序把图片处理成背景图片,OM的素材管理页面图片的实现方式在这里遇到了一个挑战。

简单列举下om各种不同尺寸图片在平台上的展示方案。

[color=rgb(153, 153, 153) !important]1、高<50px:

(1)宽<175px,原图居中显示;

(2)宽>175px,定宽等比上下居中显示;

2、50px<高<400px:

(1)宽<175px,原图居中显示;

(2)宽>175px,定宽等比显示;

3、高>400px:

(1)宽<175px,原图居中显示、超出400px高度隐藏;

(2)宽>175px,定宽等比显示、超出400px高度隐藏;

这种方案,用css和img实现起来,只需要设置外层盒子最大高宽,图片自适应缩放即可。

.pic-list .pic-item .pic-body .pic {

width: 100%;

text-align: center;

overflow: hidden;

max-height: 4rem;

min-height: 0.5rem;

display: -webkit-box;

-webkit-box-orient: horizontal;

-webkit-box-align: center;

-webkit-box-pack: center;

}

.pic img {

-webkit-box-flex: 1;

}

然而因为小程序里是用背景图片的方式,简单的css设置并不能实现智能裁图的方案。需要配合js计算出不同尺寸图片对应的适配尺寸。

.pic-list .pic-item .pic-body .pic {

width: 100%;

text-align: center;

overflow: hidden;

max-height: 800rpx;

min-height: 100rpx;

display:flex;

align-items:center;

justify-content:center;

}

这里需要后台接口提供数据列表的图片高宽,js对拿到图片的不同尺寸进行算法计算,重新赋值再返回给数据。

3、css3动画改变默认loading

小程序默认提供的loading是普通的菊花loading,这里OM使用自定义的keyframes序列帧动画。

.icon-loading {

animation: loadingWhite 1.2s infinite linear;

animation-timing-function: steps(10);

}

@keyframes loadingWhite {

0% {

background-position:0 0;

}

100%{

background-position:-500rpx 0;

}

}

微信小程序集成了很多原生APP的组件,从体验和页面流畅度来说,都会比HTML5要优秀很多。

微信小程序相对于HTML5开发来说,除了熟悉API需要学习成本之外,开发难度指数3颗星,还是很容易上手的。

开发者工具、组件和API目前刚刚对外公测,还不算太成熟,里面还有很多坑需要开发者去填。

文章篇幅有限,在这里只能简单从UI开发角度介绍下小程序的开发经验。有关小程序的更多API,可以查阅小程序开发文档 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324687704&siteId=291194637