通过拖放触摸设备启用滚动

时间:2014-11-13 10:20:45

标签: javascript android jquery ios css

table with horizontal scroll on touch devices

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

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

有没有css方法正常滚动???如果不是,我如何使用javascriptjquery ???

实现它

更新 -webkit-overflow-scrolling:touch;没有用。

更新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也停止了工作,修复了标题。

1 个答案:

答案 0 :(得分:0)

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

  1. 您希望能够在触控设备上水平滚动表格。这现在可行。

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

  3. 如果我对上述两点有所了解,那么我认为可能解决问题的方法是更改​​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);
        });
    }
    

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

    我不确定这会100%有效 - 如果您可以在http://jsfiddle.net/等地方发布问题的重复,那么可能更容易帮助您进行调试。