简体   繁体   English

如何防止在React应用中拖放图像?

[英]How can I prevent drag and drop images in a react app?

We got how to prevent drag drop image with jquery with $('img').on('dragstart', function(event) { event.preventDefault(); }); 我们获得了如何使用$('img').on('dragstart', function(event) { event.preventDefault(); }); 来防止jquery拖放图像 $('img').on('dragstart', function(event) { event.preventDefault(); }); but I want to achieve the same in a react app, and without jquery. 但我想在React应用程序中实现相同效果,并且没有jquery。 How can I do this ? 我怎样才能做到这一点 ? a) locally (image per image) b) globally (for all images of the react app) a)本地(每个图像一个图像)b)全局(用于react应用程序的所有图像)

I still want to keep control over other interactions, so img { pointer-events: none; } 我仍然想控制其他交互,所以img { pointer-events: none; } img { pointer-events: none; } is not a solution. img { pointer-events: none; }不是解决方案。

The goal is to prevent saving image through drag and drop. 目的是防止通过拖放保存图像。

You can use onDragStart : 您可以使用onDragStart

onDragStart={this.preventDragHandler}

And the handler: 和处理程序:

preventDragHandler = (e) => {
  e.preventDefault();
}

For apps using HTML5, setting draggable={false} in react should accomplish this as well. 对于使用HTML5的应用程序,在react中设置draggable={false}应该可以做到这一点。 It will wind up setting the HTML5 draggable attribute to "false". 它将把HTML5可拖动属性设置为“ false”。

Another option would be to just use CSS and set pointer-events: none; 另一种选择是只使用CSS并设置pointer-events: none; on the image. 在图像上。

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

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