从 2.0 开始,Amaze UI JavaScript 组件转向基于 jQuery 开发,使用时确保在 Amaze UI 的脚本之前引入了 jQuery 最新正式版。
组件的调用方式普通 jQuery 插件一样,具体细节请查看各个组件的文档。
jQuery 和 Zepto.js 表面看起来差不多,其实一些细节上差异很大,同时支持 jQuery 和 Zepto.js 是一件吃力不讨好的事情,这应该也是 Foundation 5 放弃支持 Zepto 的一个原因。(下面列举的差异 Demo)
width()/height() box-sizing)决定padding、border)jQuery 官方的说明:
Note that
.width()will always return the content width, regardless of the value of the CSSbox-sizingproperty. As of jQuery 1.8, this may require retrieving the CSS width plusbox-sizingproperty and then subtracting any potential border and padding on each element when the element hasbox-sizing: border-box. To avoid this penalty, use.css("width")rather than.width().
解决方式就是在 jQuery 中使用 .css('width'),而不是 .width()。
这点上 jQuery 的处理方式是值得商榷的,比如下面的例子,$('.box').css('height') 仍然返回 20px,这不是扯蛋么:
<style>
.box {
box-sizing: border-box;
padding: 10px;
height: 0;
}
</style>
<div class="box"></div>假设用下面的 HTML 和 CSS 画了一个小三角形:
<div class="caret"></div>.caret {
width: 0;
height: 0;
border-width: 0 20px 20px;
border-color: transparent transparent blue;
border-style: none dotted solid;
}.width() 和 .css('width') 都返回 0,高度也一样;.width() 返回 40,使用 .css('width') 返回 0px。所以,这种场景,jQuery 使用 .outerWidth()/.outerHeight();Zepto 使用 .width()/.height()。
offset() top、left、width、heightwidth、height$(htmlString, attributes) $(function() {
var $list = $('<ul><li>jQuery 插入</li></ul>', {
id: 'insert-by-jquery'
});
$list.appendTo($('body'));
});jQuery 操作 ul 上的 id 不会被添加;Zepto 可以在 ul 上添加 id。
$script = $('<script />', {
src: 'http://cdn.amazeui.org/amazeui/1.0.1/js/amazeui.min.js',
id: 'ui-jquery'
});
$script.appendTo($('body'));
$script.on('load', function() {
console.log('jQ script loaded');
});使用 jQuery 时 load 事件的处理函数不会执行;使用 Zepto 时 load 事件的处理函数会执行。
其他参考链接:
Amaze UI 通过特定的 HTML 来绑定事件,多数 JS 组件通过 HTML 标记就可以实现调用。
默认的初始化事件都在 xx.amui.data-api 命名空间下,用户可以自行关闭。
关闭所有默认事件:
$(document).off('.data-api');关闭特定组件的默认事件:
$(document).off('.modal.amui.data-api');一些组件提供了自定义事件,命名的方式为 {事件名称}.{组件名称}.amui,用户可以查看组件文档了解、使用这些事件。
$('#myAlert').on('close.alert.amui', function() {
// do something
});双向数据绑定很酷?Mutation Observer 才是(或即将成为)幕后的英雄。
Amaze UI 2.1 中实验性地引入了 MutationObserver,请谨慎使用。
data-am-observe 在元素上添加 data-am-observe 属性以后,动态插入该元素的 Amaze UI JS 插件会自动初始化(演示), 支持的插件包括 Button、Dropdown、Slider、Popover、ScrollSpy、Tabs。
$().DOMObserve(options, callback) options: 监视的属性(具体参见),默认为 {childList: true, subtree: true};callback(mutations, observer): DOM 发生变化时的处理函数,第一个参数为存储 MutationRecord 对象的数组,第二个参数为 MutationObserver 实例本身。
DOM 变化监视演示,打开控制台查看 log
<p id="js-do-actions">
<button class="am-btn am-btn-primary" data-insert>插入 p 元素</button>
<button class="am-btn am-btn-secondary" data-addClass>添加 Class</button>
<button class="am-btn am-btn-warning" data-remove>移除 p 元素</button>
</p>
<div id="js-do-demo">
<p>DOM 变化监视演示,打开控制台查看 log</p>
</div>
<script>
$(function() {
var $wrapper = $('#js-do-demo');
$wrapper.DOMObserve({
childList: true,
attributes: true,
subtree: true
}, function(mutations, observer) {
console.log(observer.constructor === window.MutationObserver);
console.log('#js-do-demo 的 DOM 发生变化鸟:' + mutations[0].type);
});
$('#js-do-actions').find('button').on('click', function(e) {
var $t = $(e.target);
if ($t.is('[data-insert]')) {
$wrapper.append('<p>插入了一个 p</p>');
} else if($t.is('[data-remove]')) {
$wrapper.find('p').last().remove();
} else {
$wrapper.addClass('am-text-danger');
}
});
})
</script>参考链接:
关于前端模块化,Amaze UI 1.0 的时候曾做过一个关于前端 JS 模块化的调查,截止 2014.11.13 共 1869 个投票:
显然,模块化是必然趋势,ES6 将原生支持模块化。
Amaze UI 2.0 按照 CommonJS 规范来组织模块(前端也像 Node.js 一样编写模块)。最终如何打包,用户可以自行选择。
SPM 貌似不支持直接通过源码提取依赖,使用 Sea.js 的用户可能需要自行修改打包工具。
建议阅读的文章:
