1

我的小代码问题无法按我的意愿工作。我将首先发布代码。

$(function() {

$(document).ready(function(){ 
    if( $( "#skillelementwithme" ).hasClass( "skillelement_collapsed" ) )
    {
        $( "#skillelementwithme" ).click(function() {
            $( "#skillelementwithme" ).toggleClass( "skillelement_active_withme", 500 );
            $( "#skillelementwithyou" ).fadeTo( 1000, 0 );
    });
    }
    else if (( "#skillelementwithme" ).hasClass( "skillelement_active_withme" ))
    {
        $( "#skillelementwithme" ).click(function() {
            $( "#skillelementwithme" ).toggleClass( "skillelement_collapsed", 500 );
            $( "#skillelementwithyou" ).fadeTo( 1000 , 1 );
    });
    }
})
});

现在虽然淡入淡出效果非常好,但我无法让“skillelementwithyou”类淡入淡出变得不透明。也许我理解这些功能绝对有问题。我希望你们能帮助我。可能它只是很小的东西,我会恨自己。

4

1 回答 1

1

http://jsfiddle.net/isherwood/pmX7z/

$(function () {
    $("#skillelementwithme").click(function () {
        $("#skillelementwithme")
            .toggleClass("skillelement_active_withme skillelement_collapsed");
        $("#skillelementwithyou").fadeToggle();
    });
});

你的一些错误:

$(function() {并且$(document).ready(function(){基本相同,因此您可以摆脱一个。

( "#skillelementwithme" )是无效的选择器(您缺少 jQuery 对象引用$)。

包装click()内部if语句。在 JS 解析时if将被读取,返回的部分true将授予对包含.click()函数的访问权限,而其他 ( false) 将被忽略且永远无法访问。反之亦然(在处理程序中传递if逻辑click)有助于在检查每个单击事件的语句时保持事件活跃。

.toggleClass( "skillelement_collapsed", 500 );toggleClass 不接受时间参数。这不是动画。

于 2014-07-31T19:48:26.567 回答