我正在尝试上下滑动而不是左右移动
我有这个卷,如图所示:
我可以使用箭头图标(onClick())处理事件,但我想添加向上滑动事件, 添加滑动事件时,它在左侧工作 我希望它在图像显示时向下 有什么帮助吗?
答案 0 :(得分:51)
jQuery Mobile本身为我们提供了捕获swipeleft和swiperight的能力。然而,它并没有为我们提供swipeup和swipeown开箱即用。通过调整jQuery团队为swipeleft和swiperight所做的工作,我们能够以相同的方式创建和捕获这些事件。请参阅以下代码以实现swipeup和swipeown:
(function() {
var supportTouch = $.support.touch,
scrollEvent = "touchmove scroll",
touchStartEvent = supportTouch ? "touchstart" : "mousedown",
touchStopEvent = supportTouch ? "touchend" : "mouseup",
touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
$.event.special.swipeupdown = {
setup: function() {
var thisObject = this;
var $this = $(thisObject);
$this.bind(touchStartEvent, function(event) {
var data = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] :
event,
start = {
time: (new Date).getTime(),
coords: [ data.pageX, data.pageY ],
origin: $(event.target)
},
stop;
function moveHandler(event) {
if (!start) {
return;
}
var data = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] :
event;
stop = {
time: (new Date).getTime(),
coords: [ data.pageX, data.pageY ]
};
// prevent scrolling
if (Math.abs(start.coords[1] - stop.coords[1]) > 10) {
event.preventDefault();
}
}
$this
.bind(touchMoveEvent, moveHandler)
.one(touchStopEvent, function(event) {
$this.unbind(touchMoveEvent, moveHandler);
if (start && stop) {
if (stop.time - start.time < 1000 &&
Math.abs(start.coords[1] - stop.coords[1]) > 30 &&
Math.abs(start.coords[0] - stop.coords[0]) < 75) {
start.origin
.trigger("swipeupdown")
.trigger(start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown");
}
}
start = stop = undefined;
});
});
}
};
$.each({
swipedown: "swipeupdown",
swipeup: "swipeupdown"
}, function(event, sourceEvent){
$.event.special[event] = {
setup: function(){
$(this).bind(sourceEvent, $.noop);
}
};
});
})();
以下是Blackdynamo
的答案答案 1 :(得分:1)
我在这里接受了答案的问题,因为当滑动的原点和目标不相同时,没有检测到滑动。
这里可能是一个更简单的答案,我直接覆盖jquery handleSwipe事件(基于jquery.mobile-1.4.5)并使用垂直滑动附加它,上下调用:
(function( $, window, undefined ) {
//custom handleSwipe with swiperight, swipeleft, swipeup, swipedown
$.event.special.swipe.handleSwipe = function( start, stop, thisObject, origTarget ) {
if ( stop.time - start.time < $.event.special.swipe.durationThreshold ) {
var horSwipe = Math.abs( start.coords[0] - stop.coords[0] ) > $.event.special.swipe.horizontalDistanceThreshold;
var verSwipe = Math.abs( start.coords[1] - stop.coords[1] ) > $.event.special.swipe.verticalDistanceThreshold;
if( horSwipe != verSwipe ) {
var direction;
if(horSwipe)
direction = start.coords[0] > stop.coords[0] ? "swipeleft" : "swiperight";
else
direction = start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown";
$.event.trigger($.Event( "swipe", { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);
$.event.trigger($.Event( direction, { target: origTarget, swipestart: start, swipestop: stop }), undefined, thisObject);
return true;
}
return false;
}
return false;
}
//do binding
$.each({
swipeup: "swipe.up",
swipedown: "swipe.down"
}, function( event, sourceEvent ) {
$.event.special[ event ] = {
setup: function() {
$( this ).bind( sourceEvent, $.noop );
},
teardown: function() {
$( this ).unbind( sourceEvent );
}
};
});
})( jQuery, this );