9

I am solving this problem and I do not know what to do.

Situation: I have draggable elements at the top of page and some sortable holders in iframe. When I try to drag element to iframe, it is working fine. But, when iframe is scrolled down and I start dragging draggable element, it connects to first sortable holder in iframe, not to that sortable holder which is currently at top of visible part of iframe.

js fiddle with complete code: https://jsfiddle.net/0d420qpj/

screen video : http://screencast-o-matic.com/watch/coltDdhakq

Watch video and you will see problem in action.

$(".drag").draggable({ 
      helper : "clone",
      iframeFix: $('#iframe'),
      iframeOffset: $('#iframe').offset(),
      connectToSortable : f.find(".sort_holder"),
      distance : 10,
      cursorAt: { left: 20, top : 20},
      scroll : true,
      start: function(event, ui) { 

      },
      drag: function(event, ui) { 

      },
      stop: function(event, ui) { 

      }
    });

Can you please help me solve this situation?

3

2 Answers 2

3
+25

The video was deleted, so mb I don't understand your problem completely. But as I see, your .drag-element just connects to top of container and scrolls it. So if you disable scrolling of draggable and sortable or decrease sensitivity, the issue will be gone.

.sortable({ 
    scroll: false
});
.draggable({ 
    scroll: false
});

https://jsfiddle.net/0d420qpj/4/

Or you can place you draggable element to another position (left or right from container).

Update

Ok, I found this way to resolve your issue. It's not so beautiful, but it works when scrolling is disabled.

$('#iframe').contents().on('scroll', function() {
  $(".drag").draggable("option", "cursorAt", { top: -1 * $('#iframe').contents().scrollTop() });
});

https://jsfiddle.net/dz0orkox/1/

Update 2

For center alignment of cursor need to add height from top to iframe. 60px in our case

$('#iframe').contents().on('scroll', function() {
    $(".drag").draggable("option", "cursorAt", { top: -1 * $('#iframe').contents().scrollTop() + 60 });
});

https://jsfiddle.net/dz0orkox/3/

5
  • I'm happy to see an answer! The problem is, the following: When starting to drag the red square from outside of the iframe into the iframe it jumps away from the mouse pointer vertically creating an offset. Especially when the iframe is scrolled. Removing the option to scroll, when the mouse pointer is close to an edge doesn't seem to solve the problem.
    – Sandro
    Commented Sep 23, 2016 at 15:08
  • Thanks for updating your answer. But when I drag the square down to the rectangles the square is still not "attached" to the mouse pointer, right?
    – Sandro
    Commented Sep 23, 2016 at 18:15
  • 1
    Yes, not attached. Need to add top value of iframe to cursor top value. ~60px in the case. Updated Commented Sep 23, 2016 at 19:28
  • Yeah, looks good over the iframe. Can you make that work above the iframe (before moving over the iframe) as well?
    – Sandro
    Commented Sep 23, 2016 at 21:38
  • I think one would need to overwrite jQueryui functionality at some point.
    – Sandro
    Commented Sep 24, 2016 at 7:57
0

There seems to be no way to find a working solution to that so maybe it's an option for you to use HTML 5 drag and drop instead. E.g. https://github.com/StackHive/DragDropInterface. As I have the same issue I will probably go into this direction.

Edit: There is a more advanced library. See this answer: Offset issues with jQuery Draggable into an iFrame based Sortable

Not the answer you're looking for? Browse other questions tagged or ask your own question.