HTML5 拖拽移动端 [英] HTML5 Drag & Drop for Mobile

查看:61
本文介绍了HTML5 拖拽移动端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对 WHATWG HTML5 拖放的实现:

This is my implementation of WHATWG HTML5 Drag and Drop:

function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("Text",ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data=ev.dataTransfer.getData("Text");
    ev.target.innerHTML+=" <p>"+document.getElementById(data).innerHTML+"</p>";
}

.div {
    width:350px;height:70px;padding:10px;border:1px solid #aaaaaa;
}

<div class="div" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div class="div" ondrop="drop(event)" ondragover="allowDrop(event)">
    <button id="drag1" draggable="true" ondragstart="drag(event)" width="336" height="69">Code</button>
    <button id="drag2" draggable="true" ondragstart="drag(event)" width="336" height="69">Code</button>
</div>

它在 Google Chrome 中运行良好,但在 iOS 或 Android 中却不行.我已经阅读过有关此的其他主题,但其中大多数建议使用 jQuery 或 JavaScript 插件.

It works fine in Google Chrome, but not in iOS or Android. I have read other topics about this but most of them suggest using jQuery or a JavaScript plugin.

是否可以让 HTML 拖放实现在 iOS 和 Android(移动设备)上工作?

Is it possible to make the HTML drag and drop implementation work on iOS and Android (mobile devices)?

推荐答案

不幸的是,除 Internet Explorer Mobile 10 及更高版本和单个已弃用的 Presto 版本的 Opera(现已被替换为基于 Webkit 的版本).请参阅 http://caniuse.com/#feat=dragndrop

Unfortunately Drag and Drop is not currently supported by any mobile browsers except Internet Explorer Mobile 10 onwards and a single deprecated Presto version of Opera (which has now been replaced by Webkit based versions). See http://caniuse.com/#feat=dragndrop

最好的解决方案是检测对拖放的支持(确保这不会在支持 API 的移动版本的浏览器中给你误报),然后使用 JavaScript 在移动设备上进行 polyfill.这也将有助于在旧版浏览器中提供支持,这些版本早于对拖放的原生支持.

The best solution would be to detect support for drag and drop (ensuring that this does not give you false positives in mobile versions of browsers that do support the API) and then use JavaScript to polyfill on mobile devices. This will also help provide support in legacy browser versions that pre-date native support for drag and drop.

您可以使用优秀的 YepNope 条件加载库来运行测试,然后有条件地加载一个或多个脚本以提供JavaScript 支持拖放,或者您可以使用 Modernizr 来执行 测试并加载脚本.

You can use the excellent YepNope conditional loading library to run the test and then conditionally load one or more scripts to provide JavaScript support for drag and drop, or you could use Modernizr to both carry out the test and load the scripts.

这篇关于HTML5 拖拽移动端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆