javascript - 添加向左/向右滑动到网页,但使用默认向上/向下滑动

标签 javascript html ios swipe

我正在使用 padilicious 来检测将在 iOS 和桌面上查看的网页的滑动手势。向左/向右滑动我网站的上一页和下一页效果很好。但是,向上/向下滑动时,它似乎覆盖了 iPhone/iPad 中的默认行为。我想要向上/向下滑动来滚动页面,当我没有恶意运行时它会这样做。只是让代码忽略向上/向下滑动似乎不起作用。

我经历过的恶作剧代码段

function processingRoutine() {
    var swipedElement = document.getElementById(triggerElementID);
    if ( swipeDirection == 'left' ) {
        document.location = document.getElementById('nextPage').href;
    } else if ( swipeDirection == 'right' ) {
        document.location = document.getElementById('prevPage').href;
    } else if ( swipeDirection == 'up' ) {
        return;
    } else if ( swipeDirection == 'down' ) {
        return;
    }


}

最佳答案

从所有函数中删除 event.preventDefault();。在函数 processingRoutine() {} 中添加 event.preventDefault(); 以获得所需内容。

function processingRoutine() {
    var swipedElement = document.getElementById(triggerElementID);
    if ( swipeDirection == 'left' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'orange';
        event.preventDefault();
    } else if ( swipeDirection == 'right' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'green';
        event.preventDefault();
    } else if ( swipeDirection == 'up' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'maroon';
    } else if ( swipeDirection == 'down' ) {
        // REPLACE WITH YOUR ROUTINES
        //swipedElement.style.backgroundColor = 'purple';
    }
}

关于javascript - 添加向左/向右滑动到网页,但使用默认向上/向下滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5304340/

相关文章:

ios - 我的 Sprite 节点不会出现在我的场景中

javascript - 在弹出窗口而不是 DIV 中打开一个新页面

javascript - firefox 17 中的 UniversalXPConnect 权限错误

javascript - Javascript 中的 `TimeEvent` 对象是什么?它有什么方法和属性?

javascript - 一个元素中可以水平和垂直放置多少个等宽字符?

ios - 水平滚动 UICollectionView 时对齐单元格的中心

ios - 如何从模态 XIB 设置 VC 的背景颜色

javascript - 在 $.getJSON 中发送

javascript - 如何创建 svg sprite 然后获取每个 svg 的坐标?

html - 选择具有行跨度的表的第一列