jquery - 可拖动可调整大小在 Chrome 中不起作用

标签 jquery jquery-ui drag-and-drop jquery-ui-draggable jquery-ui-resizable

我在 Chrome 中的图像上使可调整大小和可拖动功能一起工作时遇到一些问题。我的页面侧面有一个(可拖动)图像列表,可以将其拖放到 div 上(图像已克隆)。放置的图像应可调整大小并可拖动。我编写了以下 JavaScript:

$(function() {
  $(".scrollable").scrollable();
  $(".draggable").draggable({
    helper: 'clone',
    appendTo: 'body'
  });

  $("#canvas").droppable({
    accept: '.draggable',
    drop: function(e, ui){
      if (ui.draggable.attr("class").indexOf('item') == -1){
        var clone = $(ui.draggable).clone();
        clone.removeClass("ui-draggable draggable");
        clone.addClass('item');
        $(this).append(clone);
        $('.item').resizable({
          containment: 'parent',
          aspectRatio: true
        }).parent().draggable({
          containment: 'parent'
        });
      }
    }
  });
});

这会在 Firefox 中生成以下代码,该代码可以完美运行:

<div id="canvas" class="white-box ui-droppable">
  <div class="ui-wrapper ui-draggable" style="overflow: hidden; position: relative; width: 126px; height: 41px; top: 104px; left: 211px; margin: 0px;">
    <img class="item ui-resizable" src="/images/qr-code/description-sm.png?1328705570" alt="Description-sm" style="resize: none; position: static; display: block; height: 41px; width: 126px;">
    <div class="ui-resizable-handle ui-resizable-e"></div>
    <div class="ui-resizable-handle ui-resizable-s"></div>
    <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 1001;"></div>
  </div>
</div>

然而,在 Chrome 中,元素被拖动和克隆,但它们没有被显示。从生成的以下代码可以明显看出这一点:

<div id="canvas" class="white-box ui-droppable">
  <div class="ui-wrapper ui-draggable" style="overflow-x: hidden; overflow-y: hidden; width: 0px; height: 0px; top: auto; left: auto; margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; position: relative; ">
    <img alt="Description-sm" class="item ui-resizable" src="/images/qr-code/description-sm.png?1328705570" style="resize: none; position: static; zoom: 1; display: block; height: 0px; width: 0px; ">
    <div class="ui-resizable-handle ui-resizable-e"></div><div class="ui-resizable-handle ui-resizable-s"></div>
    <div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" style="z-index: 1001; "></div>
  </div>
</div>

但是,我无法理解为什么图像及其相应的外部 div 的宽度和高度变为 0px。一个简单的解决方案是在 javascript 中更改高度/宽度,但我确信会有更好的解决方案。

感谢您的帮助。

最佳答案

诀窍是专门将高度和宽度添加到 img 标签中。如果没有这个,div 和图像高度将被显式设置为零(但仅在 Chrome 中)。

关于jquery - 可拖动可调整大小在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212103/

相关文章:

JavaScript 编码协助

jquery - 如何使用滚动条查看可拖动div中的长内容?

jquery - jQuery 中的嵌套 Accordion 菜单

ios - 如何跨 collectionview 添加 View ?

c# - 读取鼠标拖动的项目

jquery - 是否有 ADO.NET 数据服务 jQuery 插件?

javascript - 计算在输入字段中接受空格

jquery - 有没有办法让 JQuery ajax 成功函数访问它所包含的对象?

jquery - 可排序,可选择文本

html - 拖放 Revel框架使用html5