I've used JQuery to make the elements in a table draggable. (I've never used JQuery before this). It works fine, but is very slow. For example, from the moment I click and hold on an item, to the moment the cursor changes is about 2 seconds. This is on Firefox 3.0.6. Once the item is being dragged, there's a shorter, but still noticeable delay (about half a second, I'd estimate) between when I let go of the mouse button and when the drop visibly takes place.
I suspect that the reason it's so slow is because the table is quite big (6 columns and about 100 rows), but it doesn't seem to me that it's obscenely big, and I wonder if there's something dumb I'm doing that is making it so slow. For example, I wonder if the JQuery code is being pointlessly applied to every element in the table each time you drag something. I don't know why that would happen, though.
In case it helps, here's my code (note that I've taken out the cursor call, as I was afraid it might be slowing things down).
<script type='text/javascript'>
$(document).ready
(
function()
{
$('.draggable_div').draggable
(
{
start: function(event, ui)
{
$(this).css('background-color','#ddddff');
}
}
);
$('.draggable_td').droppable
(
{
over: function(event, ui)
{
$(this).css('background-color', '#ccffcc');
},
out: function(event, ui)
{
$(this).css('background-color', null);
},
drop: function(event, ui)
{
// snip: removed code here to save space.
}
}
);
}
);
</script>
The HTML table is like this (as generated by PHP):
<table id='main_table' border=0>
<tr>
<th width=14.2857142857%>0</th>
<th width=14.2857142857%>1</th>
<th width=14.2857142857%>2</th>
<th width=14.2857142857%>3</th>
<th width=14.2857142857%>4</th>
<th width=14.2857142857%>5</th>
<th width=14.2857142857%>6</th>
</tr>
<tr>
<td class=draggable_td id='td:0:0:'>
<div class=draggable_div id='div:0:0:1962'>
content
</div>
</td>
<td class=draggable_td id='td:0:1:1962'>
<div class=draggable_div id='div:0:1:14482'>
content
</div>
</td>
<!-- snip: all the other cells removed for brevity -->
</tr>
<!-- snip: all the other rows removed for brevity -->
</table>
(Note: it doesn't seem to work at all in IE 7, so perhaps I'm just doing something very wrong here...)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…