我使用jQuery和touchSwipe插件向上和向下滾動內容。我有一些數學相關的問題,想看看社區是否能夠迅速幫助我弄清楚這一點。jQuery touchSwipe內容滾動功能
touchSwipe「distance」變量只返回從零開始的正值,所以當用戶向上滑動然後決定向下滑動時會出現問題,因爲距離值從ex改變:(0 initial,15up, 32up,20up,5up,10down,40down)。我創建了4條if語句來捕捉所有這些情況。如果陳述如何處理「方向改變」?
touchSwipe:http://labs.skinkers.com/touchSwipe/
的向上和向下滑動功能:
var _scrolling=false;
var _prev=0;
$("#list").swipe({
swipeStatus:function(event, phase, direction, distance, fingerCount) {
if(phase=="end"){return false;}
if(phase=="move" && !_scrolling) {
var t = parseInt($("#list").css("top").split('px')[0]);
var s = 0;
if(direction=="up" && (distance-_prev) > 0){
//still moving up
s = t-(distance-_prev);
_prev=distance;
} else if(direction=="up" && (distance-_prev) < 0){
//direction change - moving down
} else if(direction=="down" && (distance-_prev) > 0){
//still moving down
s = t+(distance-_prev);
_prev=distance;
} else if(direction=="down" && (distance-_prev) < 0){
//direction change - moving up
}
_scrolling=true;
$("#list").animate({ top:s }, 70, function() {_scrolling=false;});
}<!--if end-->
}<!--swipeStatus end-->
});