如何检测浏览器支持文件API拖放

发布于 2024-11-18 04:10:28 字数 70 浏览 3 评论 0原文

我喜欢只为支持拖拽的浏览器在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

少女七分熟 2024-11-25 04:10:28

为什么不直接从 Modernizr 复制所需的部分呢?

var isEventSupported = (function() {

      var TAGNAMES = {
        'select': 'input', 'change': 'input',
        'submit': 'form', 'reset': 'form',
        'error': 'img', 'load': 'img', 'abort': 'img'
      };

      function isEventSupported( eventName, element ) {

        element = element || document.createElement(TAGNAMES[eventName] || 'div');
        eventName = 'on' + eventName;

        // When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize", whereas `in` "catches" those
        var isSupported = eventName in element;

        if ( !isSupported ) {
          // If it has no `setAttribute` (i.e. doesn't implement Node interface), try generic element
          if ( !element.setAttribute ) {
            element = document.createElement('div');
          }
          if ( element.setAttribute && element.removeAttribute ) {
            element.setAttribute(eventName, '');
            isSupported = typeof element[eventName] == 'function';

            // If property was created, "remove it" (by setting value to `undefined`)
            if ( typeof element[eventName] != 'undefined' ) {
              element[eventName] = undefined;
            }
            element.removeAttribute(eventName);
          }
        }

        element = null;
        return isSupported;
      }
      return isEventSupported;
    })();

用法:

if (isEventSupported('dragstart') && isEventSupported('drop')) {
  ...
}

对于文件 API:

var isFileAPIEnabled = function() {
  return !!window.FileReader;
};

Why not just copy required parts from Modernizr?

var isEventSupported = (function() {

      var TAGNAMES = {
        'select': 'input', 'change': 'input',
        'submit': 'form', 'reset': 'form',
        'error': 'img', 'load': 'img', 'abort': 'img'
      };

      function isEventSupported( eventName, element ) {

        element = element || document.createElement(TAGNAMES[eventName] || 'div');
        eventName = 'on' + eventName;

        // When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize", whereas `in` "catches" those
        var isSupported = eventName in element;

        if ( !isSupported ) {
          // If it has no `setAttribute` (i.e. doesn't implement Node interface), try generic element
          if ( !element.setAttribute ) {
            element = document.createElement('div');
          }
          if ( element.setAttribute && element.removeAttribute ) {
            element.setAttribute(eventName, '');
            isSupported = typeof element[eventName] == 'function';

            // If property was created, "remove it" (by setting value to `undefined`)
            if ( typeof element[eventName] != 'undefined' ) {
              element[eventName] = undefined;
            }
            element.removeAttribute(eventName);
          }
        }

        element = null;
        return isSupported;
      }
      return isEventSupported;
    })();

Usage:

if (isEventSupported('dragstart') && isEventSupported('drop')) {
  ...
}

And for File API:

var isFileAPIEnabled = function() {
  return !!window.FileReader;
};
送君千里 2024-11-25 04:10:28

您可以使用:

return 'draggable' in document.createElement('span') && typeof(window.FileReader) != '未定义';

You can use:

return 'draggable' in document.createElement('span') && typeof(window.FileReader) != 'undefined';

翻身的咸鱼 2024-11-25 04:10:28

如果您根本不想处理 Modernizr,您可以复制它的拖放检测功能:

var supportsDragAndDrop = function() {
    var div = document.createElement('div');
    return ('draggable' in div) || ('ondragstart' in div && 'ondrop' in div);
}

从 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:

var supportsDragAndDrop = function() {
    var div = document.createElement('div');
    return ('draggable' in div) || ('ondragstart' in div && 'ondrop' in div);
}

Got it from Modernizr GitHub repository:

https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js

甩你一脸翔 2024-11-25 04:10:28

查看 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

醉生梦死 2024-11-25 04:10:28

除了这似乎错误地将 iOS 检测为支持拖放

except this seems to incorrectly detect iOS as supporting drag and drop

地狱即天堂 2024-11-25 04:10:28

不知道为什么每个人都需要创建一个新元素来检查这一点。我认为只需检查 body 元素是否支持拖动事件以及浏览器是否支持 File API 就足够了

supportsDragAndDrop(){
   body = document.body
   return ("draggable" in body || ("ondragstart" in body && "ondrop" in body)) 
        && window.FormData && window.FileReader
}

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

supportsDragAndDrop(){
   body = document.body
   return ("draggable" in body || ("ondragstart" in body && "ondrop" in body)) 
        && window.FormData && window.FileReader
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文