[英]mouseover on an img, but mouseout on a div
I have a little problems with mouseover and mouseout 我在mouseover和mouseout上有一些问题
I want to use mouseover when the user put his mouse in the image (id = calendrieragenda), but mouseout only when he leave the parent div (id = divagenda), but it's don't work, when the user leave his mouse from the image, it's activate the function mouseout 我想在用户将鼠标放在图像中时使用mouseover(id = calendrieragenda),但是仅当他离开父div(id = divagenda)时将mouseout退出,但是当用户将鼠标从图片,它激活了mouseout功能
var divagenda = document.getElementById('divagenda'); var calendrieragenda = document.getElementById('imageagenda'); calendrieragenda.addEventListener('mouseover', function() { document.getElementById('divagenda').className = 'popUpAgendaMouseOver'; }); divagenda.addEventListener('mouseout', function() { document.getElementById('divagenda').className = 'popUpAgendaMouseOut'; });
#divagenda { margin-top: 1em; } #imageagenda { width: 8%; position: relative; right: 6%; margin-top: 1em; margin-bottom: 1em; transition-duration: 0.5s; transform: translateX(-50%); left: 50%; z-index: 600; } .popUpAgendaMouseOver { border-radius : 1em; border : 1px rgba(250, 250, 250, .8) solid; background-color: #444444; transition: 1s; } .popUpAgendaMouseOut { border : none; background-color:none; transition: 1s; }
<div id="divagenda"> <a href="link" title="Lien vers l'Agenda" target="_blank"> <img id="imageagenda" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRSf_HZLgiKNGwWv6V9Urtv3P2Sfo_Liw2dwOnq_oXg6-WInr_s" /> </a> </div>
I do a jsfiddle to show you my code : https://jsfiddle.net/v7pkhymm/7/ 我做了一个jsfiddle给你看我的代码: https ://jsfiddle.net/v7pkhymm/7/
Thank you very much and have a nice day ! 非常感谢您,祝您愉快!
The problem you're having is that your event is bubbling from the img up so the div will also receive that event. 您遇到的问题是您的事件正在从img冒泡,因此div也将接收该事件。
There are a couple way to prevent this. 有两种方法可以防止这种情况。 You could add an event listener at the calendrieragenda level to stopPropagation:
您可以在calendrieragenda级别添加一个事件侦听器以stopPropagation:
calendrieragenda.addEventListener('mouseout', function(event) {
event.stopPropagation();
});
Or you could check on the divagenda event listener that the target of the event is really the div: 或者,您可以检查divagenda事件侦听器,确认事件的目标确实是div:
divagenda.addEventListener('mouseout', function(event) {
if (event.target !== this) {
return;
}
document.getElementById('divagenda').className = 'popUpAgendaMouseOut';
});
I would prefer the second method as it does not create an unnecessary event listener. 我希望使用第二种方法,因为它不会创建不必要的事件侦听器。
A better approach is to use mouseleave
to avoid the bubble. 更好的方法是使用
mouseleave
避免气泡。
classList
collection to add and remove classes. classList
集合添加和删除类。 divagenda
to avoid repeated getElementById
calls. divagenda
避免重复的getElementById
调用。 var divagenda = document.getElementById('divagenda'); var calendrieragenda = document.getElementById('imageagenda'); calendrieragenda.addEventListener('mouseover', function() { divagenda.classList.add('popUpAgendaMouseOver'); }); divagenda.addEventListener('mouseleave', function() { this.classList.remove('popUpAgendaMouseOver'); this.classList.add('popUpAgendaMouseOut'); });
#divagenda { margin-top: 1em; } #imageagenda { width: 8%; position: relative; right: 6%; margin-top: 1em; margin-bottom: 1em; transition-duration: 0.5s; transform: translateX(-50%); left: 50%; z-index: 600; } .popUpAgendaMouseOver { border-radius : 1em; border : 1px rgba(250, 250, 250, .8) solid; background-color: #444444; transition: 1s; } .popUpAgendaMouseOut { border : none; background-color:none; transition: 1s; }
<div id="divagenda"> <a href="link" title="Lien vers l'Agenda" target="_blank"> <img id="imageagenda" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRSf_HZLgiKNGwWv6V9Urtv3P2Sfo_Liw2dwOnq_oXg6-WInr_s" /> </a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.