c# – 如何保存拖动项目的位置

前端之家收集整理的这篇文章主要介绍了c# – 如何保存拖动项目的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
HI,
我想把删除的项目的位置保存到数据库[aspx,javascript].用户可以删除任意数量的项目,调整大小并删除[隐藏],最后当他们点击“保存”按钮时,应将其保存到数据库.我有代码在drop / stop中执行此操作,但它会保存所有丢弃的项目,我只想在最后阶段保存.
我想很多开发人员应该已经做到了,所以请建议一些代码.
  1. $(function() {
  2. $('#frame img').live('mousemove',function(event) {
  3. $('#frame img').resizable().parent().draggable();
  4. });
  5. });
  6.  
  7.  
  8. $(function() {
  9. $('#frame img').live('dblclick',function(event) {
  10. // $(this).resizable("destroy") not working
  11. $(this).hide();
  12. //$(this).unbind("resizable"); not working
  13. //$(this).removeclass(); not working
  14. });
  15. });
  16.  
  17. $(document).ready(function() {
  18. //Counter
  19. counter = 0;
  20.  
  21. //Make element draggable
  22. $("img").draggable({
  23. helper: 'clone',containment: '#frame',//When first dragged
  24. stop: function(ev,ui) {
  25. var pos = $(ui.helper).offset();
  26. objName = "#clonediv" + counter
  27. $(objName).css({ "left": pos.left,"top": pos.top });
  28.  
  29. $(objName).removeClass("drag");
  30. //When an existiung object is dragged
  31. $(objName).draggable({
  32. containment: 'parent',stop: function(ev,ui) {
  33. var pos = $(ui.helper).offset();
  34. }
  35. });
  36. }
  37. });
  38.  
  39. //Make element droppable
  40. $("#frame").droppable({
  41.  
  42. drop: function(ev,ui) {
  43.  
  44. if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {
  45. var pos = $(ui.helper).offset();
  46.  
  47. counter++;
  48. var element = $(ui.helper).clone();
  49.  
  50. //var element = element1.resizable();
  51. element.addClass("tempclass");
  52. $(this).append(element);
  53. $(".tempclass").attr("id","clonediv" + counter);
  54. //$(".tempclass").attr("onclick",function(){ $(this).remove(););
  55. $("#clonediv" + counter).removeClass("tempclass");
  56. //Get the dynamically item id
  57. draggedNumber = ui.helper.attr('id').search(/drag([0-9])/)
  58. itemDragged = "dragged" + RegExp.$1
  59. //console.log(itemDragged)
  60. //alert('left' + pos.left + ',top' + pos.top + 'of item' + itemDragged);
  61. $("#clonediv" + counter).addClass(itemDragged);
  62. }
  63. }
  64. });
  65. //Make the element resizable
  66.  
  67.  
  68. });

请纠正我,如果有什么问题.
提前致谢

解决方法

假设您的元素是列表项下的列表项,其ID为项.

jQuery的

  1. var locations = [];
  2.  
  3. $('#items li').each(function(i) {
  4.  
  5. locations[i] = {
  6. x: $(this).offset().left,y: $(this).offset().top
  7. };
  8.  
  9. });

然后将其发布到您的服务器端.不知道更多的细节,我不能100%肯定这是否是必需的.

页面加载时,只需循环遍历位置并向您添加一个属性,如(假设为PHP)

HTML / PHP

  1. <ul id="items">
  2. <?PHP foreach($items as $item): ?>
  3. <li style="left: <?PHP echo $item['x']; ?>px; top: <?PHP echo $item['x']; ?>px">item</li>
  4. <?PHP endforeach; ?>
  5. </ul>

当然你也可能需要做

CSS

  1. #items {
  2. position: relative;
  3. }
  4.  
  5. #items li {
  6. position: absolute;
  7. }

猜你在找的C#相关文章