Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript if/else语句不起作用,但是里面的代码起作用,正如我在没有if/else的情况下测试的那样_Javascript_Html_Css_If Statement_Width - Fatal编程技术网

Javascript if/else语句不起作用,但是里面的代码起作用,正如我在没有if/else的情况下测试的那样

Javascript if/else语句不起作用,但是里面的代码起作用,正如我在没有if/else的情况下测试的那样,javascript,html,css,if-statement,width,Javascript,Html,Css,If Statement,Width,基本上,我尝试创建一个if-else语句来检查div的宽度,然后根据结果来扩展或收缩内部的iframe。它们在运行代码本身时没有问题,只运行if/else语句,因为我在没有if/else的情况下测试了代码 网址为 代码: Javascript: var sc = document.getElementById("scfullscreen"); var sccount = 0; $(document).ready(function() { $(sc).click(function() { scco

基本上,我尝试创建一个if-else语句来检查div的宽度,然后根据结果来扩展或收缩内部的iframe。它们在运行代码本身时没有问题,只运行if/else语句,因为我在没有if/else的情况下测试了代码

网址为

代码:

Javascript:

var sc = document.getElementById("scfullscreen");
var sccount = 0;
$(document).ready(function() {
$(sc).click(function() {
sccount= sccount+1;
if (sccount%2 === 0) {
    animateFn('20%','80%');
    $('#soundcloud').animate({
            left: '20%',
            width: '80%',
        }, 1000);

} 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:


也可以考虑将代码晾干

var sc = document.getElementById("scfullscreen");
$(document).ready(function() {
$('#scfullscreen').click(function() {
    var l= parseInt(sc.style.left,10);
    if (l > 0) {
        animateFN('0%','20%');

    } else {
        animateFn('20%','80%');
        });
    }
});
});
});

 function animateFn(l, w){
        $('#soundcloud').animate({
                left: l,
                width: w,
            }, 1000);
        $('#scfullscreen').animate({
                left: l,
                width: w,
            }, 1000);
        $('#scexpand').addClass('rotated');
}

请提供足够的代码,包括基本html/css,以重现此问题。
style.left
的值不是一个数字,而是一个字符串,如
“100px”
,您不能使用条件。您应该首先将其强制转换为整数:
if(parseInt(sc.style.left,10)>0)
感谢您的帮助非常感谢并对charliefl的问题表示抱歉我已经更新了我的问题谢谢您现在就尝试:)由于某些原因,它仍然不起作用对不便表示抱歉我更改了它,因此它检查了宽度,而这是我已启用的站点,因此可以对其进行检查l3mmydubz.onhub.onlinetry just var l=parseInt(sc.style.left);如果(l>0){
* {
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;
}
}
var sc = document.getElementById("scfullscreen");
$(document).ready(function() {
$('#scfullscreen').click(function() {
    var l= parseInt(sc.style.left,10);
    if (l > 0) {
        animateFN('0%','20%');

    } else {
        animateFn('20%','80%');
        });
    }
});
});
});

 function animateFn(l, w){
        $('#soundcloud').animate({
                left: l,
                width: w,
            }, 1000);
        $('#scfullscreen').animate({
                left: l,
                width: w,
            }, 1000);
        $('#scexpand').addClass('rotated');
}