zepto 的touch 、event

一.touch

1.on()  绑定事件处理程序;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>同jquery类似事件</title>
	<style type="text/css">
		.btn2{
			width: 100px;
			height: 100px;
			background: #f0f;
		}

	</style>
</head>
<body>		
	<button class="btn1">按钮1</button>
	<div class="btn2">按钮2</div>
	<button class="btn3">按钮3</button>

<script src="../js/zepto.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/touch.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	$(function () {
		$('.btn1').on('touchstart',function () {
			console.log("我是按钮1输出")
		});

	})
</script>
</body>
</html>

2.off() 方法移除用目标元素on绑定的事件处理程序。

	$(function () {
		$('.btn1').on('touchstart',function () {
			console.log("我是按钮1输出")
		});
// 移除btn1绑定的touchstart事件 ,点击btn1没有stouchstart事件
		$('.btn1').off('touchstart')

	})

3.bind() 为每个匹配元素的特定事件绑定事件处理函数,可同时绑定多个事件,也可以自定义事件。官网不体检 ,用on代替


		$('.btn2').bind('touchstart touchmove',function(){
			$(this).toggleClass("btnSty")
		});

4.one() 为每一个匹配元素的特定事件(像click)绑定一个一次性的事件处理函数。只执行一次。

$('.btn2').one('click',function () {
			console.log('我只输出一次')
		})

5.trigger() 触发有bind定义的事件(通常是自定义事件)

页面加载到$('.btn3').trigger()自动触发有bind定义的事件,

$(function () {
		
		$('.btn3').on('myClick', function (event, a, b) {
			console.log(a+' '+b)
		})

		$('.btn3').trigger('myClick', ['hah', 'lala'])

	})

6  unbind() bind的反向操作,删除匹配元素所绑定的bind事件。

$('#btn2').unbind('touchmove');

上面几个方法同jQuery类似,下面讲几个zepto touch方法

1、tap()点击事件 ,利用在document上绑定touch事件来模拟tap事件的,并且tap事件会冒泡到document上

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>zepto_touch</title>
		<style type="text/css">
      
			.box{
				 width: 100px;
				 height: 100px;
				 background: #FFFF00;
				 margin: 0 auto;
				 text-align: center;
				 line-height: 100px;
			 }
			.btn{
				width: 100px;
				height: 50px;
				background: #FF0000;
				border-radius: 10px;
				margin-left:130px;
			}
		</style>
	</head>
	<body style="height: 1000px;">

		<div id="box1" class="box">按钮1</div><br />
		<div id="box2" class="box">按钮2</div><br />
		<div id="box3" class="box">按钮3</div><br />
		<button id="btn" class="btn">button</button>
		<script src="../js/zepto.js" type="text/javascript" charset="utf-8"></script>
		<script src="../js/touch.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
	        		
	    $(function () {
		        $('#box1').tap(function () {
		        	console.log('我是tap事件')//我是tap事件
		        })
	        })
		</script>
	</body>
</html>

2 、singleTap() 点击事件

	$('#box2').singleTap(function () {
			console.log('singTap:我被单击了')//singTap:我被单击了
		})

3 doubleTap() 双击事件

	$('#box3').doubleTap(function () {
			console.log('doubleTap:我被双击了')//doubleTap:我被双击了
		})

4 longTap() 当一个元素被按住超过750ms触发。

$('#btn').longTap(function () {
			console.log('longTap:谁长时间按我')//longTap:谁长时间按我
		})

5  swipe, swipeLeft, swipeRight, swipeUp, swipeDown — 当元素被划过(同一个方向大于30px)时触发。(可选择给定的方向) 在一个方向滑动大于30px即为滑动。否则算点击。

$('#btn').swipeLeft(function () {
                    alert('我向左滑动了');
                });

二、事件处理机制

zepto有自己的一套事件机制,并且对不同的浏览器做了兼容的内部封装处理。
1、像新版本的zepto中已经舍弃了bind,delegate,die,同样jquery中舍弃了live等。
2、现在统一使用on,off标准事件来绑定事件。

$(function(){
				//简单绑定
				$('#box').on('touchstart',function(){
					alert('ddd');
				});
				//事件委托
				$('#box1').on('touchstart','p',function(){
					alert($(this).text());
				});
				//解除绑定事件
				//$('#box').off('touchstart');
				
				//一次函数
				$('#box2').one('touchstart',function(){
					alert('我就执行一次');
				});
			})

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值