tags:

views:

77

answers:

1

I am new to jquery ..My req is to clone dynamically created image by on_click and able to drag that cloned object .And should be able to delete it and capture the position of that image.After trying for a week with ajax controls,now thought of using jquery. If you could suggest some article related to this will help me.I read lot of articles in this wensite,but couldn't come to a solution.

@@

I got the below code to work,but how to add resizable functionality

$(document).ready(function() { //Counter

      counter = 0;
      //Make element draggable
      $(".drag").draggable({ 
          helper: 'clone',
          containment: 'frame',

          //When first dragged
          stop: function(ev, ui) {
              var pos = $(ui.helper).offset();
              objName = "#clonediv" + counter
              $(objName).css({ "left": pos.left, "top": pos.top });
              $(objName).removeClass("drag");


              //When an existiung object is dragged
              $(objName).draggable({
                  containment: 'parent',
                  stop: function(ev, ui) {
                      var pos = $(ui.helper).offset();
                      //console.log($(this).attr("id"));
                      //console.log(pos.left)
                      //console.log(pos.top)
                  }
              });
          }
      });
      //Make the element resizable 


      //Make element droppable
      $("#frame").droppable({ 

          drop: function(ev, ui) {
              if (ui.helper.attr('id').search(/drag[0-9]/) != -1) {
                  counter++;
                  var element = $(ui.draggable).clone();
                  element.addClass("tempclass");
                  $(this).append(element);
                  $(".tempclass").attr("id", "clonediv" + counter);
                  $("#clonediv" + counter).removeClass("tempclass");

                  //Get the dynamically item id
                  draggedNumber = ui.helper.attr('id').search(/drag([0-9])/)
                  itemDragged = "dragged" + RegExp.$1
                  //console.log(itemDragged)

                  $("#clonediv" + counter).addClass(itemDragged);
              }
          }
      });
  });

I have some sample code to resize the image,but i couldn't drop the clone into my frame

$(document).ready(function() { $("#fff").resizable({ handles: "all", ghost: true, autoHide: true }).parent().draggable({ helper: 'clone', containment: 'frame' });

  });
 ***my Code behind .cs class***
 TableCell newcell = new TableCell();
                System.Web.UI.WebControls.Image myImage = new System.Web.UI.WebControls.Image();
                myImage.ID = "imgv" + dt.Rows[datavount]["id"];
                //myImage.Attributes.Add("alt",  myImage.ID+"fffff");
                myImage.Width = Unit.Pixel(30);
                myImage.Height = Unit.Pixel(30);

Panel newpanel = new Panel(); newpanel.ID = "pnl" + myImage.ID; newpanel.BorderStyle = BorderStyle.Outset; newpanel.Controls.Add(myImage); newpanel.Width = Unit.Pixel(50); newpanel.Height = Unit.Pixel(50); newpanel.Attributes.Add("class", "drag");[my jquery function is applied to drag class] newcell.Controls.Add(newpanel);

Thanks,

+1  A: 

It sounds like jQuery UI Draggable is exactly what you're after. You'd use the helper: 'clone' option for the cloning, like this:

$("img").draggable({
  helper: "clone"
});

You can attach whatever behaviors you want with the drop event, for example adding a close button, getting the position, etc, for example:

$("img").draggable({
  helper: "clone",
  stop: function(e, ui) {
    var top = ui.position.top;   //new left position of cloned/dragged image
    var left = ui.position.left; //new top position of cloned/dragged image
  }
});
Nick Craver
Thanks Nick . I couldn't get it to work and i guess the problem is the jquery library i am using in .aspx page. I need to achieve all behaviors. Please suggest the jquery library and i am using IE7.
Vani
@Vani - Which version of jQuery and jQuery UI are you using?...if the above isn't working, you *should* be getting a JavaScript error of some sort in the lower left of the browser.
Nick Craver
Nick.The functionality is not working with error Microsoft JScript runtime error: Object doesn't support this property or method.the version is jquery-1.4.2.js.So my question is do i need to include any other specific script for drag,drop and clone function?
Vani
@Vani - which version of jQuery UI, and are you running this inside a `document.ready` function?
Nick Craver
Vani
I don't know my images are overlaping with each other after applying Drag functionality to the div class. ANy thing to do with position?.My dynamically created images are also overlapping and not fropped onthe target frame.
Vani
@Vani - I'm not sure exactly what you're after, are you wanting the target frame to be a droppable (http://jqueryui.com/demos/droppable/) with some logic on the `drop` event?
Nick Craver
Nick,Please see my above updated code.The scenario is ,panelA is populated with images dynamically.I should be able to drag image from Panel A to PanelB and that dragged image should be a clone resizable / rotate. Problem i am facing with [ dynamic image is once i give .drag class to dynamic images they are overlapping with each other and i couldn't drop it on Panel B. ] [static image is i can clone,drag,drop but couldn't resize them ] .
Vani
I got this code working for me $(document).ready(function() { $('img').resizable({ handles: "all", ghost: true, autoHide: true }).parent().draggable({ helper: 'clone', containment: 'frame' }); }); wih the only problem ,i couldn't drop it.
Vani