Thuộc tính sự kiện onmousedown kích hoạt khi người dùng nhấn xuống nút chuột trên một phần tử (chưa thả ra).
Cú pháp
<tagname onmousedown="script"></tagname>
Ví dụ
<!DOCTYPE html>
<html>
<head>
<style>
body { color: #000; height: 100vh; background-color: #FBAB7E; background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%); text-align: center; }
.circle { background: #db133a; height: 150px; width: 150px; border-radius: 50%; margin: 10px auto; transition: transform 0.1s; }
p { margin: 30px auto; }
</style>
</head>
<body>
<h1>Demo onmousedown / onmouseup</h1>
<div class="circle" onmousedown="mouseDown()" onmouseup="mouseUp()"></div>
<p>Nhấn chuột xuống trên vòng tròn đỏ</p>
<script>
function mouseDown() {
document.querySelector('.circle').style.transform = 'scale(0.8)';
}
function mouseUp() {
document.querySelector('.circle').style.transform = 'scale(1)';
}
</script>
</body>
</html>
Kết quả
Nhấn giữ chuột trên vòng tròn → thu nhỏ (scale 0.8). Thả ra → khôi phục.
Thứ tự: mousedown → mouseup → click. Dùng mousedown cho hành động "nhấn giữ" (drag, game, long-press). event.button cho biết nút nào: 0=trái, 1=giữa, 2=phải.