javascript - 拖动在 Internet Explorer 中不起作用

标签 javascript jquery html css internet-explorer

我正在尝试调整大小以及在我的代码中拖动一个 div。代码在下面,但我根本无法在 ie 中拖动 div,但我可以在 chrome 中执行相同的操作。但调整大小在两者中都有效。请检查并指导我如何在 ie 中也进行拖动。

interact('.divname')
  .resizable(true)
  .on('resizemove', function (event) {
    var target = event.target;

    // add the change in coords to the previous width of the target element
    var
      newWidth  = parseFloat(target.style.width ) + event.dx,
      newHeight = parseFloat(target.style.height) + event.dy;

    // update the element's style
    target.style.width  = newWidth + 'px';
    target.style.height = newHeight + 'px';

    //target.textContent = newWidth + '×' + newHeight;
  });  


interact('.divname')
  .draggable({
    // enable inertial throwing
    inertia: true,
    // keep the element within the area of it's parent
    restrict: {
      restriction: "parent",
      endOnly: true,
      elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
    },

    // call this function on every dragmove event
    onmove: function (event) {
      var target = event.target,
          // keep the dragged position in the data-x/data-y attributes
          x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
          y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;

      // translate the element
      target.style.webkitTransform =
      target.style.transform =
        'translate(' + x + 'px, ' + y + 'px)';

      // update the posiion attributes
      target.setAttribute('data-x', x);
      target.setAttribute('data-y', y);
    },
    // call this function on every dragend event
    onend: function (event) {
      var textEl = event.target.querySelector('p');

      textEl && (textEl.textContent =
        'moved a distance of '
        + (Math.sqrt(event.dx * event.dx +
                     event.dy * event.dy)|0) + 'px');
    }
  });


<div class="divname">
</div>

最佳答案

IE8 不支持 transform,所以你的 onmove 处理程序在那里不起作用。此外,IE8 不支持 textContent,因此您的 onend 处理程序也无法正常工作。

对于 IE9,您可以进行转换,但您需要使用 ms 的 vendor 前缀,类似于 webkit

关于javascript - 拖动在 Internet Explorer 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28337858/

相关文章:

javascript - Highcharts numberFormat 返回负零

javascript - Azure移动服务成功回调 `push.apns.send`

javascript - 查询 list 上的空指针异常

javascript - 滚动后如何使主体隐藏的模态窗口居中?

html - 使用css的div矩阵结构

javascript - Iframe 每 X 秒更改一次 src

javascript - 将文件拖放到 Chrome 的 textarea 中时获取光标位置

javascript - jQuery .getJSON 失败,语法错误?

javascript - 使用适用于移动设备的 javascript 将摄像头流式传输到服务器

html - 证明跨度之间的内容