【发布时间】:2017-11-15 04:16:16
【问题描述】:
我想取消绑定并重新绑定当项目被点击时的悬停样式。
$('.merc-list img').mouseover(function(){
if ($(this).hasClass('with-grayscale')){
$(this).removeClass('with-grayscale');
}
$(this).css('transform', 'scale(' + 1.3 + ')');
$('.merc-list img').not(this).each(function(){
$(this).addClass('with-grayscale');
$(this).css('transform', 'scale(' + 0.9 + ')');
});
});
$('.merc-list img').mouseleave(function(){
if ($(this).hasClass('with-grayscale')){
$(this).removeClass('with-grayscale');
}
$(this).css('transform', 'scale(' + 1.0 + ')');
$('.merc-list img').not(this).each(function(){
if ($(this).hasClass('with-grayscale')){
$(this).removeClass('with-grayscale');
}
$(this).css('transform', 'scale(' + 1.0 + ')');
});
});
$('.merc-list img').on('click', function(){
if ($(this).hasClass('selected')){
$(this).bind('mouseover mouseleave'); // rebind for clicked image
$(this).removeClass('selected');
$(this).css('transform', 'scale(' + 1.0 + ')');
$('.merc-list img').not(this).each(function(){
$(this).bind('mouseover mouseleave'); // rebind for every other image except clicked image
$(this).removeClass('with-grayscale');
$(this).css('transform', 'scale(' + 1.0 + ')');
});
}else{
$(this).unbind('mouseover mouseleave'); // unbind for clicked image
if ($(this).hasClass('with-grayscale')){
$(this).removeClass('with-grayscale');
}
$(this).addClass('selected');
$(this).css('transform', 'scale(' + 1.3 + ')');
$('.merc-list img').not(this).each(function(){
$(this).unbind('mouseover mouseleave'); // unbind for every other image except clicked image
$(this).addClass('with-grayscale');
if ($(this).hasClass('selected')){
$(this).removeClass('selected');
}
$(this).css('transform', 'scale(' + 0.9 + ')');
});
}
});
当我将鼠标悬停在它上面时,所有其他图像都会缩小并变成灰色,并且悬停在上面的元素会变大并且没有灰度(在本例中我将鼠标悬停在 Phantom 上)
现在,当我单击 Phantom 时,我想冻结每个雇佣兵上的每个mouseover 和mouseleave。 但是,例如,当我取消点击 Phantom 时,我想取消冻结所有内容并返回到原来的悬停动画。
正如您在我的代码中看到的那样,我正在解除绑定和重新绑定 mouseover 和 mouseenter 功能,但它没有重新绑定(它不会回到原来的悬停状态动画)。
感谢您的帮助!
【问题讨论】: