由网友(关系淡了,加点盐)分享简介:你怎么能检测到用户在一个网页的JavaScript?How can you detect that a user swiped his finger in some direction over a web page with JavaScript?我在想,如果有,将工作的同时在iPhone和Android手机网站...
你怎么能检测到用户在一个网页的JavaScript?
How can you detect that a user swiped his finger in some direction over a web page with JavaScript?
我在想,如果有,将工作的同时在iPhone和Android手机网站的一个解决方案。
I was wondering if there was one solution that would work for websites on both the iPhone and an Android phone.
推荐答案
简单香草JS code样品:
Simple vanilla JS code sample:
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
var xDown = null;
var yDown = null;
function handleTouchStart(evt) {
xDown = evt.touches[0].clientX;
yDown = evt.touches[0].clientY;
};
function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
/* left swipe */
} else {
/* right swipe */
}
} else {
if ( yDiff > 0 ) {
/* up swipe */
} else {
/* down swipe */
}
}
/* reset values */
xDown = null;
yDown = null;
};
经过测试,在Android的。
Tested in Android.
相关推荐
最新文章