mousedown和click沖突事件


鼠標事件,一般用button來區分鼠標的按鍵(DOM3標准規定: click事件只能監聽左鍵, 只能通過mousedown和mouseup來判斷鼠標鍵):

1.鼠標左鍵 button = 0

2.鼠標右鍵 button = 2

3.鼠標滑輪 button = 1 

div.onmousedown = function (e) {
    var event = e || window.event;
    if(event.button == 2){
        console.log('right');
    }else if(event.button == 0){
        console.log('left');
    }
}

解決mousedown和click的之間的沖突  (利用事件發生時間來判斷 點擊事件時間短)

var key = false;//設置了一個標志 false為點擊事件 ture為鼠標移動事件
var firstTime = 0;
var lastTime = 0;
div.onclick = function() {
    if(key){
        console.log('click');
        key = false;
    }
}
div.onmousedown = function() {
    firstTime = new Date().getTime();
    console.log('mouseDown');
}
div.onmouseup = function() {
    console.log('mouseUp');
//鼠標抬起后 記錄時間 超過200ms就是移動事件 lastTime = new Date().getTime(); if( (lastTime - firstTime)
< 200){ key = true; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM