移动开发流量省起来之Zepto

来源:互联网 发布:mac 命令行安装mysql 编辑:程序博客网 时间:2024/06/11 21:10

博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助的可以关注我。 转载请注明"深蓝的镰刀"


事情是这样的:最近开发的一个手机网站客户反应访问起来特别慢,刷了半天才能看到页面,然后问我们是不是网站出问题了。于是我赶紧找了各种手机测试一下,没有问题,首先排除程序错误的问题,然后服务器用的又是云服务器,只要不是在国外应该不会太慢才对,打听了一下,原来是该客户用的是2G网络访问的,说是在信号不好的地方几乎刷不出页面。。。

虽然说客户的网络质量令人堪忧,不过作为一个有点追求(强迫症)的攻城狮还是决定帮他优化一下。

页面另存为,打开一个页面所需要加载的所有资源包括图片、js、html一共才300K(jquery库特地使用了压缩版的,只有100K左右。。。),将图片各种压缩,最终还是有160多K,看来只有把jquery库给去了,但是将所有的jquery代码改写成原生js又有一种想死的感觉,而且很多兼容问题层出不穷,那么有没有一个既像jquery这么好用的,又能省流量的js库呢?答案是:Zepto

一张图说明Zepto.js的优势:


jquery 1.x最新版284KB,压缩后94KB;jquery2.x最新版247KB,压缩后84KB;Zepto最新版54KB,压缩后9KB!!!

然后看看功能方面。

选择器

<html><body><ul id="items">    <p>This is it!</p></ul><script src="./zepto1.1.6.js"></script><script>    alert($("#items").html());</script></body></html>

追加

<html><body><ul id="items">    <p>This is it!</p>    <p>Hello</p></ul><script src="./zepto1.1.6.js"></script><script>    $('ul').append('<p>new list item</p>')   </script></body></html>

克隆 (注:zepto的clone()方法不能像jquery的clone()可以同时克隆data和绑定事件)

<html><body><ul id="items">    <p>This is it!</p>    <p>Hello</p></ul><script src="./zepto1.1.6.js"></script><script>    $('ul').append($("#items").clone())   </script></body></html>

ajax

$.ajax({  type: 'GET',  url: '/projects',  data: { name: 'Zepto.js' },  dataType: 'json',  timeout: 300,  context: $('body'),  success: function(data){    this.append(data.project.html)  },  error: function(xhr, type){    alert('Ajax error!')  }})$.ajax({  type: 'POST',  url: '/projects',  data: JSON.stringify({ name: 'Zepto.js' }),  contentType: 'application/json'})

因为Zepto是jQuery-compatible的,所有如果你会使用jquery,那么你已经会了Zepto。以上这些用法基本与jquery一致,下面说几个我看到的与jquery不同的地方。

1.Zepto基础库不支持很多css选择器

比如很常用的$(":selected"),$("p:eq(1)"),$("li:first")这类,不过Zepto的库提供很多拓展的模块,你只需要在他的官网上按需要编译你需要的模块然后保存为zepto.js即可,或者直接使用在线编译,其中如果开启了selector模块,你就能支持大部分的css选择器了。

2.如果你开启了detect模块,那么你就可以用Zepto判断用户设备、操作系统和浏览器的功能(测试了几个还算可以用,不知是否准确)

<html><body><ul id="items">    <p>This is it!</p></ul><!-- 该js必须开启了detect模块 --><script src="./zepto.js"></script><script>    // general device typealert($.os.phone);alert($.os.tablet);// specific OSalert($.os.ios);alert($.os.android);alert($.os.webos);alert($.os.blackberry);alert($.os.bb10);alert($.os.rimtabletos);// specific device typealert($.os.iphone);alert($.os.ipad);alert($.os.ipod); // [v1.1]alert($.os.touchpad);alert($.os.kindle);// specific browseralert($.browser.chrome);alert($.browser.firefox);alert($.browser.safari); // [v1.1]alert($.browser.webview); // (iOS) [v1.1]alert($.browser.silk);alert($.browser.playbook);alert($.browser.ie); // [v1.1]</script></body></html>


3.如果开启了form模块,就可以对你的表单进行数据序列化,类似jquery的jquery form插件

<html><body><form><input name="user" value="xxx" type="text"/><input name="password" value="123" type="password"/></form><!-- 该js必须开启了form模块 --><script src="./zepto.js"></script><script> var formData = $('form').serializeArray(); alert(formData[0]['name']); alert(formData[1]['name']); alert(formData[0]['value']); alert(formData[1]['value']);</script></body></html>

4.如果开启了touch模块,你就可以使用tap(触屏点击) 和 swipe(触屏滑动),类似Jquery mobile 插件

<html><body><style>.delete { display: none; }#items{font-size:30px;}</style><ul id="items">  <li>List item 1 <span class="delete">DELETE</span></li>  <li>List item 2 <span class="delete">DELETE</span></li></ul><!-- 该js必须开启了touch模块 --><script src="./zepto.js"></script><script>$('#items li').swipe(function(){  $('.delete').hide()  $('.delete', this).show()})$('.delete').tap(function(){  $(this).parent('li').remove()})</script></body></html>

注:Zepto的swipe事件在某些Android手机(如安卓4.4)仍存在不起作用的情况。期待Zepto修复这个bug。

这么多有用的模块如果用jquery来实现,除了需要加载那个压缩后还有90多KB的核心库外,我还要加载各种插件诸如jquery mobile,jquery form,jquery detect等等,这个大小不用我说,要多臃肿多臃肿,而Zepto全部开启模块后只有39KB,所以说作为业绩良心省流量的手机网站还是使用Zepto吧。

总的来说,Zepto像是一个Jquery体系的一个精简版,专注于移动端,兼顾主流PC浏览器,对于Jquery库的文件大小问题我猜想Jquery在发展的同时可能因为很多历史遗留问题还有需要兼顾各种并不是主流的浏览器导致代码略臃肿。

不过Jquery是否会被替代也难说,毕竟在pc端上几十KB的流量真心不算什么啊。

1 0