2015-04-22 20 views
7

Sto riscontrando un problema con jQuery-UI draggable e droppable. Ho bisogno di trascinare un draggable all'interno di un droppable che è posto all'interno di un iframe. Funziona bene finché non faccio scorrere l'iframe. Le coordinate trascinabili non vengono aggiornate.jQuery UI dropzone offset errato all'interno di scorrimento iframe

La questione è dimostrato in questo fiddle

sto usando la soluzione di seguito per effettuare il drag and drop di Iframe possibile in primo luogo. Calcola le compensazioni giuste ma non utilizza gli offset di scorrimento dell'iframe. Ho provato ma non ho potuto ottenerlo ottimizzato in modo da prendere in considerazione le scroll scroll.

// Create new object to cache iframe offsets 
$.ui.ddmanager.frameOffsets = {}; 

// Override the native `prepareOffsets` method. This is almost 
// identical to the un-edited method, except for the last part! 
$.ui.ddmanager.prepareOffsets = function (t, event) { 
    var i, j, 
     m = $.ui.ddmanager.droppables[t.options.scope] || [], 
     type = event ? event.type : null, // workaround for #2317 
     list = (t.currentItem || t.element).find(":data(ui-droppable)").addBack(), 
     doc, frameOffset; 

    droppablesLoop: for (i = 0; i < m.length; i++) { 

     //No disabled and non-accepted 
     if (m[i].options.disabled || (t && !m[i].accept.call(m[i].element[0], (t.currentItem || t.element)))) { 
      continue; 
     } 

     // Filter out elements in the current dragoged item 
     for (j = 0; j < list.length; j++) { 
      if (list[j] === m[i].element[0]) { 
       m[i].proportions().height = 0; 
       continue droppablesLoop; 
      } 
     } 

     m[i].visible = m[i].element.css("display") !== "none"; 
     if (!m[i].visible) { 
      continue; 
     } 

     //Activate the droppable if used directly from draggables 
     if (type === "mousedown") { 
      m[i]._activate.call(m[i], event); 
     } 

     // Re-calculate offset 
     m[i].offset = m[i].element.offset(); 

     // Re-calculate proportions (jQuery UI ~1.10 introduced a `proportions` cache method, so support both here!) 
     proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight }; 
     typeof m[i].proportions === 'function' ? m[i].proportions(proportions) : (m[i].proportions = proportions); 

     /* ============ Here comes the fun bit! =============== */ 

     // If the element is within an another document... 
     if ((doc = m[i].document[0]) !== document) { 
      // Determine in the frame offset using cached offset (if already calculated) 
      frameOffset = $.ui.ddmanager.frameOffsets[doc]; 
      if (!frameOffset) { 
       // Calculate and cache the offset in our new `$.ui.ddmanager.frameOffsets` object 
       frameOffset = $.ui.ddmanager.frameOffsets[doc] = $(
        // Different browsers store it on different properties (IE...) 
        (doc.defaultView || doc.parentWindow).frameElement 
       ).offset(); 
      } 

      // Add the frame offset to the calculated offset 
      m[i].offset.left += frameOffset.left; 
      m[i].offset.top += frameOffset.top; 
     } 
    } 
} 

Qualcuno ha un suggerimento per risolvere il problema. Le raccomandazioni per ottenere la stessa cosa in un altro modo sono anche più che benvenute.

+0

Qualche commento sulla risposta ??? –

+0

Questo funziona bene in safari, Chrome ha problemi. – gwing33

risposta

3

È possibile modificare l'altezza proportions s' a seconda della quantità di scorrimento nella iframe. L'importo può essere raggiunto utilizzando $("iframe").contents().scrollTop() come avete usato per modificare la quantità di scorrimento:

proportions = { 
     width: m[i].element[0].offsetWidth, 
     height: m[i].element[0].offsetHeight - $("iframe").contents().scrollTop() 
}; 

Ecco il DEMO.

+2

@Boyd Qualche commento? –

+1

@falsarella Drag and Drop. Vedrai la differenza. –

+0

Grazie. Questo ha davvero risolto il mio problema. Nel frattempo ho implementato la stessa funzionalità utilizzando il drag and drop HTML5 che sembra essere una soluzione più appropriata per il drag and drop tra iframe. Ha anche un supporto cross-browser abbastanza accettabile. – Boyd