zepto.js的api地址 http://www.css88.com/doc/zeptojs_api/ javascript
类库源码下载http://www.zeptojs.cn/ 滚动当前页面,看到这部分点击下载css
和使用jquery同样,咱们只要html页面引入便可。
zepto的api与jq几乎同样,同时各个接口名字也是同样的,咱们只要按着写jq同样去写zepto就行了,既然这样,咱们为什么要用zepto,主要就是zepto提供时触摸事件,为开发移动端的更好处理。html
jq是为多个浏览器支持的类库,并无封装上touch事件,若是咱们的项目是pc或者虽然是移动端,可是没有不少触摸事件的效果,咱们选择jq便可,咱们能够看出,咱们采用zepto的时候
java
手机项目的开发,须要触摸事件。jquery
zepto的支持是高级浏览器,ie10以上。css3
1.zepto.js的helloworld输出web
咱们学习以前,须要保证对jq有很熟练的使用技巧,咱们使用zepto,基本的调用以下api
<!DOCTYPE html> <html> <head> <title>demo1</title> </head> <body> zepto.js </body> <script type="text/javascript" src="zepto.min.js"></script> <script type="text/javascript"> // 当页面ready的时候,执行回调: Zepto(function($){ alert('helloworld!') }); </script> </html>
zeopt的代码咱们是放在浏览器
Zepto(function($){app
//code
});
的内部,对比jq,不过是把$换成Zepto了。
2.zepto.js的基本使用
咱们获取div的内容,点击修改按钮,修改div内容,代码以下
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>demo1</title> </head> <body> <div class="con">zepto.js </div> <span class="edit">修改</span> </body> <script type="text/javascript" src="zepto.min.js"></script> <script type="text/javascript"> // 当页面ready的时候,执行回调: Zepto(function($){ $(".edit").click(function(){ $(".con").html("我被修改了!"); }); }); </script> </html>
很完美,不过咱们监听事件,同时zepto推荐的而是经过on的处理,咱们使用on处理,代码以下
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>demo1</title> </head> <body> <div class="con">zepto.js </div> <span class="edit">修改</span> </body> <script type="text/javascript" src="zepto.min.js"></script> <script type="text/javascript"> // 当页面ready的时候,执行回调: Zepto(function($){ $(".edit").on("click",function(){ $(".con").html("我被修改了!"); }); }); </script> </html>
使用被推荐的写法,才会减小问题的出现。经过这段的测试,咱们知道zepto的使用jq几乎同样的
3.zepto.js的触摸事件使用
这才是咱们会选择zepto而不使用jq的缘由,就是使用它提供好的touch模块,咱们先了解这些接口
“touch”模块添加如下事件,
tap
—元素tap的时候触发。
singleTap
and doubleTap
— 这一对事件能够用来检测元素上的单击和双击。(若是你不须要检测单击、双击,使用 tap
代替)。
longTap
— 当一个元素被按住超过750ms触发。
swipe
, swipeLeft
, swipeRight
, swipeUp
, swipeDown
— 当元素被划过期触发。(可选择给定的方向)
咱们给元素添加事件处理,看看每一个触摸事件区别,代码以下(须要引用zepto类库)
<!DOCTYPE html> <html> <head> <title>iPhone.Zepto</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="format-detection" content="telephone=no" /> <script src="zepto.min.js"></script> <style type="text/css"> *{ margin: 0; padding: 0;} ul{ list-style: none;} .touch{ width:300px; margin:20px; background:#C96; border-radius:8px; height:40px; line-height:40px; text-align:center;} </style> </head> <body> <div class="touch touch1">点击一下触发tap</div> <div class="touch touch2">点击时间超过750ms触发longTap</div> <div class="touch touch3">滑动一下触发swipe</div> <div class="touch touch4">向左滑动触发swipeLeft</div> <div class="touch touch5">向右滑动触发swipeRight</div> <div class="touch touch6">向上滑动触发swipeUp</div> <div class="touch touch7">向下滑动触发swipeDown</div> </body> <script type="text/javascript"> Zepto(function($){ $(".touch1").tap(function(){ alert($(this).html()) }); $(".touch2").longTap(function(){ alert($(this).html()) }); $(".touch3").swipe(function(){ alert($(this).html()) }); $(".touch4").swipeLeft(function(){ alert($(this).html()) }); $(".touch5").swipeRight(function(){ alert($(this).html()) }); $(".touch6").swipeUp(function(){ alert($(this).html()) }); $(".touch7").swipeDown(function(){ alert($(this).html()) }); }); </script> </html> <!-- //点击事件 $("#rr").on('click', function(e){ var tempdeg=$("#aa").css('transform'); tempdeg=tempdeg.split('deg')[0].split('(')[1]; $("#aa").css('transform','rotate('+parseInt(tempdeg-90)+'deg)'); $("#aa").children('a').css('transform','rotate('+parseInt(-(tempdeg-90))+'deg)'); });-->
其余的事件我就不介绍了,jq咱们都已经了解到了,咱们利用zepto提供的触摸事件,作一些移动端网页经常使用的效果;
4.zepto.js利用触摸事件开发实例
实例1:具备滑动事件的tab切换
咱们了解到额tab切换,是在点击选项内容是,切换下面的内容,在这种功能的基础上,咱们对内容区进行向左滑动,和向右滑动,也能够切换内容区,而且选项卡自动获取焦点,zepto的tab切换代码以下:
<!DOCTYPE html> <html> <head> <title>iPhone.Zepto</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="format-detection" content="telephone=no" /> <script src="zepto.min.js"></script> <style type="text/css"> *{ margin: 0; padding: 0;} ul{ list-style: none;} /*demo*/ .tab1{height:400px; width:400px;} .tabnav{height:50px; line-height:50px;} .tabnav span{ cursor:pointer; margin:0 10px;} .tabnav .fou{ color:#36F;} .tabbox{height:350px; background:#FCC;} </style> </head> <body> <div class="tab1"> <div class="tabnav"> <span class="fou">111</span> <span>222</span> <span>333</span> </div> <div class="tabbox"> <div>111111</div> <div style="display:none;">22222222222</div> <div style="display:none;">33333333</div> </div> </div> </body> <script type="text/javascript"> Zepto(function($){ $(".tab1").find(".tabnav").children().click(function(){ $(".tab1").find(".tabbox").children().eq($(this).index()).show().siblings().hide(); $(this).addClass("fou").siblings().removeClass("fou"); }); }); </script> </html>
这是最基本的tab切换,咱们还要对内容区添加触摸事件,进行滑动切换的操做,代码以下
<!DOCTYPE html> <html> <head> <title>iPhone.Zepto</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="format-detection" content="telephone=no" /> <script src="zepto.min.js"></script> <style type="text/css"> *{ margin: 0; padding: 0;} ul{ list-style: none;} /*demo*/ .tab1{height:400px; width:300px;} .tabnav{height:50px; line-height:50px;} .tabnav span{ cursor:pointer; margin:0 10px;} .tabnav .fou{ color:#36F;} .tabbox{height:350px;} .tabbox div{ height:350px; background:#FCC;} </style> </head> <body> <div class="tab1"> <div class="tabnav"> <span class="fou">111</span> <span>222</span> <span>333</span> </div> <div class="tabbox"> <div>111111</div> <div style="display:none;">22222222222</div> <div style="display:none;">33333333</div> </div> </div> </body> <script type="text/javascript"> Zepto(function($){ var index=0; $(".tab1").find(".tabnav").children().click(function(){ $(".tab1").find(".tabbox").children().eq($(this).index()).show().siblings().hide(); $(this).addClass("fou").siblings().removeClass("fou"); index=$(this).index(); }); var len=$(".tab1").find(".tabnav").children().length-1; $(".tab1").find(".tabbox").children().swipeLeft(function(){ if(index>=len){ index=0; }else{ index=index+1; }; $(".tab1").find(".tabbox").children().eq(index).show().siblings().hide(); $(".tab1").find(".tabnav").children().eq(index).addClass("fou").siblings().removeClass("fou"); }); $(".tab1").find(".tabbox").children().swipeRight(function(){ if(index<=0){ index=len; }else{ index=index-1; }; $(".tab1").find(".tabbox").children().eq(index).show().siblings().hide(); $(".tab1").find(".tabnav").children().eq(index).addClass("fou").siblings().removeClass("fou"); }); }); </script> </html>
实例2:结合css3的变换旋转,作圆形导航
咱们见过这样的导航,一个圆形,按着指定的角度排列分开,截图以下
点击圆圈内容能够连接到新页面,单击中间的圆形区域,根据手势左右,进行转盘切换
点击左右按钮,也能够进行转盘切换
示例代码以下:
<!DOCTYPE html> <html> <head> <title>iPhone.Zepto</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="format-detection" content="telephone=no" /> <script src="zepto.min.js"></script> </head> <body> <nav style="position:relative; height:100px;"> <div id="aa" style=" width:200px; height:200px; margin-left:-100px; background:#06C; position:absolute; top:-100px;; left:50%; transform-origin:center center;transition:all .3s linear 0s;transform:rotate(0deg); border-radius:100px;"> <a style=" position:absolute; width:50px; height:50px; background:#9C0;border-radius:25px; margin-left:-25px;left:100px; top:-25px; text-align:center; line-height:50px;transition:all .3s linear 0s; transform-origin:center center;transform:rotate(0deg);">11</a> <a style=" position:absolute; width:50px; height:50px; background:#9C0;border-radius:25px; margin-left:-25px;left:200px; top:75px; text-align:center; line-height:50px;transition:all .3s linear 0s; transform-origin:center center;transform:rotate(0deg);">22</a> <a style=" position:absolute; width:50px; height:50px; background:#9C0;border-radius:25px; margin-left:-25px;left:100px; top:175px; text-align:center; line-height:50px;transition:all .3s linear 0s; transform-origin:center center;transform:rotate(0deg);">33</a> <a style=" position:absolute; width:50px; height:50px; background:#9C0;border-radius:25px; margin-left:-25px;left:0px; top:75px; text-align:center; line-height:50px;transition:all .3s linear 0s; transform-origin:center center;transform:rotate(0deg);">456</a> </div> </nav> <span id="ll" style=" display:inline-block; height:30px; line-height:30px; text-align:center; background:#0CF; border-radius:10px;">左动</span> <span id="rr" style=" display:inline-block; height:30px; line-height:30px; text-align:center; background:#0CF; border-radius:10px;">右动</span> <p>案例被手机支持,可用模拟器,点击按钮可旋转,在导航区左右滑动可旋转</p> </body> <script type="text/javascript"> Zepto(function($){ //手滑事件 $("#aa").swipeRight(function(){ var tempdeg=$(this).css('transform'); tempdeg=tempdeg.split('deg')[0].split('(')[1]; $(this).css('transform','rotate('+parseInt(tempdeg-90)+'deg)'); $(this).children('a').css('transform','rotate('+parseInt(-(tempdeg-90))+'deg)'); }); $("#aa").swipeLeft(function(){ var tempdeg=$("#aa").css('transform'); tempdeg=tempdeg.split('deg')[0].split('(')[1]; $("#aa").css('transform','rotate('+(parseInt(tempdeg)+90)+'deg)'); $("#aa").children('a').css('transform','rotate('+(-(parseInt(tempdeg)+90))+'deg)'); }); //点击事件 $("#rr").on('click', function(e){ var tempdeg=$("#aa").css('transform'); tempdeg=tempdeg.split('deg')[0].split('(')[1]; $("#aa").css('transform','rotate('+parseInt(tempdeg-90)+'deg)'); $("#aa").children('a').css('transform','rotate('+parseInt(-(tempdeg-90))+'deg)'); }); $("#ll").on('click', function(e){ var tempdeg=$("#aa").css('transform'); tempdeg=tempdeg.split('deg')[0].split('(')[1]; $("#aa").css('transform','rotate('+(parseInt(tempdeg)+90)+'deg)'); $("#aa").children('a').css('transform','rotate('+(-(parseInt(tempdeg)+90))+'deg)'); }); }); </script> </html>
实例3:触摸事件的焦点图实现
咱们已经作过带滑动的tab切换效果,其实效果很像了,咱们在加上动画处理就行了!!!!!!