gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:05:17 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

表达式 !sccount%2 被解释为好像它是这样括起来的:

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

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

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

if ( sccount % 2 !== 0 ) 

关于javascript - 我有一个基于计数器的 if/else 语句,但由于某种原因,else 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34967651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com