1

I am using jquery draggable in my app where I applied draggable to image and saved positions(x and y coordinates) in database. as I am dealing with the server and database when I am on edit then the image should be stayed at where it was placed before saved.

I have x,y coordinates with me , so now I need to apply them for image so that it will be placed on the screen where it was before save, How to do this ?

I tried by applying css through jquery to the draggable image but not worked .

  $("#dvPreview").append("<img id='draggable' />");
  $('#dvPreview img').attr('src', $("#eidtedfile").attr("src"));
  if($("#dvPreview img").width() > 640 ){
    $("#dvPreview img").width("638")
  }
  x = $("#x").val().toNum()
  y = $("#y").val().toNum()
  $("#dvPreview img").css({'top': y , 'left' : x}); 

  $("#draggable" ).draggable({
    containment: "parent",
    drag: function() {
    var $this = $(this);
    var thisPos = $this.position();
    var parentPos = $this.parent().position();
    var x = thisPos.left - parentPos.left;
    var y = thisPos.top - parentPos.top;
    $("#x").val(x);
    $("#y").val(y); 
    }
  })
ratnakar
  • 352
  • 1
  • 11
  • I think it should work by simply assigning css. eg `draggable_element.css({'top': 10, 'left' : 20});` – tkay Apr 02 '15 at 11:39
  • I tried it @tkay but its not working, I mentioned that one in my question "I tried by applying css through jquery to the draggable image but not worked" – ratnakar Apr 02 '15 at 11:47
  • Then there should be some other problems with your code. Please post something. – tkay Apr 02 '15 at 12:07
  • What does the browser console say? have you defined toNum() method? The positioning part is working fine for me. [check this fiddle](http://jsfiddle.net/myyx6xjz/1/) – tkay Apr 02 '15 at 12:46
  • Sorry for the previous fiddle . [fiddle](http://jsfiddle.net/myyx6xjz/2/) – tkay Apr 02 '15 at 12:57
  • Yes I defined that method – ratnakar Apr 02 '15 at 12:59
  • @tkay my code is working fine now , I had some issues with rotatable which causes errors , I removed that js file now and its working fine. thanks for your ideas. – ratnakar Apr 02 '15 at 13:09

0 Answers0