【问题标题】:Disable drag-drop while HTML5 file browse popup is open在 HTML5 文件浏览弹出窗口打开时禁用拖放
【发布时间】:2014-12-25 04:57:01
【问题描述】:

我正在开发一个具有 HTML5 文件输入按钮和拖放区域的 Angular JS 应用程序。 只要文件浏览弹出窗口打开,我就需要禁用拖放区域。实现这一目标的最佳方法是什么?

【问题讨论】:

  • 你能显示你的代码吗?谢谢。
  • 对话框打开时是否所有内容都没有禁用?如果您试图阻止两者都起作用,为什么不做一个微不足道的条件来测试。说 onclick 文件输入你禁用 DnD 然后当它没有启用时。
  • 只需设置一个标志。如果设置了,则窗口打开,不接受。 False,窗口关闭,接受。
  • 如何知道文件浏览窗口何时关闭?我查看了其他线程,找不到任何解决方案。 OnFocus 事件没有帮助,因为当文件从文件浏览窗口拖放到浏览器时文件输入按钮获得焦点

标签: angularjs html


【解决方案1】:

这是我刚刚为您制作的一个示例,这是一个更新的 Google DnD 控制器,我制作了更高级并添加了您想要的功能。

window.fileBrowserActive = false; //init the controller to false
function DnDFileController(selector, onDropCallback) {
   var el_ = document.querySelector(selector);//select our element
   var overCount = 0;//over count is 0
   this.dragenter = function(e) 
   {
       e.stopPropagation();//stop propagation and prevent the default action if any
       e.preventDefault();
       overCount++;//increment and assign the overCount var to 1
       if(fileBrowserActive == false)//if the fileBrowserAction is false we can add dropping class
       {
           el_.classList.add('dropping');
       }
       else  //else add error or none at all it's your choice
       {
           el_.classList.add('error');
       }
   };


   this.dragover = function(e) 
   {
       e.stopPropagation();
       e.preventDefault();
   };

   this.dragleave = function(e)
   {
       e.stopPropagation();
       e.preventDefault();
       if (--overCount <= 0) { //we decrement and assign overCount once if it's equal to or less than 0 remove the classes and assign overCount to 0 
          el_.classList.remove('dropping');
          el_.classList.remove('error');  
          overCount = 0;
       }
    };

   this.drop = function(e) {
      e.stopPropagation();
      e.preventDefault();
      el_.classList.remove('dropping');
      el_.classList.remove('error');
       if(fileBrowserActive === false)
       {  //if fileBrowserActive is false send the datatranser data to the callback
          onDropCallback(e.dataTransfer);
       }
       else 
       {  //else send false
           onDropCallback(false);
       }
   };
    el_.addEventListener('dragenter', this.dragenter, false);
    el_.addEventListener('dragover', this.dragover, false);
    el_.addEventListener('dragleave', this.dragleave, false);
    el_.addEventListener('drop', this.drop, false);

}
var isFileBrowserActive = function(e){
    fileBrowserActive = true; //once clicked on it is active 
    this.onchange = function(e){ //create the onchange listener
        if(e.target.value !== "") //if the value is not blank we keep it active
        {   //so now user can't DnD AND use the file browser
            fileBrowserActive = true;
        }
        else if(e.target.value == "") //if it is blank means they click cancel most likely
        {
            fileBrowserActive = false;//assign false back
        }  //remove the listener 
        this.removeEventListener('change',this.onchange,false);
    };
    //assign the listener for change
    this.addEventListener('change',this.onchange,false);
};

 var fB = document.getElementById('fileBrowser'); //grab our element for file input
 fB.addEventListener('click',isFileBrowserActive,false); //assign the click event
 var activation = new DnDFileController('#dragNDrop',function(e){ //grab our DnD element
                  console.log(e); //console log the e (either datatransfer || false)
                  });

查看This Fiddle 了解其工作原理

【讨论】:

  • 为了测试而想添加的东西,把东西拖到蓝色方块上,它会变成绿色,意思是GOOD TO GO。然后打开文件浏览器并移动对话框,现在再次拖动它会变成红色,意思是错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多