Jquery:小的绑定悬停/取消绑定代码片段,几行长,不知道出了什么问题
我创建了 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;"});
});
});
我需要保留背景颜色动画,它需要淡入淡出。
I've created DIV.cb-toggle, when the user hovers over this div, it animates to Orange, when they hover off of this div, it animates back to gray, when the user clicks this div, it animates to blue, telling the user that it's been selected. So when it's NOT selected, it has mouseenter mouseleave animations, but when it's selected i want to unbind these events, I DO NOT want the hover event to work when it's been selected, only when it's not selected. What's the best way to do what i'm trying to accomplish? I came up with the code below but i'm pretty sure this is a horrible way to do it and i don't know what to do. thank you so much for any help.
my code:
$('.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');
});
});
and I'm calling this bind:
$(".cb-toggle").bind("click.myfadee", function(){
$(".cb-toggle").mouseenter(function() {
$(this).animate({"background":"orange", "color":"#fff;"});
}).mouseleave(function() {
$(this).animate({"background":"gray", "color":"#fff;"});
});
});
I need to keep the background color animation, it needs to fade.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用 CSS 进行样式设置,以简化整个设置,而无需取消/重新绑定,如下所示:
然后您可以这样做:
这种方法还可以让您将所有样式、颜色等卸载到 CSS,这意味着无需更改 JavaScript当您决定调整颜色或任何其他样式时需要:)
或者,如果您需要支持 IE6,请添加
.live()
处理程序,仅在具有.active
类的悬停时触发,如下所示: CSS:
I would use CSS for the styling to simplify your whole setup without un/re-binding, like this:
Then you can do just this:
This approach also lets you offload all styling, colors, etc to the CSS, meaning no JavaScript changes are needed when you decide to tweak the colors or any other styling :)
Or, if you need to support IE6, add a
.live()
handler for the hover that triggers on only the ones with the.active
class, like this:With matching CSS:
您可能应该只使用选定的类。另外,我建议不要使用此处的任何 .css() 调用。只需使用类即可。
You should probably just use a selected class. Also I'd recommend against using any of the .css() calls you have here. Just use classes.