【问题标题】:jQuery fadeColor problemsjQuery 褪色问题
【发布时间】:2009-11-11 09:27:59
【问题描述】:

您好,我是 JQuery 的新手。我有两个我无法弄清楚的问题。由于时间紧迫,我正在使用复制和过去的代码。

1)当我将鼠标悬停在链接上时,一旦我将鼠标从链接上移开,它就不会变回原来的颜色。

2) 如果我在链接上快速移动鼠标,它们会卡在一个循环中并一遍又一遍地淡入淡出......我知道我应该能够使用 stop() 但是不知道这是否是我需要的。

// JavaScript Document
    $(document).ready(function() {
    //Grab the original BG color of the link
    var originalBG = $("#nav li a").css("background-color"); 
    //The color you want to fade too
var fadeColor = "#FFFFFF"; 

//Now animate on links with class = animate
$("#nav li a").hover( 
    function() { 
        $(this)
            //Fade to the new color
            .animate({backgroundColor:fadeColor}, 350)
            //Fade back to original color
            .animate({backgroundColor:originalBG}, 350) 
        }, 
    function(){

        }
    );
});

更新:来自建议 - 解决了我的一些问题,但现在有时如果您将鼠标悬停在链接上,它不会消失。

// JavaScript Document
$(document).ready(function() {
//Grab the original BG color of the link
var originalBG = "#351411"; 
//The color you want to fade too
var fadeColor = "#FFFFFF"; 

//Now animate on links with class = animate
$("#nav li a").hover( 
    function() { 
                //Fade to the new color
                $(this).stop().animate({backgroundColor:fadeColor}, 350)
        }, 
    function(){
                //Fade back to original color
                $(this).stop().animate({backgroundColor:originalBG}, 350) 
        }
    );
});

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    不起作用,因为 .css("background-color") 以另一种颜色格式返回,例如:“rgb(18, 52, 86)”。

    【讨论】:

    • @Soufiane Hassou 的解决方案可能有效。但我不知道“动画”是否接受这种格式:“rgb(18, 52, 86)”。试试 ;)
    【解决方案2】:

    试试这个:

    $("#nav li a").hover( 
        function() { 
    
                    //Fade to the new color
                    $(this).animate({backgroundColor:fadeColor}, 350)
    
            }, 
        function(){
                    //Fade back to original color
                    $(this).animate({backgroundColor:originalBG}, 350) 
            }
        );
    

    【讨论】:

    • 这有点帮助。现在,有时如果您将鼠标悬停在链接上,它不会淡入或淡出。
    • “有时”是指当您快速进出鼠标时,不是吗?
    • 也许设置超时并在鼠标移出时清除它会解决您的问题。
    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 2011-06-29
    • 2011-06-24
    相关资源
    最近更新 更多