javascript - 分配给父 div 的事件侦听器在按钮上停止
问题描述
我有一个要登录的按钮列表,并希望为它们分配一个事件侦听器以获得 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)
解决方案
事实证明,添加边距会触发mouseenter
&mouseout
事件。我刚刚添加了一个这样的间隔 div:
<div class="main">
<div class="google hover">
<button @click="loginWithProvider('google')">
<i class="fab fa-google"></i>
Login with Google
</button>
</div>
<div class="spacer"></div>
<div class="twitter hover">
<button @click="loginWithProvider('twitter')">
<i class="fab fa-twitter"></i>
Login with Twitter
</button>
</div>
<div class="spacer"></div>
<div class="github hover">
<button @click="loginWithProvider('github')">
<i class="fab fa-github"></i>
Login with GitHub
</button>
</div>
</div>
并编辑了我的 CSS:
// Removed
.button {
margin-buttom: 10px;
}
// Added
.spacer {
height: 10px;
}
推荐阅读
- python - 如何部分回滚?
- c - 将用户的二进制输入转换为双精度值时出错
- javascript - plotly typescript 的导出成员中缺少 maxdpeth
- pdf - 以编程方式标记 PDF 以实现可访问性
- java - Firebase Firestore 为存在的密钥提供 null pinter 异常
- c++ - 使用函数c ++打印结构指针
- math - 2个二进制补码二进制数之和
- reactjs - 如何避免从另一个 useEffect() 触发 useEffect()?
- java - HttpSessionStorage - 在 SESSION 中找不到消息
- linux - 在 docker 桌面 WSL2 中使用 /dev/random 性能下降