Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

59
Views
How to only execute onmousemove function if onmousedown is active

I don't usually use vanilla js, and I simply can't make this happen. handleTilePressed is the function I want to execute, but I don't know how to make it so that onmousemove only works if onmousedown is active.

    tile.onmousemove = function() { handleTilePressed(i) }

Any help is greatly appreciated.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to create a variable that could determine it the mouse was click or not:

var isMouseDown = false;
tile.addEventListener('mousedown', e => {
  isMouseDown = true
});

window.addEventListener('mouseup', e => {
isMouseDown = false
});

Now you can use this variable to determine if you are dragging or not

tile.onmousemove = function() { 
 if(isMouseDown)
 handleTilePressed(i) 

}
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs