您需要检查“平移”事件中的 X 或 Y 位置,如果它小于“向下”上的 X 或 Y 位置,则返回 false,如下所示
$('#your_reel_image').on('down', function(e, x, y, ev){
$(this).data("lastPosition",{"x":x,"y":y});//store the position on mousedown
}).on('pan', function(e,x,y,ev){//event when you do a mousemove while the mousedown
var $reel=$(this);
var delta=$reel.data("lastPosition").x-x;//difference between current and last position
if(delta<0){
$reel.data("lastPosition",{"x":x,"y":y});//update position
$reel.data("direction","right");//set current direction to right
}
else if(delta>0){
$reel.data("direction","left");//set current direction to left
return false;//prevent the reverse animation
}
});
我还在fractionChange中添加了一个函数,以防止仅将光标向左移动时会发生1帧反向动画
$('#your_reel_image').on('fractionChange',function(){
if($(this).data("direction")=="left"){//if the current direction is left
$(this).data("direction","")//clear the current direction
return false;//return false prevent the 1 frame animation
//without the last line you can return 1 frame at a time on mouseup
}
});
如果要防止鼠标滚轮上的反向动画,则需要添加此功能
$('#your_reel_image').on('wheel',function(e, distance, ev){
var $reel=$(this);
//almost the same as in the pan function without storing the position
if(distance<0){
$reel.data("direction","right");
}
else if(distance>0){
$reel.data("direction","left");
return false;
}
});
http://jsfiddle.net/N68qa/1/