Jquery:优化 MouseOver 上的 Droppable
我正在使用 Jquery 的 Draggable/Droppable 功能来允许从 TreeView 拖放到简单的 HTML 表,但我发现随着表中单元格数量的增加,Droppable 性能变得非常缓慢。
我环顾四周,人们建议的最常见的解决方案是限制活动可拖动和可放置的数量。现在,限制可拖动对象已经足够简单了(使用树视图节点上的鼠标悬停来启用拖动)。
然后我尝试对可放置的单元格执行相同的操作(即,仅当用户将鼠标悬停在单元格上时才使单元格可放置),但遇到了计时问题。
基本上发生的情况是,用户将节点拖动到单元格上并且无法删除它。但当我尝试第二次时,它起作用了!换句话说,鼠标悬停事件需要在其工作之前完成,但“完成”意味着停止您的第一次拖放,这显然远非理想。
这是我正在使用的代码:
<table id="taskTable">
<tbody>
<tr>
<td class="taskTableHourCell">6:00</td>
<td class='aCell'></td> <!-- this is the cell to be dragged on, is actually created dynamically, see below -->
</tr>
</tbody>
</table>
<script type="text/javascript">
function addCell()
{
var newCell = $("<td class='aCell'></td>");
newCell.mouseover(function(){$(this).droppable({ drop: onDrop, hoverClass: "ui-state-active", addClasses: false });});
// Add the cell to the table, code not really relevant here
}
</script>
显然,对于动态添加到表中的每个新单元格(在页面加载或表调整大小时)都会调用 addCell() 。
有办法解决这个问题吗?如果 Javascript 有类似 'beginmouseover' 事件之类的东西,这会容易得多......
I'm using the Draggable/Droppable functionality of Jquery to allow drag-and-drop from a TreeView to a simple HTML table, but am finding that Droppable performance gets very sluggish as the number of cells increase in the table.
I've looked around and the most-common solution people suggest is limiting the number of active draggables and droppables. Now, limiting the draggables was simple enough (use the mouseover of the treeview node to enable dragging).
Then I tried to do the same thing for a droppable (ie, only make a cell Droppable when the user mouseovers it), but have hit a timing problem.
Basically what happens is, the user drags a node over the cell and can't drop it. But when I then try to do it a second time, it works! In other words, the mouseover event needs to complete before it works, but 'completing it' means stopping your first drag-and-drop which is obviously far from ideal.
This is the code that I'm using:
<table id="taskTable">
<tbody>
<tr>
<td class="taskTableHourCell">6:00</td>
<td class='aCell'></td> <!-- this is the cell to be dragged on, is actually created dynamically, see below -->
</tr>
</tbody>
</table>
<script type="text/javascript">
function addCell()
{
var newCell = $("<td class='aCell'></td>");
newCell.mouseover(function(){$(this).droppable({ drop: onDrop, hoverClass: "ui-state-active", addClasses: false });});
// Add the cell to the table, code not really relevant here
}
</script>
and obviously addCell() is called for each new cell that is added to the table dynamically (on page load, or on table resize).
Is there a way around this? This would be much easier if Javascript has something like a 'beginmouseover' event...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我通过简单地使 TABLE 可放置(而不是单个单元格)然后计算放置处理程序中的放置位置来避免这种情况。
In the end, I managed to avoid it by simply making the TABLE droppable (instead of the individual cells) and then figuring out the dropposition in the drop handler.