【问题标题】:jQuery UI dropzone wrong offset inside scrolled iframe滚动 iframe 内的 jQuery UI dropzone 错误偏移
【发布时间】:2015-04-22 21:20:27
【问题描述】:

我在使用 jQuery-UI 可拖动对象和可放置对象时遇到问题。我需要在放置在 iframe 中的 droppable 中拖动一个可拖动的对象。在我滚动 iframe 之前,这可以正常工作。可放置的坐标不会更新。

这个问题在这个fiddle中得到了证明

我首先使用下面的解决方法使拖放到 iframe 成为可能。它计算正确的偏移量,但不使用 iframe 的滚动偏移量。我尝试了但无法对其进行调整,因此它会考虑滚动偏移。

// 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;
        }
    }
}

有没有人提出解决问题的建议。以另一种方式实现相同目标的建议也非常受欢迎。

【问题讨论】:

  • 对答案有什么意见???
  • 这在 safari 中运行良好,Chrome 有问题。

标签: javascript jquery jquery-ui iframe offset


【解决方案1】:

您可以根据 iframe 中的滚动量来更改 proportions 的高度。该数量可以使用 $("iframe").contents().scrollTop() 来实现,因为您已经使用它来更改滚动量:

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

这是DEMO

【讨论】:

  • @falsarella 拖放。你会看到区别。
  • 谢谢。这确实解决了我的问题。同时,我使用 HTML5 拖放实现了相同的功能,这似乎是在 iframe 之间拖放的更合适的解决方案。它还具有相当可接受的跨浏览器支持。
  • 我很惊讶这个答案被接受了,建议的解决方案非常人为,无法处理正常的预期用例。这是经过稍微修改的文档的公认答案,您很快就会发现它已损坏:jsfiddle.net/m7nyfqct
猜你喜欢
  • 1970-01-01
  • 2017-11-13
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多