javascript - 在触摸设备上通过拖放启用滚动

标签 javascript android jquery ios css

table with horizontal scroll on touch devices

重新排列列和点击现在可以在触摸设备上使用。现在面临滚动问题。我试图用 iScroll 插件解决它,但它没有用。我从 chrome 浏览器的设备模式截取的屏幕截图。

表格列可以即时添加,因此列数可能会有所不同。

是否有任何 css 方法可以正常滚动???如果不是,我如何使用 javascriptjquery 实现它???

更新: -webkit-溢出滚动:触摸;不工作。

更新 2: 尝试使用以下代码:

if (Modernizr.touch) {
            $('.container-fluid').css('overflow', 'auto');            
        }

还有这个:

 if (Modernizr.touch) {            
              //iScroll plugin                
              var myScroll = new IScroll('#tblGrid', {               
                    scrollbars: true
                });
            }

它们都不起作用。

更新 3:

下面是启用表格列拖动和点击事件的代码:

var clickms = 200;
    var lastTouchDown = -1;

    function touchHandler(event) {
        var touch = event.changedTouches[0];

        var d = new Date(); var type = "";
        switch (event.type) {
            case "touchstart": type = "mousedown"; lastTouchDown = d.getTime(); break;
            case "touchmove": type = "mousemove"; lastTouchDown = -1; break;
            case "touchend": if (lastTouchDown > -1 && (d.getTime() - lastTouchDown) < clickms) { lastTouchDown = -1; type = "click"; break; } type = "mouseup"; break;
            default: return;
        }

        var simulatedEvent = document.createEvent("MouseEvent");
        simulatedEvent.initMouseEvent(type, true, true, window, 1,
        touch.screenX, touch.screenY,
        touch.clientX, touch.clientY, false,
        false, false, false, 0, null);

        touch.target.dispatchEvent(simulatedEvent);
        event.preventDefault();
    }

function init() {
        document.addEventListener("touchstart", touchHandler, true);
        document.addEventListener("touchmove", touchHandler, true);
        document.addEventListener("touchend", touchHandler, true);
        document.addEventListener("touchcancel", touchHandler, true);
    }
$(document).ready(function () { 

init();

        var myScroll;
        function loaded() {
            myScroll = new IScroll('#tblGrid', {
                mouseWheel: true,
                scrollbars: true,
                click: true,
                eventPassthrough: true,
                tap: true
            });
        }
        if (Modernizr.touch) {            
            loaded();
        }

});

更新 4:

我尝试使用 iScroll 4,现在可以滚动了。但是,当我重新排列/拖放列时,滚动也有效,在这种情况下,由于 touchmove 事件,拖放无法正常工作。

并且 jquery.floatThead 也停止了修复 header 的工作。

最佳答案

我不完全确定你的最终目标是什么,但让我看看我是否理解:

  1. 您希望能够在触摸设备上水平滚动表格。这现在有效。

  2. 您希望能够拖放列以重新排列它们。您希望通过拖动列标题来完成此操作。现在,当您执行此操作时,touchmove 监听器会在您拖动列时导致整个表格水平滚动,这是一个问题。

如果我在上述两点上是正确的,那么我认为可能解决您的问题的方法是更改​​ init() 以便它只添加触摸监听器到您的表格标题(而不是整个文档)。像这样:

function init() {
    $( "th" ).each(function( index ) {
        this.addEventListener("touchstart", touchHandler, true);
        this.addEventListener("touchmove", touchHandler, true);
        this.addEventListener("touchend", touchHandler, true);
        this.addEventListener("touchcancel", touchHandler, true);
    });
}

您还需要将四个事件监听器应用于添加到表中的任何新列标题(无论您当前在何处处理“添加列”逻辑)。

我不确定这是否会 100% 有效 - 如果您可以在类似 http://jsfiddle.net/ 的地方发布问题的重现,可能更容易帮助您调试它。

关于javascript - 在触摸设备上通过拖放启用滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26906479/

相关文章:

javascript - 带有两个参数的 JS 函数,引用不同的对象

javascript - 通过 iframe 嵌入 Facebook 帖子不起作用

android - 如何在 Activity 中使用 navGraphViewModels(不在 Fragment 中)

android - 如何在抖动中保存扫描的QR码图像?

jquery - 你如何保持你的 jQuery 插件更新?

javascript - 如何替换jquery mobile中的li?

javascript - 如何让所有错误都被指出而不是一一指出

android - 如何直接使用适配器从 AutoCompleteTextView 中删除数据

javascript - 分解为更小的功能 MVC

asp.net - jQuery 帮助 - 如何仅选择包装集中的第一项?