本文整理常見滑鼠互動效果,包含右鍵停用、游標樣式、移入移出、自動捲動、連結色彩與滑輪停用。
document.onmousedown = function(event){
if(event.button == 2){
alert('已停用滑鼠右鍵');
}
};
event.button 可用來區分左鍵、右鍵與中鍵組合。
pDom.style.cursor = 'pointer';
cursor 可設定 default、pointer、move、help、text、progress、resize 等值。
function mOver(el){ el.style.fontSize = '20px'; }
function mOut(el){ el.style.fontSize = ''; }
var timer;
document.ondblclick = function(){
timer = setInterval(function(){ window.scrollBy(0, 1); }, 10);
};
document.onmousedown = function(){ clearInterval(timer); };
linkDOMs[i].onmouseover = function(){
this.style.color = 'red';
};
linkDOMs[i].onmouseout = function(){
this.style.color = '';
};
document.onmousewheel = function(){
alert('禁止使用滑輪');
return false;
};
這些效果多用於增強互動,實際專案仍可搭配事件監聽與樣式類別,讓程式碼更易維護。