jQuery Animate text-color on hover

后端 未结 1 849
-上瘾入骨i
-上瘾入骨i 2020-12-21 13:04

Using the following code, trying to get the font color to animate on hover, similar to how I have the border-bottom-color animating. Changing the border-bottom-color works w

相关标签:
1条回答
  • 2020-12-21 13:23

    By looking at your code I can tell that you've forgotten # near css colors, so instead of this 'color': '2E9ECE' use this 'color': '#2E9ECE'. You may also want to work on your style, I have rewritten your last hover to something like this:

    $('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');
        }
    );
    

    which, in my opinion, is more readable and shorter. Take a look at jQuery API hover and animate

    UPDATE: I've verified, this code works (tested with newest versions of FireFox and 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>
    
    0 讨论(0)
提交回复
热议问题