如何检测浏览器支持文件API拖放
我喜欢只为支持拖拽的浏览器在 div 上添加背景。删除文件,
但我不喜欢使用 Modernizr,只是一行脚本
I like to add a background on a div only for browsers which support drag & drop for files
I don't like to use modernizr though, just a one line script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么不直接从 Modernizr 复制所需的部分呢?
用法:
对于文件 API:
Why not just copy required parts from Modernizr?
Usage:
And for File API:
您可以使用:
return 'draggable' in document.createElement('span') && typeof(window.FileReader) != '未定义';
You can use:
return 'draggable' in document.createElement('span') && typeof(window.FileReader) != 'undefined';
如果您根本不想处理 Modernizr,您可以复制它的拖放检测功能:
从 Modernizr GitHub 存储库获取它:
https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js
If you don't want to deal with Modernizr at all, you can just replicate what it does for drag'n'drop detection:
Got it from Modernizr GitHub repository:
https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js
查看 HTML5 拖放检测的 modernizr 源代码技术 https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js
checkout modernizr source code technique for the HTML5 drag and drop detection https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js
除了这似乎错误地将 iOS 检测为支持拖放
except this seems to incorrectly detect iOS as supporting drag and drop
不知道为什么每个人都需要创建一个新元素来检查这一点。我认为只需检查 body 元素是否支持拖动事件以及浏览器是否支持 File API 就足够了
Not sure why everybody needs to create a new element to check this. I think it's enough to just check that the body element supports dragging events and that the browser supports the File API