我使用的是hammer.js库及其jQuery插件。我开始按照文档的建议使用它,所以类似于这样的东西可以在.game-card-mobile div上启动它。
/* Create Hammer object for swipable game cards */
var $gameCard = $('.game-card-mobile');
var $gameCardTouch = $gameCard.hammer();
$gameCardTouch.on("panright press panleft", function(ev) {
console.log(ev.type);
}); 这允许我将可用的操作限制在右、左和按下pan/swipe元素,但是在一个事件中,假设panright我将在控制台中打印出许多条目,尽管只执行了一个pan。于是我把印心改为:
$gameCardTouch.on("panend", function(ev) {
console.log(ev.type);
});现在它侦听在操作结束时发生的panend操作,因此在控制台中只返回一个打印输出,但是现在总是panend,因此我失去了对之前3个操作的限制,无法检测执行了什么特定操作。
有没有办法将这些组合起来,这样我就可以从panright中得到一个打印出来,如果用户向右滑动,panleft如果向左滑动并按下,只要他们完成该操作一次就可以了?
发布于 2016-03-13 18:44:23
正在使用主干、jQuery和Hammer解决类似的问题。下面是我解决这个问题的方法(只发布相关信息)。希望有人能找到有用的东西。
var Slideshow = Backbone.View.extend( {
events: {
"pan .slideshow-slide" : "handlePan",
"panstart .slideshow-slide" : "handlePanStart",
"panend .slideshow-slide" : "handlePanEnd",
},
state: {
panHistory: [],
},
handlePanStart: function( evt ) {
// attempt to get the pan x coord
var startX = this.getPanXFromEvent( evt );
// if an x coord couldn't be retrieved, get out
if ( !startX ) {
this.logger.warn( "Pan Start: Unable to find x", evt );
return;
}
this.logger.log( "Pan Start", evt );
// set the pan x array so this is its first and only element
this.state.panHistory = [ startX ];
},
handlePan: function( evt ) {
// cache the pan history
var pans = this.state.panHistory,
// get the x coord from this pan event
lastX = this.getPanXFromEvent( evt );
// track deltas on the x axis during the pan, so we know if the user
// is switching directions between pan start and pan end
if ( lastX ) {
pans.push( lastX );
}
},
handlePanEnd: function( evt ) {
// get the direction of the pan
switch ( this.getDirectionFromPanEvent( evt ) ) {
case Hammer.DIRECTION_LEFT:
// if we panned left and the next slide isn't out of
// range, go to the next slide.. otherwise fall back
// on the switch statement's default
if ( !this.isOutOfRange( this.state.current + 1 ) ) {
this.logger.log( "Pan End: Moving Left", evt );
this.gotoNextSlide();
return;
}
case Hammer.DIRECTION_RIGHT:
// if we panned right and the previous slide isn't out
// of range, go to the previous slide.. otherwise fall
// back on the switch statement's default
if ( !this.isOutOfRange( this.state.current - 1 ) ) {
this.logger.log( "Pan End: Moving Right", evt );
this.gotoPrevSlide();
return;
}
// Snap back to the current slide by default by calling
// gotoSlide on the current index to reset the transform
default:
this.logger.log( "Pan End: Snapping Back", evt );
this.gotoSlide( this.state.current );
}
},
getPanXFromEvent: function( evt ) {
return Utils.getValue( "originalEvent.gesture.center.x", evt );
},
getDirectionFromPanHistory: function( evt ) {
// placeholders for start, end, and last pan x coords
var i, start, last, end,
// cache the pan x array so we don't have to type it 50 times
pans = this.state.panHistory;
// if there aren't enough pans to calculate a delta, return 0
if ( pans.length < 2 ) {
return 0;
}
// get the starting pan x
start = pans[ 0 ];
// set last and end to the last pan x
last = end = pans[ pans.length - 1 ];
// loop backwards through the pans to find a pan x coord different from the ending one
// since there's a chance that identical pan x coords will be stacked in the array
for ( i = pans.length - 2; last === end && i >= 0; i-- ) {
last = pans[ i ];
}
// if the last pan was to the right, and we're farther right
// than we started, move right
return end > last && end > start ? Hammer.DIRECTION_RIGHT
// if the last pan was to the left, and we're farther left
// than we started, move left
: end < last && end < start ? Hammer.DIRECTION_LEFT
// otherwise move nowhere
: 0;
},
} );https://stackoverflow.com/questions/29333815
复制相似问题