Javascript 在没有外部库的情况下检测刷卡事件?
有没有人有检测刷击的代码,而不是纯jQuery中的点击,没有jQuery Mobile或外部库 我已经看过了,但是里面有很多额外的代码。我真正需要的是一个布尔值——是一个滑动或点击事件——而不是方向、位置等Javascript 在没有外部库的情况下检测刷卡事件?,javascript,jquery,Javascript,Jquery,有没有人有检测刷击的代码,而不是纯jQuery中的点击,没有jQuery Mobile或外部库 我已经看过了,但是里面有很多额外的代码。我真正需要的是一个布尔值——是一个滑动或点击事件——而不是方向、位置等 我无法在我的项目中使用外部库,我认为这将是一段非常有用的简单代码,用于堆栈溢出 这是一个非常基本的问题。我有一个依赖项,它是一个插件,但它的开销没有我认为的TouchSwipe那么大 这个不太长,你可以把不需要的东西拿出来 /* * jSwipe - jQuery Plugin * ht
我无法在我的项目中使用外部库,我认为这将是一段非常有用的简单代码,用于堆栈溢出 这是一个非常基本的问题。我有一个依赖项,它是一个插件,但它的开销没有我认为的TouchSwipe那么大
这个不太长,你可以把不需要的东西拿出来
/*
* jSwipe - jQuery Plugin
* http://plugins.jquery.com/project/swipe
* http://www.ryanscherf.com/demos/swipe/
*
* Copyright (c) 2009 Ryan Scherf (www.ryanscherf.com)
* Licensed under the MIT license
*
* $Date: 2009-07-14 (Tue, 14 Jul 2009) $
* $version: 0.1.2
*
* This jQuery plugin will only run on devices running Mobile Safari
* on iPhone or iPod Touch devices running iPhone OS 2.0 or later.
* http://developer.apple.com/iphone/library/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW5
*/
(function($) {
$.fn.swipe = function(options) {
// Default thresholds & swipe functions
var defaults = {
threshold: {
x: 30,
y: 10
},
swipeLeft: function() { alert('swiped left') },
swipeRight: function() { alert('swiped right') }
};
var options = $.extend(defaults, options);
if (!this) return false;
return this.each(function() {
var me = $(this)
// Private variables for each element
var originalCoord = { x: 0, y: 0 }
var finalCoord = { x: 0, y: 0 }
// Screen touched, store the original coordinate
function touchStart(event) {
//console.log('Starting swipe gesture...')
originalCoord.x = event.targetTouches[0].pageX
originalCoord.y = event.targetTouches[0].pageY
}
// Store coordinates as finger is swiping
function touchMove(event) {
event.preventDefault();
finalCoord.x = event.targetTouches[0].pageX // Updated X,Y coordinates
finalCoord.y = event.targetTouches[0].pageY
}
// Done Swiping
// Swipe should only be on X axis, ignore if swipe on Y axis
// Calculate if the swipe was left or right
function touchEnd(event) {
//console.log('Ending swipe gesture...')
var changeY = originalCoord.y - finalCoord.y
if(changeY < defaults.threshold.y && changeY > (defaults.threshold.y*-1)) {
changeX = originalCoord.x - finalCoord.x
if(changeX > defaults.threshold.x) {
defaults.swipeLeft()
}
if(changeX < (defaults.threshold.x*-1)) {
defaults.swipeRight()
}
}
}
// Swipe was started
function touchStart(event) {
//console.log('Starting swipe gesture...')
originalCoord.x = event.targetTouches[0].pageX
originalCoord.y = event.targetTouches[0].pageY
finalCoord.x = originalCoord.x
finalCoord.y = originalCoord.y
}
// Swipe was canceled
function touchCancel(event) {
//console.log('Canceling swipe gesture...')
}
// Add gestures to all swipable areas
this.addEventListener("touchstart", touchStart, false);
this.addEventListener("touchmove", touchMove, false);
this.addEventListener("touchend", touchEnd, false);
this.addEventListener("touchcancel", touchCancel, false);
});
};
})(jQuery);
/*
*jSwipe-jQuery插件
* http://plugins.jquery.com/project/swipe
* http://www.ryanscherf.com/demos/swipe/
*
*版权所有(c)2009 Ryan Scherf(www.ryanscherf.com)
*根据麻省理工学院许可证获得许可
*
*日期:2009-07-14(2009年7月14日星期二)$
*$version:0.1.2
*
*此jQuery插件将仅在运行Mobile Safari的设备上运行
*在运行iPhone OS 2.0或更高版本的iPhone或iPod Touch设备上。
* http://developer.apple.com/iphone/library/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW5
*/
(函数($){
$.fn.swipe=功能(选项){
//默认阈值和滑动功能
var默认值={
阈值:{
x:30,
y:10
},
swipeLeft:function(){alert('swiped left')},
SwiperRight:function(){alert('swiped right')}
};
var options=$.extend(默认值,选项);
如果(!this)返回false;
返回此值。每个(函数(){
var me=$(这个)
//每个元素的私有变量
var originalcord={x:0,y:0}
var finalcord={x:0,y:0}
//触摸屏幕,存储原始坐标
功能触摸启动(事件){
//console.log('开始滑动手势…')
OriginalCord.x=event.targetTouches[0]。pageX
originalcord.y=event.targetTouches[0].pageY
}
//在手指滑动时存储坐标
功能触摸移动(事件){
event.preventDefault();
finalcord.x=event.targetTouches[0]。pageX//更新了x,Y坐标
finalcord.y=event.targetTouches[0]。pageY
}
//刷卡完毕
//只能在X轴上滑动,如果在Y轴上滑动则忽略
//计算刷卡是向左还是向右
函数touchEnd(事件){
//log('结束滑动手势…')
var changeY=原始订单y-最终订单y
if(changeY(defaults.threshold.y*-1)){
changeX=原始订单.x-最终订单.x
如果(changeX>defaults.threshold.x){
defaults.swipleft()
}
如果(changeX<(默认值阈值x*-1)){
默认值
}
}
}
//开始刷卡
功能触摸启动(事件){
//console.log('开始滑动手势…')
OriginalCord.x=event.targetTouches[0]。pageX
originalcord.y=event.targetTouches[0].pageY
finalcord.x=originalcord.x
最终订单y=原始订单y
}
//刷卡被取消了
函数touchCancel(事件){
//log('取消滑动手势…')
}
//向所有可滑动区域添加手势
此.addEventListener(“touchstart”,touchstart,false);
此.addEventListener(“touchmove”,touchmove,false);
此.addEventListener(“touchend”,touchend,false);
此.addEventListener(“touchcancel”,touchcancel,false);
});
};
})(jQuery);
为什么不做如下操作:与以前的跳线相比,监听mousedown
事件->获取跳线->监听mouseup
->。通过这种方式,您可以区分“点击”和“滑动”,您始终可以抓取一个简单的库并删除覆盖head@Johan-我想这是最好的办法。当我写代码时,我会把它作为答案贴在这里。它依赖于jquery.event.move。