图像可拖动但不调整大小

时间:2011-12-22 15:04:56

标签: jquery jquery-resizable

我在容器div中动态添加img标签。在创建img的同时,我添加了2类draggable和resizable,然后追加到容器中。所以我能够拖动但无法调整大小。可能是什么问题?

以下是创建img标签的代码

var img = document.createElement('img');
$(img).attr("id", "dyndiv" + count);
$(img).attr("width", 40);
$(img).attr("height", 100);
$(img).attr("src", 'Uploads/' + window.frames['ifrm'].document.getElementById('dvFileName').innerHTML);

var $ctrl = $(img).addClass("draggable resizable ui-widget-content")
    .draggable({ 
        containment: '#containment-wrapper', 
        cursor: 'move', 
        snap: '#containment-wrapper' 
    })
    .resizable({
        aspectRatio:true,
        containment: '#containment-wrapper'
    });

objid = "dyndiv" + count;
count++;

$(img).css("margin-top", Math.ceil(140/2) + 'px');
$(img).css("margin-left", Math.ceil(60/2) + 'px');
$(img).css({
    'top':20,
    'left':30,
    'zindex':400 + count
});                                 
$("#containment-wrapper").append($ctrl);

这是@Andrew Whitaker建议的更改后的代码,但仍然无法帮助我解决此问题。

          var $wrap_div = $("<div></div>", { 'id' : "dyndiv_img" + count }) ;
          $wrap_div.addClass("wrapper");
           $wrap_div.resizable({ containment: '#containment-wrapper'});

      var img_clip = document.createElement('img');

                                                                     $(img_clip).attr("id", "dyndiv" + count);
                                                                     $(img_clip).attr("src", current_clip ) ;                                                                         
                                                                     var $ctrl = $(img_clip).addClass("draggable ui-resizable ui-widget-content").draggable({ containment: '#containment-wrapper', cursor: 'move',delay: 200,distance: 30, opacity: 0.35}).wrap($wrap_div);
                                                                     objid = "dyndiv" + count ;
                                                                     count++;  

                                                                     $(img_clip).css({'top':20,'left':30});  
                                                                         //($ctrl).resizable({ containment: '#containment-wrapper'}) ;                                                                
                                                                     $("#containment-wrapper").append($ctrl);
                                                                     $('#' + objid).position({
                                                                     of: $( "#containment-wrapper" ),
                                                                     my: "center" + " " + "center",
                                                                     at: "center" + " " + "center"

                                                                    });

1 个答案:

答案 0 :(得分:1)

最后我发现了这个方法。 img tag的调整大小和拖动功能不需要包装div。

              $(img_tag).resizable().parent().draggable();

这适合我。

:)