jQuery fadeIn CSS 类

发布于 2024-11-19 10:33:16 字数 611 浏览 7 评论 0原文

我正在尝试对某些具有“button”类的对象执行简单的 fadeIn() CSS 类。我想在悬停时淡入“hoverbutton”类,然后在鼠标离开该项目时淡出。

我在问题中发现了这一点。它似乎工作得很好,直到我注意到当我将鼠标悬停在多个按钮上时,有些按钮很快就会卡在“悬停按钮”类上。不知道如何解决这个问题。任何建议都会很棒。

$('.button').hover(function(){
    $(this).addClass('hoverbutton', 200);
}, function(){
    $(this).removeClass('hoverbutton', 200);
});

当我将鼠标悬停在一个项目上并在第一个项目的淡入淡出完成之前快速跳转到该类的另一个项目时,它们似乎被卡住了。

stop()ing 产生相同的结果。悬停类仍然卡住

$('.button').hover(function(){
  $(this).stop().addClass('hoverbutton', 200);
}, function(){
  $(this).stop().removeClass('hoverbutton', 200);
});

I'm trying to do simple fadeIn() CSS class on certain objects that have the "button" class. I want to fadeIn the "hoverbutton" class on hover and then fadeOut when their mouse leaves the item.

I found this in the questions. It seemed to work well until i noticed when i hover over multiple buttons quickly some get stuck on the "hoverbutton" class. Not a clue how to fix that. Any suggestions would be great.

$('.button').hover(function(){
    $(this).addClass('hoverbutton', 200);
}, function(){
    $(this).removeClass('hoverbutton', 200);
});

it seems they get stuck when i hover over one and quickly jump to another item with that class before the fading on the first one is complete.

stop()ing produced the same result. hover class still gets stuck

$('.button').hover(function(){
  $(this).stop().addClass('hoverbutton', 200);
}, function(){
  $(this).stop().removeClass('hoverbutton', 200);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

知足的幸福 2024-11-26 10:33:16

问题是因为 jQueryUI 使用 style 属性来执行动画,如果它没有完成(因为悬停在悬停完成之前发生),则它的动画类不会实际上已添加,因此无法通过悬停来删除。

要解决此问题,我们需要在悬停时执行两件事:

  • 停止 addClass() 动画,
  • 删除由 addClass() 动画创建的任何临时样式,

就像这样。这

$('.button').hover(function() {
    $(this)
        .addClass('hoverbutton', 200);
}, function() {
    $(this).stop()
        .attr("style", "")
        .removeClass('hoverbutton', 200);
});

是一个示例:http://jsfiddle.net/RBTT8/

The problem is because jQueryUI is using the style property to do the animation, and if it doesn't complete (because the hover-out occurs before hover-in finishes) the class it's animating to isn't actually added, and so can't be removed by the hover-out.

To fix this, we need to do two things on the hover-out:

  • stop the addClass() animation
  • remove any temporary style created by the addClass() animation

Like so..

$('.button').hover(function() {
    $(this)
        .addClass('hoverbutton', 200);
}, function() {
    $(this).stop()
        .attr("style", "")
        .removeClass('hoverbutton', 200);
});

Here's an example: http://jsfiddle.net/RBTT8/

旧伤慢歌 2024-11-26 10:33:16

尝试停止动画队列:

$('.button').hover(function(){
    $(this).stop().addClass('hoverbutton', 200);
}, function(){
    $(this).stop().removeClass('hoverbutton', 200);
});

Try stopping the animation queue:

$('.button').hover(function(){
    $(this).stop().addClass('hoverbutton', 200);
}, function(){
    $(this).stop().removeClass('hoverbutton', 200);
});
你不是我要的菜∠ 2024-11-26 10:33:16

查看 stop() 的文档。您可以将其添加到您的呼叫中,如下所示:

$('.button').hover(function(){
  $(this).stop().addClass('hoverbutton', 200);
}, function(){
  $(this).stop().removeClass('hoverbutton', 200);
});

Check out the documentation for stop(). You would add it to your call like so:

$('.button').hover(function(){
  $(this).stop().addClass('hoverbutton', 200);
}, function(){
  $(this).stop().removeClass('hoverbutton', 200);
});
故人的歌 2024-11-26 10:33:16

实现相同效果的另一种方法是使用 CSS3 过渡,一切都支持Internet Explorer 除外(最高版本 10)。

这是一个示例: http://jsfiddle.net/3yr8G/

An alternative approach to achieve the same effect would be to use CSS3 Transitions, supported in everything except Internet Explorer (up to version 10).

Here's an example: http://jsfiddle.net/3yr8G/

毁我热情 2024-11-26 10:33:16

尝试使用 CSS add --

    ex)--styles take 0.5s to appear        transition: all 0.5s ease-in-out;

到您需要淡入

w3schools 过渡链接的元素:
http://www.w3schools.com/css/css3_transitions.asp

Try using CSS add --

    ex)--styles take 0.5s to appear        transition: all 0.5s ease-in-out;

to the element u need to fade in

w3schools transition link:
http://www.w3schools.com/css/css3_transitions.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文