【发布时间】:2010-09-20 13:41:35
【问题描述】:
我创建了 DIV.cb-toggle,当用户将鼠标悬停在此 div 上时,它会变为橙色,当他们将鼠标悬停在此 div 之外时,它会变为灰色,当用户单击此 div 时,它会变为灰色蓝色,告诉用户它已被选中。所以当它没有被选中时,它有 mouseenter mouseleave 动画,但是当它被选中时我想取消绑定这些事件,我不希望悬停事件在它被选中时起作用,只有在它没有被选中时才起作用。做我想要完成的事情的最佳方法是什么?我想出了下面的代码,但我很确定这是一种可怕的方法,我不知道该怎么做。非常感谢您的帮助。
我的代码:
$('.cb-toggle').toggle(function() {
$(this).animate({"background":"blue", "color":"#fff;"});
$(".cb-toggle").unbind("click.myfadee");
}, function() {
$(this).animate({"background":"gray", "color":"#fff;"});
$('.cb-toggle').trigger('mouseenter');
});
});
我称之为绑定:
$(".cb-toggle").bind("click.myfadee", function(){
$(".cb-toggle").mouseenter(function() {
$(this).animate({"background":"orange", "color":"#fff;"});
}).mouseleave(function() {
$(this).animate({"background":"gray", "color":"#fff;"});
});
});
我需要保留背景颜色动画,它需要褪色。
【问题讨论】:
-
为什么解绑,变得复杂了?如果您的点击功能是将“选定”类添加到您的 div,则您可以更改鼠标进入/离开功能以检查该类,如果存在则不更改颜色。
-
代码是正确的,所以认为你必须在jsfiddle.net 上更新一点,这样每个人都可以看到有什么问题
-
我不小心把有.css(etc)的代码贴出来了,应该是.animate(etc),bg颜色需要动画
标签: javascript jquery hover bind unbind