我有一个基于计数器的 if/else 语句,但由于某种原因,else 不起作用

I have a counter based if/else statement but for some reason the else won't work

本文关键字:else 语句 不起作用 由于某种原因 if 有一个 计数器      更新时间:2023-09-26

if 段有效,但无效,当您尝试单击最小化按钮时可以看到其他段,因为它会扩展但不幸的是不会收缩。

$("#scfullscreen").click(function() {
sccount+=1;
if (sccount % 2 !== 0) {
    animateFn('20%','80%');
} else {
    animateFN('0','20%');
    }
});

问题是我不知道是什么原因导致它在这里不起作用是javascript的其余部分

    var sccount = 0;
    $(document).ready(function() {
    $("#scfullscreen").click(function() {
    sccount+=1;
    if (sccount % 2 !== 0) {
        animateFn('20%','80%');
    } else {
        animateFN('0','20%');
        }
    });
    });
    function animateFn(l, w){
        $('#soundcloud').animate({
                left: l,
                width: w,
            }, 1000);
        $('#scfullscreen').animate({
                left: l,
                width: w,
            }, 1000);
        $('#scexpand').addClass('rotated');
    }

这是我的网页

    <!doctype html>
<html>
    <head>
        <title>L3mmy Dubz</title>
        <link rel="stylesheet" type="text/css" href="styles.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="jquery.js" type="text/javascript"></script>
        <script src="animation.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="header"></div>
        <a href="index.html">
            <div id="homebtn" class="btn">Home</div>
        </a>
        <div id="musicbtn" class="btn">Music</div>
        <iframe id="soundcloud" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/users/19690125&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;visual=true"></iframe>
        <div class="btn" id="scfullscreen" class="scfullscreenclick">
            <img id="scexpand" height="100%" alt="fullscreen" src="images'plus.png" />
        </div>
    </body>
</html>

这是我的 CSS

* {
    margin:0px;
    border:0px;
    padding:0px;
}
body {
    background-color:#B22800;
    height:100%;
    width:100%;
}
#header {
    position:fixed;
    left:0px;
    width:20%;
    height:100%;
    background-color: #7C1C00;
    opacity:0.9;
}
.btn {
    position:fixed;
    line-height:200%;
    text-overflow:clip;
    display: block;
    overflow: hidden;
    white-space: nowrap;
    height:7.5%;
    width:20%;
    color:white;
    Font-size:2em;
    text-align:center;
}
.btn:hover {
    background-color:#ff3a00;
}
#homebtn{
    top:0%;
}
#musicbtn{
    top:7.5%;
}
#header a {
    text-decoration:none;
    color:#fff;
}
#soundcloud {
    width:20%;
    height:77.5%;
    position:fixed;
    left:0px;
    top:15%;
}
#scfullscreen {
    bottom:0px;
    display:block;
    position:fixed;
    overflow: hidden;
    white-space: nowrap;
    height:7.5%;
    left:0px;
    width:20%;
}
#scfullscreen:hover {
    background-color:#ff3a00;
}
.rotated {
  transform: rotate(45deg);
  -ms-transform: rotate(45deg); /* IE 9 */
  -moz-transform: rotate(45deg); /* Firefox */
  -webkit-transform: rotate(45deg); /* Safari and Chrome */
  -o-transform: rotate(45deg); /* Opera */
}
@media screen and (max-width:768px) {
    #header {
        width:30%;
    }
    #soundcloud {
        width:30%;
    }
    #scfullscreen {
        width:30%;
        left:0px;
    }
}
非常感谢任何

可以提供的帮助,因为我是javascript的新手,但我还没有在网上发现它的问题。

附言我的网站可以在这里找到,如果更容易检查该 http://l3mmydubz.onhub.online提前感谢,詹姆斯

问题出在if语句测试表达式中:

if (!sccount%2 === 0) {

表达式 !sccount%2 被解释为好像它被括起来,如下所示:

if ( (!sccount) % 2 === 0)

sccount不为零时,!sccountfalse。反过来,false将在%表达式中转换为0,因此除非sccount 0,否则答案始终为零。

如果您只想根据sccount是偶数还是奇数在两个操作之间切换,则可以使用 !== 来比较结果:

if ( sccount % 2 !== 0 ) 

简单的错别字写了animateFN而不是animateFn 'facepalm' 感谢您的帮助,尽管值得赞赏