JS三教九流系列-Zepto.js-为移动端开发的类库

zepto.js的api地址 http://www.css88.com/doc/zeptojs_api/ 

类库源码下载http://www.zeptojs.cn/ 滚动当前页面,看到这部分点击下载

090136_wTVG_2352644.png

和使用jquery一样,我们只要html页面引入即可。
zepto的api与jq几乎一样,同时各个接口名字也是一样的,我们只要按着写jq一样去写zepto就好了,既然这样,我们为何要用zepto,主要就是zepto提供时触摸事件,为开发移动端的更好处理。

jq是为多个浏览器支持的类库,并没有封装上touch事件,如果我们的项目是pc或者虽然是移动端,但是没有很多触摸事件的效果,我们选择jq即可,我们可以看出,我们采用zepto的时候

手机项目的开发,需要触摸事件。

zepto的支持是高级浏览器,ie10以上。


1.zepto.js的helloworld输出

我们学习之前,需要保证对jq有很熟练的使用技巧,我们使用zepto,基本的调用如下

<!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($){

  //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触发。

swipeswipeLeftswipeRightswipeUpswipeDown — 当元素被划过时触发。(可选择给定的方向)

我们给元素添加事件处理,看看每个触摸事件区别,代码如下(需要引用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的变换旋转,做圆形导航

我们见过这样的导航,一个圆形,按着指定的角度排列分开,截图如下

093635_gle9_2352644.png

点击圆圈内容可以链接到新页面,单击中间的圆形区域,根据手势左右,进行转盘切换

点击左右按钮,也可以进行转盘切换

示例代码如下:

<!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切换效果,其实效果很像了,我们在加上动画处理就好了!!!!!!

 

 



转载于:https://my.oschina.net/tbd/blog/493354

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值