久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何像 Gmail 一樣檢測進入和離開窗口的 HTML5 拖動

How do I detect a HTML5 drag event entering and leaving the window, like Gmail does?(如何像 Gmail 一樣檢測進入和離開窗口的 HTML5 拖動事件?)
本文介紹了如何像 Gmail 一樣檢測進入和離開窗口的 HTML5 拖動事件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我希望能夠在帶有文件的光標進入瀏覽器窗口后立即突出顯示放置區域,這與 Gmail 的做法完全一樣.但我無法讓它發揮作用,我覺得我只是錯過了一些非常明顯的東西.

I'd like to be able to highlight the drop area as soon as the cursor carrying a file enters the browser window, exactly the way Gmail does it. But I can't make it work, and I feel like I'm just missing something really obvious.

我一直在嘗試做這樣的事情:

I keep trying to do something like this:

this.body = $('body').get(0)
this.body.addEventListener("dragenter", this.dragenter, true)
this.body.addEventListener("dragleave", this.dragleave, true)`

但是,只要光標移出和移出 BODY 以外的元素,就會觸發事件,這是有道理的,但絕對行不通.我可以在所有東西上放置一個元素,覆蓋整個窗口并對其進行檢測,但這將是一種可怕的方式.

But that fires the events whenever the cursor moves over and out of elements other than BODY, which makes sense, but absolutely doesn't work. I could place an element on top of everything, covering the entire window and detect on that, but that'd be a horrible way to go about it.

我錯過了什么?

推薦答案

我用超時解決了它(不是很干凈,但可以):

I solved it with a timeout (not squeaky-clean, but works):

var dropTarget = $('.dropTarget'),
    html = $('html'),
    showDrag = false,
    timeout = -1;

html.bind('dragenter', function () {
    dropTarget.addClass('dragging');
    showDrag = true; 
});
html.bind('dragover', function(){
    showDrag = true; 
});
html.bind('dragleave', function (e) {
    showDrag = false; 
    clearTimeout( timeout );
    timeout = setTimeout( function(){
        if( !showDrag ){ dropTarget.removeClass('dragging'); }
    }, 200 );
});

我的示例使用 jQuery,但這不是必需的.以下是正在發生的事情的摘要:

My example uses jQuery, but it's not necessary. Here's a summary of what's going on:

  • dragenter 和 html(或正文)的 dragover 上將標志 (showDrag) 設置為 true元素.
  • dragleave 上將標志設置為 false.然后設置一個短暫的超時來檢查標志是否仍然為假.
  • 理想情況下,在設置下一個超時之前跟蹤并清除它.
  • Set a flag (showDrag) to true on dragenter and dragover of the html (or body) element.
  • On dragleave set the flag to false. Then set a brief timeout to check if the flag is still false.
  • Ideally, keep track of the timeout and clear it before setting the next one.

這樣,每個 dragleave 事件都會給 DOM 足夠的時間讓新的 dragover 事件重置標志.我們關心的真正的,最終的 dragleave 會看到標志仍然是假的.

This way, each dragleave event gives the DOM enough time for a new dragover event to reset the flag. The real, final dragleave that we care about will see that the flag is still false.

這篇關于如何像 Gmail 一樣檢測進入和離開窗口的 HTML5 拖動事件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How do I write FileReader test in Jasmine?(如何在 Jasmine 中編寫 FileReader 測試?)
JS/HTML5 WebSocket: Connect without HTTP call(JS/HTML5 WebSocket:無需 HTTP 調用即可連接)
CSS: Repeat Table Header after Page Break (Print View)(CSS:分頁后重復表頭(打印視圖))
Put HTML head in another file(將 HTML 頭放在另一個文件中)
What is the real difference between the quot;headquot; and quot;headerquot; tag?(“頭和“頭之間的真正區別是什么?和“標題標簽?)
Header/Footer Layout with 100% Content Height in IE8(IE8 中具有 100% 內容高度的頁眉/頁腳布局)
主站蜘蛛池模板: 不卡的av电影 | 亚洲第一色av | 完全免费在线视频 | 国产综合av | 成人精品系列 | 久久成人精品一区二区三区 | 在线日韩欧美 | 欧美精品一区二区三区四区五区 | 日韩在线免费视频 | 欧美日韩在线观看一区 | 欧美日韩在线不卡 | 国产日韩精品在线 | 免费观看www7722午夜电影 | 男女羞羞网站 | 亚洲视频三区 | 日韩中文字幕 | 亚洲精品性视频 | 国产一级片久久久 | 色偷偷噜噜噜亚洲男人 | 中文字幕 在线观看 | 91p在线观看 | 国产片网站 | 亚洲最大看片网站 | 奇米av| 欧美日韩一区二区三区不卡视频 | 一区二区三区在线看 | 日韩美女爱爱 | 亚洲成人第一页 | 国内精品久久久久久久 | 国产日韩一区二区 | 国产一级片免费视频 | 亚洲成av人影片在线观看 | xxxxx黄色片 欧美一区免费 | 精品国产一区二区三区久久影院 | 亚洲国产成人精品久久 | 日日爱视频 | 国产精品资源在线 | 国产在线观看不卡一区二区三区 | 99精品网 | 日韩成人一区 | 免费激情av|