【问题标题】:jQuery Animate text-color on hover悬停时的jQuery动画文本颜色
【发布时间】:2012-06-16 14:57:51
【问题描述】:

使用以下代码,尝试让字体颜色在悬停时进行动画处理,类似于我的边框底部颜色动画处理方式。更改边框底部颜色效果很好,但字体颜色似乎不会改变。一个完整的例子可以在这里看到:http://www.buenolisto.com/alma。任何帮助是极大的赞赏。我也已经在调用 jQuery UI:https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.js

jQuery("li.social").hover(function() {
jQuery(this).find("img").stop(true, true).animate({
    'marginTop': "-=20px"
}, 'fast');
}, function() {
jQuery(this).find("img").stop(true, true).animate({
    'marginTop': "+=20px"
}, 'fast');
})
jQuery("li.reservas").hover(function() {
jQuery(this).find("img").stop(true, true).fadeOut({
    'marginTop': "-=30px"
}, 'slow');
}, function() {
jQuery(this).find("img").stop(true, true).fadeIn({
    'marginTop': "+=30px"
}, 'slow');
})
jQuery("ul.menu li").hover(function() {
jQuery(this).find("a").stop(true, true).animate({
    'borderBottomColor': '#2E9ECE',
    'color': '2E9ECE'
}, 'slow');
}, function() {
jQuery(this).find("a").stop(true, true).animate({
    'borderBottomColor': '#FFDF85',
    'color': 'FFDF85'
}, 'slow');
})​

【问题讨论】:

    标签: jquery fonts colors hover jquery-animate


    【解决方案1】:

    通过查看您的代码,我可以看出您在 css 颜色附近忘记了 #,因此请使用此 'color': '2E9ECE' 而不是此 'color': '#2E9ECE' 。您可能还想改进您的风格,我已将您上次的悬停改写为:

    $('ul.menu li a').hover(
        function() {
            // do this on hover
            $(this).animate({
                'borderBottomColor': '#2E9ECE',
                'color': '#2E9ECE'
            }, 'slow');
        }, 
        function() {
            // do this on hover out
            $(this).animate({
                'borderBottomColor': '#FFDF85',
                'color': '#FEFEFE'
            }, 'slow');
        }
    );
    

    在我看来,它更具可读性和更短。看看 jQuery API hover 和 animate

    更新:我已验证,此代码有效(已使用最新版本的 FireFox 和 Chrome 进行测试):

    <html>                                                                  
    <head>                                                                  
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script> 
    <script type="text/javascript">                                         
        $(function() {
            $("a").hover(
                function() {
                    $(this).animate({ color: "#00ff00" }, 'slow');
                },function() {
                    $(this).animate({ color: "#ff0000" }, 'slow');
            });
        });
    </script>                                                               
    </head>                                                                 
    <body>                                                                  
        <a href="#">aaa</a><br />
        <a href="#">bbb</a><br />
    </body>                                                                 
    </html>
    

    【讨论】:

    • 我尝试了如下代码,但似乎仍然存在一些问题。颜色正在改变,但不是我如何预测它们我希望字体颜色在悬停时动画为#2E9ECE,并在未悬停时恢复为#FEFEFE。我是一个真正的新手,所以请多多包涵:) jQuery('ul.menu li a').hover(function() { jQuery(this).stop(true, true).animate({' borderBottomColor': '#2E9ECE','color': '#2E9ECE'}, 'slow'); }, function() { jQuery(this).stop(true, true).animate({'borderBottomColor': '# FFDF85','color': '#FEFEFE'}, 'slow'); } );
    • 我已经编辑了我的帖子以匹配你的颜色,这很简单,只需在 API 上循环。
    • 我遇到的问题是我第一次将鼠标悬停在一个元素上,动画将不会启动……但如果我第二次将鼠标悬停在该元素上,动画就会执行。然后我遇到了当前菜单项也没有显示正确颜色的问题。
    • 不,它没有工作......行为都是错误的。加载页面后我第一次将鼠标悬停在一个元素上什么都不会发生。然后再次将鼠标悬停在元素上会产生所需的效果。同样,我的 current-menu-item CSS 规则也坏了。不过,我仍然想让这个工作。看看 API,它就超出了我的想象。 :(
    • @user1447958 我明白了,重新阅读我更新的答案,它应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2018-06-29
    • 1970-01-01
    • 2011-11-22
    • 2016-03-26
    • 1970-01-01
    相关资源
    最近更新 更多