Search code examples
jqueryjquery-resizable

Image is draggable but not resizing


I am adding img tag dynamically in a container div. While creating img i am adding 2 class draggable and resizable and then append to the container. So i am able to drag but unable to resize. What might be the issue?

Below is the code of creating img tag

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);

This is the changed code as suggested by @Andrew Whitaker but still it is not working kindly help me with this..

          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"

                                                                    });

Solution

  • At last I found the method. No wrapper div is needed for resize and drag functionality for img tag.

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

    This works for me.

    :)