Fade on hover text link color

房东的猫 提交于 2019-12-06 11:12:21
Kivylius

.fadeIn() in will not fade in the text that from your .css() func. It's used with the .fadeOut() function, to show or hide a object on screen. To fade in a color change you need to use .animate() - this is a working example here using this plugin found here.

$('a').mouseover(function(){

    // #FFFFFF = color to fade | 100 = time of fading
    $(this).animate({color:'#FFFFFF'},100);
    $(this).animate({color:'#000000'},100);

}).mouseout(function(){

    // this get called when mouse leaves.
    // Set the color to default color blue        
    $(this).animate({color:'blue'},100);

    });

Note it is better to use .mouseover() / .mouseout() then use hover as it create problems (reblinking and recoloring) here and example of this bug: http://jsfiddle.net/EFgma/54/

Hope this helps.

You either need to animate the colors after you include jQuery UI (or jQuery color):

$('#menu li a').hover(
    function() { $(this).animate("color", "#FFFFFF"); },
    function() { $(this).animate("color", "#000000"); }
}

Or use CSS3 transitions (avoiding jQuery altogether):

.menu li a {
    color: #000000;
    transition: 1s color; /*animates for 1 second*/
    -moz-transition: 1s color; /*For Firefox < 16.0*/
    -webkit-transition: 1s color; /*For WebKit (Chrome, Safari)*/
}
.menu li a:hover {
    color: #FFFFFF;
}

However, CSS3 transitions' support is limited; it isn't supported in IE <= 9 (although it is supported in the soon-to-come IE10).

As always, I'd like to advocate the "progressive enhancement" approach, just because I'm anal retentive.
Don't accept this answer, as it doesn't directly answer your question.

Features that don't add functionality to a website should be carefully weighted: a color fade on the links isn't that vital, I think. So why should everyone visiting your site download jQuery + jQuery UI only for that "feature"?

Add transitions only to those who support transitions. It's also easier for you to maintain this code. It's also hardware accelerated on mobile devices.

.menu li a {
    color: #000000;
    -webkit-transition: color 1s linear;
    -moz-transition: color 1s linear;
    -ms-transition: color 1s linear;
    transition: color 1s linear;
}
.menu li a:hover {
    color: #FFFFFFF;
}

Maybe the people that use obsolete browsers (IE <= 9) will get it, that they need to update or change, if not every single site is developed "for them" and at the cost of all others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!