EN
JavaScript - on mouse wheel click event
0 points
In this article, we would like to show you on mouse wheel click event in JavaScript.
Quick solution:
xxxxxxxxxx
1
function addWheelClickedEventListener(element, callback) {
2
var started = false;
3
var removed = false;
4
var onDocumentMouseup = function(e) {
5
started = false;
6
};
7
var onElementMousedown = function(e) {
8
e.preventDefault(); // prevents auto-scrolling action
9
started = (e.button === 1);
10
};
11
var onElementMouseup = function(e) {
12
if (started) {
13
started = false;
14
callback(e);
15
}
16
};
17
document.addEventListener('mouseup', onDocumentMouseup);
18
element.addEventListener('mousedown', onElementMousedown);
19
element.addEventListener('mouseup', onElementMouseup);
20
return function() {
21
if (removed) {
22
return;
23
}
24
removed = true;
25
document.removeEventListener('mouseup', onDocumentMouseup);
26
element.removeEventListener('mousedown', onElementMousedown);
27
element.removeEventListener('mouseup', onElementMouseup);
28
};
29
}
30
31
32
// Usage example:
33
34
var element = ;
35
36
addWheelClickedEventListener(element, function(e) {
37
console.log('button clicked with mouse wheel');
38
});
In this example, to handle mouse wheel click we create a function that adds three event listeners:
mouseup
- to thedocument
, so it can cancel mouse scroll click when we accidentally click on the unwanted area,mousedown
- to the buttonelement
to handle scroll click on the button,mouseup
- to the buttonelement
to add the actual callback function that we want to execute on scroll click.
Additionally, we provide a mechanism that helps to remove all the event listeners when not needed.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<button id="element">Click me with mouse wheel</button>
5
<script>
6
7
function addWheelClickedEventListener(element, callback) {
8
var started = false;
9
var removed = false;
10
var onDocumentMouseup = function(e) {
11
started = false;
12
};
13
var onElementMousedown = function(e) {
14
e.preventDefault(); // prevents auto-scrolling action
15
started = (e.button === 1);
16
};
17
var onElementMouseup = function(e) {
18
if (started) {
19
started = false;
20
callback(e);
21
}
22
};
23
document.addEventListener('mouseup', onDocumentMouseup);
24
element.addEventListener('mousedown', onElementMousedown);
25
element.addEventListener('mouseup', onElementMouseup);
26
return function() {
27
if (removed) {
28
return;
29
}
30
removed = true;
31
document.removeEventListener('mouseup', onDocumentMouseup);
32
element.removeEventListener('mousedown', onElementMousedown);
33
element.removeEventListener('mouseup', onElementMouseup);
34
};
35
}
36
37
38
// Usage example:
39
40
var element = document.querySelector('#element');
41
42
var removeEventListener = addWheelClickedEventListener(element, function(e) {
43
console.log('button clicked with mouse wheel');
44
});
45
46
// removeEventListener() helps to remove wheel clicked event listener when not needed
47
48
</script>
49
</body>
50
</html>