首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery拖放+ .resizable

Jquery拖放+ .resizable
EN

Stack Overflow用户
提问于 2021-04-28 11:24:49
回答 1查看 58关注 0票数 0

我想将.resizable()添加到以下内容:https://jsfiddle.net/6aneLqu5/23/

问题就在这里。我可以把图像拖到容器里。但不会像上面的jsfiddle.net演示那样改变。为了更好地理解,我附加了一个调整大小的函数。http://jsfiddle.net/wJUHF/7/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-28 14:54:53

请考虑以下几点。

代码语言:javascript
复制
$(function() {
  var resizeOpts = {
    handles: "all",
    autoHide: true,
  };

  function make_draggable(el, params) {
    return $(el).draggable(params);
  }

  make_draggable(".dragImg", {
    helper: "clone"
  });

  $("#dropHere").droppable({
    accept: ".dragImg",
    drop: function(e, ui) {
      var dropItem = $(ui.helper).clone();
      $(this).append(dropItem);
      var newCount = $(".img", this).length;
      dropItem.addClass("item-" + newCount);
      $("img", dropItem).addClass("imgSize-" + newCount);

      dropItem.removeClass("dragImg ui-draggable ui-draggable-dragging");

      dropItem.dblclick(function() {
        $(this).remove();
      });
      make_draggable(dropItem, {
        containment: "parent"
      });
      $("img", dropItem).resizable(resizeOpts);
    },
  });
});
代码语言:javascript
复制
#dropHere {
  width: 400px;
  height: 400px;
  border: dotted 1px black;
}
代码语言:javascript
复制
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="dragImg"><img class="img" src="http://www.thumbshots.com/Portals/0/Images/Feature%20TS%201.jpg" /></div>

<div id="dropHere"></div>

这利用了length对象的jQuery属性来提供项的计数。

由于用户可以删除项,因此有可能出现ID冲突。所以也许用你的柜台是个更好的主意。下面是这个想法的同样的例子。

代码语言:javascript
复制
$(function() {
  var resizeOpts = {
    handles: "all",
    autoHide: true,
  };

  var count = 0;

  function make_draggable(el, params) {
    return $(el).draggable(params);
  }

  make_draggable(".dragImg", {
    helper: "clone"
  });

  $("#dropHere").droppable({
    accept: ".dragImg",
    drop: function(e, ui) {
      var dropItem = $(ui.helper).clone();
      $(this).append(dropItem);
      var newCount = ++count;
      dropItem.addClass("item-" + newCount);
      $("img", dropItem).replaceWith("<div class='img imgSize-" + newCount + " drag_elm'>Image " + newCount + "</div>");

      dropItem.removeClass("dragImg ui-draggable ui-draggable-dragging");

      dropItem.dblclick(function() {
        $(this).remove();
      });
      make_draggable(dropItem, {
        containment: "parent"
      });
      $(".img", dropItem).resizable(resizeOpts);
    },
  });
});
代码语言:javascript
复制
#dropHere {
  width: 400px;
  height: 400px;
  border: dotted 1px black;
}

.drag_elm {
  width: 80px;
  height: 80px;
  background: aqua;
  border: 1px solid red;
}
代码语言:javascript
复制
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="dragImg"><img class="img" src="http://www.thumbshots.com/Portals/0/Images/Feature%20TS%201.jpg" /></div>

<div id="dropHere"></div>

可以将计数设置为01。它只是改变了你以后使用它的方式。使用++count在使用它之前增加它,或者将它赋值给另一个变量。count++会在它使用后增加它。

更新

添加到带有.replaceWith()的第二个片段中,将图像转换为Div。

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

https://stackoverflow.com/questions/67299301

复制
相关文章

相似问题

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