首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >(Re)存储每个克隆的可拖动JQuery的位置和大小

(Re)存储每个克隆的可拖动JQuery的位置和大小
EN

Stack Overflow用户
提问于 2017-07-28 22:23:45
回答 1查看 253关注 0票数 0

我有一个div (画布),作为rects的droppable。放置在div上的Rect将被克隆,并且可以在该div中拖动和调整大小。

我的问题是:如何(重新)存储动态克隆元素的位置和大小?

工作原理:将多个矩形拖到画布上调整大小或在矩形内拖动,单击保存

到目前为止,它给出了克隆的矩形的正确数量,但它只保存了最后一个克隆元素的位置和大小。

如何分别为每个克隆的rect添加隐藏的文本字段?

代码语言:javascript
运行
复制
$(function() {
  $('#rect').draggable({
    revert: "invalid",
    helper: function(event, ui) {
      return $(this).clone(true);
    }

  });

  $('#bu').click(function() {
    alert("no of rect set: " + $('.rectset').length);

    $('.rectset').each(function() {
      var posTop = $('input#posTop').val();
      var posLeft = $('input#posLeft').val();
      var height = $('input#sizeHeight').val();
      var width = $('input#sizeWidth').val();

      alert("left: " + posLeft + ", top: " + posTop +
        " ,height: " + height + ", width: " + width);
    });
  });


  $("#canvas").droppable({
    accept: "#rect",
    drop: function(e, ui) {
      if ($(ui.draggable).hasClass('ui-draggable-dragging')) {
        /*alert("rect is dragged and not copied again");*/
        return

      }

      var droppedRect = $(ui.draggable).clone().addClass('rectset')


      droppedRect.append('<input type="hidden" id="posLeft" value="empty"></input>');
      droppedRect.append('<input type="hidden" id="posTop" value="empty"></input>');
      droppedRect.append('<input type="hidden" id="sizeWidth" value="empty"></input>');
      droppedRect.append('<input type="hidden" id="sizeHeight" value="empty"></input>');

      droppedRect.appendTo(this);

      droppedRect.draggable({
        containment: "#canvas",
        scroll: false,
        stop: function(event, ui) {
          //	alert($('input#posLeft').val() + " " + $('input#posTop').val()) ;
          var posleft = ui.position.left;
          var postop = ui.position.top;
          $('input#posLeft').attr('value', posleft);
          $('input#posTop').attr('value', postop);
          alert($('input#posLeft').val() + " " + $('input#posTop').val());

        }
      }).resizable({
        ghost: true,
        containment: "#canvas",
        stop: function(event, ui) {
          $('#size').attr('value', ui.size)

          var width = ui.size.width;
          var height = ui.size.height;
          //       alert($('input#sizeWidth').val() + " " + $('input#sizeHeight').val()) ;
          $('input#sizeWidth').attr('value', width);
          $('input#sizeHeight').attr('value', height);
          alert($('input#sizeWidth').val() + " " + $('input#sizeHeight').val());

        }
      });
    }
  });

});
代码语言:javascript
运行
复制
#canvas {
  width: 700px;
  height: 400px;
  border: 10px solid black;
  padding: 15px 15px 15px 150px;
}

#rect {
  border: 3px solid black;
  background: #ffff99;
  width: 100px;
  height: 100px;
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<button id="bu" onclick="save()">Save</button >
<div id="rect" class="ui-widget-content"> </div> 
<div id="canvas" class="ui-widget-header">

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-28 23:17:15

就像Mike说的,ID必须是唯一的。我解决这个问题的方法是添加一个计数器var i = 0,然后在每个新的droppedRect上将id属性设置为递增的数字。此外,我认为要实现将数据放到矩形上的相同目标,更好的方法是通过jQuery.data。我使用jQueryUI 1.9,因为它在小提琴中被使用了。

代码语言:javascript
运行
复制
$(function() {
  $('#rect').draggable({
    revert: "invalid",
    helper: function(event, ui) {
      return $(this).clone(true);
    }
  });
  var i = 0;
  $('#bu').click(function() {
    console.log("no of rect set: " + $('.rectset').length);

    $('.rectset').each(function(a, b) {
      console.log("left: " + $(b).data('posleft') + ", top: " + $(b).data('postop') +
        " ,height: " + $(b).data('height') + ", width: " + $(b).data('width'))
    });
  });

  $("#canvas").droppable({
    accept: "#rect",
    drop: function(e, ui) {
      if ($(ui.draggable).hasClass('ui-draggable-dragging')) {
        return;
      }

      var droppedRect = $(ui.draggable).clone().addClass('rectset').attr('id', i++)
        .appendTo(this)
        .data({
          'posleft': ui.position.left,
          'postop': ui.position.top,
          'width': ui.draggable[0].offsetWidth,
          'height': ui.draggable[0].offsetHeight
        });
      console.log("Dropped - left: " + ui.position.left + " top:" + ui.position.top + " width: " + ui.draggable[0].offsetWidth + " height: " + ui.draggable[0].offsetHeight);
      droppedRect.draggable({
        containment: "#canvas",
        scroll: false,
        stop: function(event, ui) {
          $(this).data('posleft', ui.position.left);
          $(this).data('postop', ui.position.top);
          console.log("Moved - left: " + ui.position.left + " top:" + ui.position.top);

        }
      }).resizable({
        ghost: true,
        containment: "#canvas",
        stop: function(event, ui) {
          $(this).data('width', ui.size.width);
          $(this).data('height', ui.size.height);
          console.log("Resized - width: " + ui.size.width + " height: " + ui.size.height);
        }
      });
    }
  });
});
代码语言:javascript
运行
复制
#canvas {
  width: 700px;
  height: 400px;
  border: 10px solid black;
  padding: 15px 15px 15px 150px;
}

.rect {
  border: 3px solid black;
  background: #ffff99!important;
  width: 100px;
  height: 100px;
}
代码语言:javascript
运行
复制
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

<button id="bu">Save</button >
<div id="rect" class="rect ui-widget-content"> </div> 
<div id="canvas" class="ui-widget-header">

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45375913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档