简体   繁体   English

在移动网络应用程序上“绘制”html表`td`元素

[英]“Paint” html table `td` elements on mobile web app

I'm working on a web application where the user needs to look at a grid of months and select whether documentation exists for a list of objects. 我正在开发一个Web应用程序,用户需要查看几个月的网格,并选择是否存在对象列表的文档。

For the sake of user experience, I want to provide the ease of "painting" the grid. 为了用户体验,我想提供“绘画”网格的便利性。 So say if the user knows the documentation exists for the whole year, she can just drag her finger across that row and every <td> in that row will have a class applied to it. 因此,如果用户知道整年存在文档,她可以将手指拖过该行,并且该行中的每个<td>都将应用一个类。

From what I can tell from Chrome Dev tools, I can get a lot of events firing when I attach a touchmove listener to each <td> , but the event's target is always the element the touch event started on and never on any other element. 从我从Chrome Dev工具中可以看出,当我将touchmove侦听器附加到每个<td>时,我可以触发很多事件,但事件的目标始终是触摸事件开始的元素,而不是任何其他元素。

Is there any way to have each <td> respond when a user drags his finger over a table? 当用户将手指拖到桌子上时,有没有办法让每个<td>响应?

Unfortunately the event target is set on the touchstart event so, as you drag your finger across the screen, the same event target is used as the origin of each touchmove event. 不幸的是,事件目标是在touchstart事件上设置的,因此,当您在屏幕上拖动手指时,相同的事件目标将用作每个touchmove事件的原点。

The only way around this I can think of would be to use the touchmove event to obtain the position of the touch and then figure out which table cell that would coincide with. 我能想到的唯一方法是使用touchmove事件来获取触摸的位置,然后找出与之重合的表格单元格。 If all your table cells are the same dimensions within a given table, taking the position obtained from the touchmove event and dividing it by the width and height of the table would give you the answer. 如果所有表格单元格在给定表格中的尺寸相同,则从touchmove事件获取的位置除以表格的宽度和高度将得到答案。 Here's a quick snippet to get you started (this example assumes the table's position is at the very top left of the viewport): 这是一个快速启动代码段(此示例假设表的位置位于视口的最左上方):

var _columnWidth = 40;
var _rowHeight = 20;
var touchListener = function (e) {
    var touch = e.touches[0];
    var x = touch.pageX;
    var y = touch.pageY;
    var xIndex = Math.floor(_someTable.width / x / _columnWidth);
    var yIndex  = Math.floor(_someTable.height / y / _rowHeight);
};

_someTable.addEventListener('touchmove', touchListener, false);

So, if the width of the table is 80 and the height of the table is 100, and the x and y coordinates is 42 and 67 respectively, then xIndex would yield 1 and yIndex would yield 3 (meaning the second cell from the left and the forth cell from the top using zero-based indices). 因此,如果表的宽度为80并且表的高度为100,并且x和y坐标分别为42和67,则xIndex将产生1,yIndex将产生3(意味着左边的第二个单元格,使用从零开始的索引从顶部开始的第四个单元格。

Now this is a light example and won't magically solve your problem, but it's a good place to start. 现在这是一个很好的例子,不会神奇地解决你的问题,但它是一个很好的起点。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM