3
我已经为jQuery写了一个模拟移动事件的插件,但也支持标准Web浏览器。下面是swipeleft
和swiperight
事件标记:移动Safari中的自定义“滑动”事件没有触发
(function($) {
var settings = {
swipe_h_threshold : 30,
swipe_v_threshold : 50,
taphold_threshold : 750,
startevent : ('ontouchstart' in document.documentElement) ? 'touchstart' : 'mousedown',
endevent : ('ontouchstart' in document.documentElement) ? 'touchend' : 'mouseup'
};
// swipeleft Event:
$.fn.swipeleft = function(handler) {
return this.each(function() {
$this = $(this);
var start_x = 0;
var end_x = 0;
$this.bind(settings.startevent, function(e) {
e.stopPropagation();
e.preventDefault();
start_x = e.pageX;
$this.one(settings.endevent, function(e) {
end_x = e.pageX;
if(start_x > end_x && (start_x - end_x >= settings.swipe_h_threshold))
{
handler.call(this);
}
});
});
});
};
// swiperight Event:
$.fn.swiperight = function(handler) {
return this.each(function() {
var $this = $(this);
var start_x = 0;
var end_x = 0;
$this.bind(settings.startevent, function(e) {
e.preventDefault();
e.stopPropagation();
start_x = e.pageX;
$this.one(settings.endevent, function(e) {
end_x = e.pageX;
if(start_x < end_x && (end_x - start_x >= settings.swipe_h_threshold))
{
handler.call(this);
}
});
});
});
};
}) (jQuery);
我然后使用以下叫事件。
$( '#my_div')swiperight(函数(){self.nextCard( 'r');}); $('#my_div')。swipeleft(function(){self.nextCard('r');});
这似乎在桌面浏览器上正常工作(好吧,Chrome无论如何)>http://ben-major.co.uk/labs/carousel.html,但在Mobile Safari中似乎不起作用。 swipeleft
执行没有问题,但swiperight
将不会运行任何。
任何人都可以提供任何指针?
但是怎么来的'swipeleft'会不会有问题的功能? – BenM