I’m making an image track so when I click down to drag the track I got jump into the href so I cannot drag the track while I’m holding down to the image and only the outside of the track
document.getElementById("image-1").onclick = function () {
location.href = "section-1.html";
};
document.getElementById("image-2").onclick = function () {
location.href = "section-2.html";
};
document.getElementById("image-3").onclick = function () {
location.href = "section-3.html";
};
document.getElementById("image-4").onclick = function () {
location.href = "section-4.html";
};
document.getElementById("image-5").onclick = function () {
location.href = "section-5.html";
};
I tried this on image-1 and nothing happen
document.getElementById("image-1").addEventListener("contextmenu", function(event) {
event.preventDefault();
});
document.getElementById("image-1").addEventListener("click", function() {
});
I’m expecting to disable the href when hold click but it is still able to click when click once
2
Answers
By registering separate handlers for
click
anddrag
actions you can define different actions for each of them.You have two separate events:
If this implementation is not what you want you can probably look into using:
And measuring the time difference between mouse down (click) and mouse up (release). Then based on your time threshold have a different action e.g.
One way to achieve this is to use the onmousedown, onmousemove, and onmouseup events to detect when the user is dragging the image.
HTML code:
JavaScript code:
CSS code: