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

為什么 event.clientX 在 firefox 中為 dragend 事件錯誤

Why is event.clientX incorrectly showing as 0 in firefox for dragend event?(為什么 event.clientX 在 firefox 中為 dragend 事件錯誤地顯示為 0?)
本文介紹了為什么 event.clientX 在 firefox 中為 dragend 事件錯誤地顯示為 0?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

來自 dragend 的警報將?? mouseX 顯示為零,無論它當前在哪里.這在 Chrome 中運行良好,所以不確定我做錯了什么.

The alert from dragend is showing mouseX as zero no matter where it is currently. This works fine in Chrome so not sure what I'm doing wrong.

function move(e,obj,but){
    if(typeof(obj) === 'string'){
        obj = document.getElementById(obj) ;
    }
    
    if(typeof(but) === 'string'){
        but = document.getElementById(but) ;
    }

    //elementCoord(but) ;//get the current coords of the button &
    elementCoord(obj) ;//the container
    
    e = e || window.event ;
    var mouseX = e.clientX ;
    var mouseY = e.clientY ;
            
    //alert('mouseX='+mouseX+', but.XCoord '+but.XCoord) ;
    var diffX = Math.abs(obj.XCoord - mouseX) ;
    var diffY = Math.abs(obj.YCoord - mouseY) ;
    
    but.addEventListener("dragend",function(evt){
        evt = evt || window.event ;
        mouseX = evt.clientX ;
        mouseY = evt.clientY ;
        obj.style.left = mouseX - diffX + 'px';
        obj.style.top = mouseY - diffY + 'px';
        alert('mouseX='+mouseX+' diffX='+diffX) ;
        }
    ,false) ;
    
}

忘了說,elementCoord 只是獲取對象的偏移量,將其添加為屬性.它適用于所有瀏覽器.

Forgot to mention, elementCoord just gets the offset of an object adding it as a property. It works fine in all browsers.

推薦答案

這是 Firefox 的官方問題 -- Bugzilla:錯誤 #505521,在 HTML5 拖動事件期間設置屏幕坐標.我會引用 jbmj 來總結一下,我會加粗他們引用的原始開發人員...

This is officially an issue with Firefox -- Bugzilla: Bug #505521, Set screen coordinates during HTML5 drag event. I'll quote jbmj to summarize, and I will bold the original developer they are quoting...

我不敢相信這個評論
"請注意,雖然它沒有指定屬性應該設置為什么,只是應該設置它們,而我們目前將它們設置為 0."
從 11 年前開始,它仍然是最先進的.

I can't believe that this comment
"Note though that it doesn't specify what the properties should be set to, just that they should be set and we currently set them to 0."
from 11years ago is still state of the art.

Jay 的評論啟發了我,我使用了drop";事件.但這只是一個評論,所以讓我把它打造成一個答案.

I was inspired by Jay's comment, to use the "drop" event. But that was only a comment, so let me thresh it out into an answer.

我們的問題:dragend 事件將 e.clientYe.clientX 設置為 0.

Our problem: dragend event has e.clientY and e.clientX set to 0.

我們將如何解決它:documentdrop 事件 也觸發 與我們拖動的元素的 dragend 事件.并且:drop 將具有正確的 e.clientYe.clientX 值.

How we will solve it: document's drop event also fires at the same exact time as the element we are dragging's dragend event. And: drop will have the correct values for e.clientY and e.clientX.

兩個工作演示,100% 僅 JavaScript 的解決方案:SO 代碼片段和 JSBin.SO Code Snippet 控制臺有時會吞噬控制臺中拖動的元素,而 JSBin 給了我更一致的結果.

Two working demos, 100% JavaScript-Only Solution: SO Code Snippet and JSBin. The SO Code Snippet console sometimes gobbles up the dragged element in the console, and JSBin gave me more consistent results.

var startx = 0;
var starty = 0;
dragStartHandler = function(e) {
  startx = e.clientX;
  starty = e.clientY;
}

dragOverHandler = function(e) {
  e.preventDefault();
  return false;
}

dragEndHandler = function(e) {
  if(!startx || !starty) {
    return false;
  }
  
  var diffx = e.clientX - startx;
  var diffy = e.clientY - starty;
  
  var rect = e.target.getBoundingClientRect();

var offset = { 
                top: rect.top + window.scrollY, 
                left: rect.left + window.scrollX, 
            };
  
  var newleft = offset.left + diffx;
  var newtop = offset.top + diffy;
  
  e.target.style.position = 'absolute';
  e.target.style.left = newleft + 'px';
  e.target.style.top = newtop + 'px';
  
  startx = 0;
  starty = 0;
}

document.getElementsByClassName("draggable")[0].addEventListener('dragstart', dragStartHandler);

document.addEventListener('dragover', dragOverHandler);
document.addEventListener('drop', dragEndHandler);

.draggable {
  border: 1px solid black;
  cursor: move;
  width:250px;
};

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  <BR><BR><BR>

  <div id="draggable1" class="draggable" draggable="true">
    Hey, try to drag this element!
  </div>
  
</body>
</html>

說明:

  • dragStartHandler() :綁定到可拖動元素.在這里,我們所做的只是在開始時記錄當前的 x/y 坐標.
  • dragOverHandler() :這是綁定到文檔的,這樣我們就可以覆蓋默認的拖動行為.這是進行任何類型的拖動和下降.
  • dragEndHandler() :綁定到 documentdrop.通常,我們希望 this 綁定到 elementdragend,但由于缺少 clientYclientX,我們將它綁定到文檔.這正是您在調用 dragend 時想要發生的事情,除非您有 x/y 坐標.
  • dragStartHandler() : This is bound to the draggable element. Here, all we do is record the current x/y coordinates at start.
  • dragOverHandler() : This is bound to the document, so that we can override the default dragover behavior. This is needed to do any type of drag & dropping.
  • dragEndHandler() : This is bound to the document's drop. Normally, we would want this to bind to the element's dragend, but since clientY and clientX are missing, we bind it to the document. This just does exactly what you'd want to happen when dragend is called, except you have x/y coordinates.

這篇關于為什么 event.clientX 在 firefox 中為 dragend 事件錯誤地顯示為 0?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 国产免费一区二区三区 | 一级看片免费视频囗交动图 | 999精彩视频 | 亚洲精品国产第一综合99久久 | 久久一视频 | 欧美成视频在线观看 | 91精品在线播放 | 亚洲伊人久久综合 | 日日噜噜噜夜夜爽爽狠狠视频, | 日韩在线不卡 | www.五月天婷婷.com | 国产男人的天堂 | 亚洲女人天堂成人av在线 | 免费在线成人网 | 成人在线精品 | 午夜爽爽爽男女免费观看 | 国产精品免费大片 | 天天操网| 国产 欧美 日韩 一区 | 精品一区在线 | 日韩精品在线一区 | 波多野结衣精品在线 | 国产精品免费播放 | 亚洲在线视频 | 欧美日韩免费一区二区三区 | 涩涩视频网站在线观看 | 国产在线观看一区二区三区 | 欧美精品在线一区 | 夜夜爽99久久国产综合精品女不卡 | 国产激情视频网 | 色网在线播放 | 9porny九色视频自拍 | 久久99精品久久久 | 亚洲三级国产 | 国产精品久久久久久久久婷婷 | 亚洲欧美日韩在线不卡 | 日本一区二区三区在线观看 | 精品成人一区二区 | 五月香婷婷 | 在线观看国产wwwa级羞羞视频 | 中文字幕亚洲区 |