简体   繁体   English

仅当 mousedown && mouseover 为真时触发的事件侦听器

[英]event listener that fires only when mousedown && mouseover are true

I have found many similar questions and answers like mine.我发现了很多类似我的问题和答案。 Unfortunatly, I have tried to adapt those answers to fit my problem but I cannot make it work.不幸的是,我试图调整这些答案来解决我的问题,但我无法让它发挥作用。 I am making a drawing pad application.我正在制作绘图板应用程序。 The drawing pad is made of a container full of divs.绘图板由一个装满 div 的容器组成。 I want the color to change only when mousedown and mouseover are both true.我希望只有当mousedownmouseover都为真时颜色才会改变。 basically, I want to click and drag my drawing lines.基本上,我想点击并拖动我的绘图线。 Im probably way out to lunch here but I am at my whits end trying things.我可能会出去吃午饭,但我已经迫不及待想尝试了。

 let boxes = document.getElementsByClassName("grid-item"); let isClicked = false; let isHover = false; for (box of boxes) { box.addEventListener("mousedown", () => isClicked = true); } for (box of boxes) { box.addEventListener("mouseover", () => isHover = true); } function draw() { if (isClicked == true && isHover == true) { box.target.style.background_color = "black"; } }

Instead of trying to manage and track global variables, you can use the event object passed.您可以使用传递的事件 object,而不是尝试管理和跟踪全局变量。 In my example, I have a function which can make the event target black, by changing the background color.在我的示例中,我有一个 function,它可以通过更改背景颜色使事件目标变为黑色。 I did use the style.backgroundColor because style.background-color is incorrect for js.我确实使用了style.backgroundColor因为style.background-color对于 js 是不正确的。 If the mousedown event is triggered, you can safely assume the cursor is over the element.如果触发了 mousedown 事件,您可以安全地假设 cursor 在元素上。 The mouseover event does not always make the target black.鼠标悬停事件并不总是使目标变黑。 Instead, I check the event property .buttons which will be 1 if the left mousebutton is pressed, and 0 if not.相反,我检查事件属性.buttons如果按下鼠标左键则为 1,否则为 0。 This way, I can verify if it is clicked, then we just pass the event along to our make black function.这样,我可以验证它是否被点击,然后我们将事件传递给我们的 make black function。

It is possible to solve this problem by tracking global variables, however you would need to be doing a lot of "state managing" which can quickly become messy and complex.可以通过跟踪全局变量来解决这个问题,但是您需要进行大量的“状态管理”,这很快就会变得混乱和复杂。 You would need a mouseout function that turns the isHover off, as well as a mouseup function that turns the isClick off.您需要一个关闭isHover的 mouseout function,以及一个关闭isClick的 mouseup function。 You would probably need some debouncing in case the mouseout or mouseup event didn't trigger due to a right click or the cursor being in a different position. Then you would also need to track which element isHover and isClick which is redundant in this case because we can just check the event object from our mouseover event.如果由于右键单击或 cursor 处于不同的 position 而未触发 mouseout 或 mouseup 事件,您可能需要一些去抖动。然后您还需要跟踪哪个元素isHoverisClick在这种情况下是多余的,因为我们可以从鼠标悬停事件中检查事件 object。

 const boxes = [...document.getElementsByClassName("grid-item")]; const makeBlack = event => event.target.style.backgroundColor = "black"; function equip(box) { box.addEventListener("mousedown", makeBlack); box.addEventListener("mouseover", event => { if (event.buttons == 1) makeBlack(event); }); } boxes.forEach(equip);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM