我有一个基于if / else语句的计数器,但由于某种原因,其他人不会工作

时间:2016-01-23 19:14:59

标签: javascript html css if-statement counter

当您尝试单击最小化按钮时,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');
    }

这是我的HTML

    <!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的新手,但我还没有在网上找到它的问题。

P.S。如果更容易查看http://l3mmydubz.onhub.online,我的网站就可以在这里找到 提前致谢, 詹姆斯

2 个答案:

答案 0 :(得分:3)

问题在于您的if语句测试表达式:

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

表达式!sccount%2被解释为如上所示括号:

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

sccount非零时,!sccount将为false。反过来,false将在0表达式中转换为%,因此除非sccount为{{1},否则答案总是为零}。

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

!==

答案 1 :(得分:0)

简单错字写了animateFN而不是animateFn&#39; facepalm&#39;感谢您的帮助,但感谢