Demo: jsfiddle
var $item = $('.item');
var $img = $item.find('img');
var handler = function(event) {
console.log('fired');
$img.remove(); // remove child
};
var handlerRestore = function() {
$item.append($img);
};
/// double fired event on chrome, safari, firefox
$('.item').mouseenter(handler);
$('.item').mouseleave(handlerRestore);
// one fired event on chrome, but double fired on firefox and safari
//$('.item')[0].onmouseenter = handler;
//$('.item')[0].onmouseleave = handlerRestore;
.item {
background: #EFEFEF;
display: inline;
float: left;
height: 300px;
margin: 0 10px 80px 0;
position: relative;
width: 300px;
transition: opacity 0.6s ease;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.item img {
margin: auto;
max-height: 300px;
max-width: 300px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
<img src="http://img.ifcdn.com/images/237b9b19a8cc0ea78a3b32b567423a4cd0acf77fb19b8c5a485325e2252a7b7a_1.jpg" />
</div>
Questions:
- Why event fires twice?
- Why native-event fires once in Chrome, but jquery-event fires twice in Chrome?
- How it fix?
Firefox and Safari fired twice native-event also so jquery-event. I think it is wrong.