移动开发流量省起来之Zepto

针对客户反映手机网站访问速度慢的问题,通过替换jQuery库为更轻量级的Zepto.js,有效减少了网页加载资源体积,提升了用户体验。Zepto.js不仅体积小至9KB,还提供了丰富的功能模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

移动开发流量省起来之Zepto

转载于深蓝的镰刀



事情是这样的:最近开发的一个手机网站客户反应访问起来特别慢,刷了半天才能看到页面,然后问我们是不是网站出问题了。于是我赶紧找了各种手机测试一下,没有问题,首先排除程序错误的问题,然后服务器用的又是云服务器,只要不是在国外应该不会太慢才对,打听了一下,原来是该客户用的是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="http://www.cnphp6.com/archives/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="http://www.cnphp6.com/archives/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="http://www.cnphp6.com/archives/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="http://www.cnphp6.com/archives/zepto.js"></script>
 <script>
     // general device type
 alert($.os.phone);
 alert($.os.tablet);
 
 // specific OS
 alert($.os.ios);
 alert($.os.android);
 alert($.os.webos);
 alert($.os.blackberry);
 alert($.os.bb10);
 alert($.os.rimtabletos);
 
 // specific device type
 alert($.os.iphone);
 alert($.os.ipad);
 alert($.os.ipod); // [v1.1]
 alert($.os.touchpad);
 alert($.os.kindle);
 
 // specific browser
 alert($.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="http://www.cnphp6.com/archives/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="http://www.cnphp6.com/archives/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在发展的同时可能因为很多历史遗留问题还有需要兼顾各种并不是主流的浏览器导致代码略臃肿。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值