简体   繁体   English

使用html5 draggable属性时,保留拖动A元素的外观

[英]Preserve appearance of dragged A element when using html5 draggable attribute

How can i preserve appearance of the dragged A element when using 'draggable' html5 attribute. 使用'draggable'html5属性时,如何保留拖动的A元素的外观。 On some browsers (Safari & Chrome) when dragging anchor, dragged helper is replaced with browser native implementation of dragged element as seen on the screenshots: 在某些浏览器(Safari和Chrome)上拖动锚点时,拖动的帮助器将被替换为拖动元素的浏览器本机实现,如屏幕截图所示:

When dragging DIV 拖动DIV时

拖动DIV时

When dragging A 拖动A时

拖动A时

HTML HTML

<div class="draggable">Draggable DIV</div>
<a href="" class="draggable">Draggable A</a>

CSS CSS

$('.draggable').attr('draggable', true);

Here is the quick JSBin i assembled to demonstrate this issue http://jsbin.com/pihayeceza/1/edit 这是我组装的快速JSBin来演示这个问题http://jsbin.com/pihayeceza/1/edit

Thanks 谢谢

I'm able to preserve the appearance of the dragged element by using DataTransfer.setDragImage . 我可以使用DataTransfer.setDragImage保留拖动元素的外观。 If I add the following code to the JavaScript in your jsbin instance, it work for me on Firefox, Chrome and Safari: 如果我将以下代码添加到jsbin实例中的JavaScript中,它适用于Firefox,Chrome和Safari:

$('a.draggable').on('dragstart', function (ev) {
  var dt = ev.originalEvent.dataTransfer;
  // In IE browsers, setDragImage does not exist. However, the issue we are 
  // trying to fix does not happen in these broswers. So if setDragImage is not
  // available, then just don't do anything.
  if (dt.setDragImage) 
    dt.setDragImage(ev.target, 0, 0);

});

The dataTransfer field of the event has a DataTransfer object associated with the drag operation. 事件的dataTransfer字段具有与拖动操作关联的DataTransfer对象。 You have to fetch it from the original DOM Event rather than from the jQuery Event wrapper, so ev.originalEvent.dataTransfer . 您必须从原始DOM事件而不是从jQ​​uery事件包装器中获取它,因此ev.originalEvent.dataTransfer

For IE browsers, setDragImage is not present but the problem reported in the question does not occur in the first place so if setDragImage is absent, we just don't call it. 对于IE浏览器, setDragImage不存在,但问题中报告的问题不会出现在第一位,所以如果没有setDragImage ,我们就不会调用它。

A bin with the updated code. 带有更新代码的bin

This problem happens because the default behavior of dragging a link with an href attribute is to create an image containing the url to be used as the drag placeholder. 发生此问题是因为拖动具有href属性的链接的默认行为是创建包含要用作拖动占位符的URL的图像。 You can fix this by removing the href attribute, however, to get around that without having to remove the href attribute you can use mousedown/up event handlers to remove the attribute and then re-add it, leaving the anchors clickable*. 您可以通过删除href属性来解决此问题,但是,为了解决这个问题而不必删除href属性,您可以使用mousedown / up事件处理程序删除属性,然后重新添加它,使锚点可单击*。

 $('.draggable').attr('draggable', true).on('mousedown', function () { if ($(this).is('a')) { $(this).data('href', this.href); $(this).removeAttr('href'); } }).on('mouseup', function () { if ($(this).is('a')) { $(this).attr('href', $(this).data('href')); } }).on('click', function () { console.log(this.href); }); 
 .draggable { margin: 10px; display: block; width: 200px; background: #fafafa; color: #333; text-decoration: none; height: 40px; border: 1px solid #eaeaea; line-height: 40px; text-align: center; cursor: move; border-radius: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="draggable">Draggable DIV</div> <a href="http://www.google.com" target="_blank" class="draggable">Draggable A</a> 

*Note: stack snippets doesn't let you follow the link. *注意:堆栈片段不允许您关注该链接。

Wrap your anchor tags with a div and set draggable="false" on the anchor tag. 用div包裹你的锚标签,并在锚标签上设置draggable =“false”。

<div class="draggable">
    <a href="" draggable="false">Draggable A</a>
</div>

You will need additional styling to prevent the button/link from looking blue and underlined. 您将需要额外的样式,以防止按钮/链接看起来是蓝色和下划线。

.draggable a {
  color: inherit;
  text-decoration: inherit;
}

modified your jsbin here http://jsbin.com/rebayif/edit 在这里修改了你的jsbin http://jsbin.com/rebayif/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM