【发布时间】:2020-09-05 21:29:37
【问题描述】:
我有一个要登录的按钮列表,并希望为它们分配一个事件侦听器以实现 p5.js 悬停效果。我想将它分配给包装器,但是在按钮之间切换时,它不会检测到悬停。
我也在使用 Vue.js v2.6.11
<div class="main">
<div class="google hover">
<button @click="loginWithProvider('google')">
Login with Google
</button>
</div>
<div class="twitter hover">
<button @click="loginWithProvider('twitter')">
Login with Twitter
</button>
</div>
<div class="github hover">
<button @click="loginWithProvider('github')">
Login with GitHub
</button>
</div>
</div>
以及事件监听器:
第一个版本
const main = document.querySelector('.main');
main.addEventListener('mouseenter', () => {
isHovered = true;
});
main.addEventListener('mouseout', () => {
isHovered = false;
});
第二版:
const buttons = document.querySelectorAll('.hover');
buttons.forEach(button => {
button.addEventListener('mouseenter', () => {
isHovered = true;
});
});
buttons.forEach(button => {
button.addEventListener('mouseout', () => {
isHovered = false;
});
});
两者的效果相同。输入,但是一旦我停止将鼠标悬停在一个按钮上并转到另一个按钮(留在父级div,mouseout 被触发并且没有再次输入其他按钮(除非我离开父级div)
【问题讨论】:
-
developer.mozilla.org/en-US/docs/Web/API/Element/… 好吧,就您的第一个 sn-p 而言,mouseenter 事件不会冒泡。因此,从这个意义上说,将事件侦听器放在父 main 上是有问题的。
-
嗯,我想到了。但是为什么当我为每个按钮添加一个监听器时它不起作用呢?当然它应该这样工作......
标签: javascript html css vue.js vue-component