विस्तृत जानकारी है मुद्दे के रूप में मैं समझता हूँ कि यह है कि आप निर्धारित करने के लिए अगर यह (एक खींच ऑपरेशन के लिए विंडो के बाहर दबाया गया था बाईं माउस बटन दबाया जाता है कि क्या जरूरत है)।
मुद्दों की एक संख्या हैं:
- कोई MouseMove एक खींच आपरेशन के दौरान वर्तमान बटन राज्यों के
- डेटा निकाल दिया घटनाओं देखते हैं ब्राउज़रों
मैं इस के साथ आए हैं के बीच अलग समाधान:
/**
* keeps track of the current active mouse button.
* @param notifier {Function} called when the active button is updated.
* @param oneCallPerStateChange {Boolean} (defaults to false) if true the notifier will be called only when the active button changes.
*/
function initActiveButtonWatcher(notifier, oneCallPerStateChange) {
// create variable to store button states.
var _activeButton = -1, activeButton = 0
// function to set the current active button.
function updateMouseState(e) {
// update active button.
activeButton = typeof e.buttons === "number" ? e.buttons : e.which
// notify if the active button is different.
if (oneCallPerStateChange? (_activeButton !== activeButton && typeof notifier === "function") : (typeof notifier === "function")) {
notifier(activeButton)
}
// store the last state in order to be able to tell if the state has changed.
_activeButton = activeButton
}
// function to get the current active button.
function getButtonState() { return activeButton }
// update the button state when the mouse is moved
// or an item is dragged into the window.
window.addEventListener("mousemove", updateMouseState, false)
window.addEventListener("dragover", function() { updateMouseState({which: 1}) }, false)
window.addEventListener("dragleave", function() { updateMouseState({which: 0}) }, false)
// expose the getter on the global object.
window.getButtonState = getButtonState
}
उपर्युक्त समाधान से जुड़ा हुआ है mousemove घटना, और ड्रैगओवर घटना। क्रोम ड्रैगओवर ईवेंट में सही बटन मानों के साथ एक वैध ईवेंट ऑब्जेक्ट प्राप्त करता है, हालांकि मुझे पता चला कि फ़ायरफ़ॉक्स नहीं था। मुझे लगता है कि बाएं माउस बटन की स्थिति ड्रैग ऑपरेशन के दौरान बहुत स्पष्ट है, यह दबाया जा रहा है, है ना? आप क्लिक करके और आगे बढ़ने के बिना खींच नहीं सकते हैं। तो मैं सिर्फ ड्रैगओवर पर राज्य को 1 (बाएं नीचे) सेट करता हूं।इसी तरह, ड्रैगलेव पर, मान शून्य पर सेट होता है।
आप डिफ़ॉल्ट ब्राउज़र क्रियाओं को रोकने के लिए सामान्य ड्रैगओवर, ड्रैगस्टार्ट, एंड, ड्रॉप, आदि हैंडलिंग करेंगे और window.getButtonState() द्वारा दिए गए मान को हमेशा सक्रिय माउस बटन का मान होना चाहिए।
;(function() {
var active = document.getElementsByTagName("h1")[0]
function translate(n) {
switch (n) {
case 0:
return "none"
break;
case 1:
return "left"
break;
case 2: case 4: // Firefox calls it 4, the values differ between browsers I find.
return "middle"
break;
case 3:
return "right"
}
}
initActiveButtonWatcher(
function(currentActive) {
active.innerHTML = "Active button: " + translate(currentActive)
}
)
})()
आप विभिन्न मूल्यों से निपटने के लिए कुछ फेरबदल पार ब्राउज़रों करना होगा (एफएफ:
यहां कुछ उदाहरण उपयोग है कि वर्तमान में दबाया बटन को पेज में पहली H1 टैग का मान सेट है मध्य बटन के लिए 4 की सूचना दी, आदि) लेकिन आप इस विधि के साथ वर्तमान बटन उचित रूप से अच्छी तरह से प्राप्त करने में सक्षम होना चाहिए।
इसे आजमाएं: http://jsfiddle.net/6BUA4/
बाहरी सामग्री से खींचें और छोड़ें, ऐसा लगता है कि पृष्ठ केवल डीआरओपी के बाद सक्रिय हो जाता है। –