Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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语句';在jQuery中_Javascript_Jquery_Css_If Statement - Fatal编程技术网

Javascript 通过'切换动画;If语句';在jQuery中

Javascript 通过'切换动画;If语句';在jQuery中,javascript,jquery,css,if-statement,Javascript,Jquery,Css,If Statement,我在jQuery中有一些代码,我想通过单击一个div使开关动画化。当我测试它时,它不起作用。但是,如果我删除第7行的toggle=true,它只会以一种方式工作,我无法将其关闭 $(document).ready(function () { $("#switch").click(function () { var toggle = false; if (toggle == false) { $("#circle").css("lef

我在jQuery中有一些代码,我想通过单击一个div使开关动画化。当我测试它时,它不起作用。但是,如果我删除第7行的
toggle=true
,它只会以一种方式工作,我无法将其关闭

$(document).ready(function () {
    $("#switch").click(function () {
        var toggle = false;
        if (toggle == false) {
            $("#circle").css("left", "27px");
            $("#white_rect").attr("src", "green_rect.png");
            toggle = true;
        }
        if (toggle == true) {
            $("#circle").css("left", "1px");
            $("#white_rect").attr("src", "white_rect.png");
            toggle = false;
        }
    });
});

您需要在click处理程序外部声明
toggle
变量。。。否则,在每次单击调用中,变量将重新初始化,因此变量的值将始终为false

$(document).ready(function () {
    //declare it here
    var toggle = false;
    $("#switch").click(function () {
        if (toggle == false) {
            $("#circle").css("left", "27px");
            $("#white_rect").attr("src", "green_rect.png");
            toggle = true;
        //also don't use a separate if block here as it will be affected by the execution of the above if block
        } else {
            $("#circle").css("left", "1px");
            $("#white_rect").attr("src", "white_rect.png");
            toggle = false;
        }
    });
});


您需要在click处理程序外部声明
toggle
变量。。。否则,在每次单击调用中,变量将重新初始化,因此变量的值将始终为false

$(document).ready(function () {
    //declare it here
    var toggle = false;
    $("#switch").click(function () {
        if (toggle == false) {
            $("#circle").css("left", "27px");
            $("#white_rect").attr("src", "green_rect.png");
            toggle = true;
        //also don't use a separate if block here as it will be affected by the execution of the above if block
        } else {
            $("#circle").css("left", "1px");
            $("#white_rect").attr("src", "white_rect.png");
            toggle = false;
        }
    });
});


最好严格区分外观和逻辑。因此,使用
.classes
和backgounded
s代替
。然后,您将不需要任何状态变量,代码将更加简单

HTML

<div class="container">
    <div class="switch off"></div>
</div>
.container {
    width:100%; height:100px; padding:40px; text-align:center;
}
.container .switch {
    width:94px; height: 27px; display:inline-block; background-color:pink; cursor:pointer;
}
.container .switch.on {
    background: url('http://squad.pw/tmp/img/01-on.png') no-repeat 0 0;
}
.container .switch.off {
    background: url('http://squad.pw/tmp/img/01-off.png') no-repeat 0 0;
}
$('.switch').click(function() {

    // Do visual logic
    $(this).toggleClass('on');
    $(this).toggleClass('off');

    // Do business logic
    window.toggle = !window.toggle;
});
JS

<div class="container">
    <div class="switch off"></div>
</div>
.container {
    width:100%; height:100px; padding:40px; text-align:center;
}
.container .switch {
    width:94px; height: 27px; display:inline-block; background-color:pink; cursor:pointer;
}
.container .switch.on {
    background: url('http://squad.pw/tmp/img/01-on.png') no-repeat 0 0;
}
.container .switch.off {
    background: url('http://squad.pw/tmp/img/01-off.png') no-repeat 0 0;
}
$('.switch').click(function() {

    // Do visual logic
    $(this).toggleClass('on');
    $(this).toggleClass('off');

    // Do business logic
    window.toggle = !window.toggle;
});

这里是

最好严格区分外观和逻辑。因此,使用
.classes
和backgounded
s代替
。然后,您将不需要任何状态变量,代码将更加简单

HTML

<div class="container">
    <div class="switch off"></div>
</div>
.container {
    width:100%; height:100px; padding:40px; text-align:center;
}
.container .switch {
    width:94px; height: 27px; display:inline-block; background-color:pink; cursor:pointer;
}
.container .switch.on {
    background: url('http://squad.pw/tmp/img/01-on.png') no-repeat 0 0;
}
.container .switch.off {
    background: url('http://squad.pw/tmp/img/01-off.png') no-repeat 0 0;
}
$('.switch').click(function() {

    // Do visual logic
    $(this).toggleClass('on');
    $(this).toggleClass('off');

    // Do business logic
    window.toggle = !window.toggle;
});
JS

<div class="container">
    <div class="switch off"></div>
</div>
.container {
    width:100%; height:100px; padding:40px; text-align:center;
}
.container .switch {
    width:94px; height: 27px; display:inline-block; background-color:pink; cursor:pointer;
}
.container .switch.on {
    background: url('http://squad.pw/tmp/img/01-on.png') no-repeat 0 0;
}
.container .switch.off {
    background: url('http://squad.pw/tmp/img/01-off.png') no-repeat 0 0;
}
$('.switch').click(function() {

    // Do visual logic
    $(this).toggleClass('on');
    $(this).toggleClass('off');

    // Do business logic
    window.toggle = !window.toggle;
});

这里是

你也能提供你的HTML吗?还是小提琴链接?你也能提供你的HTML吗?还是小提琴链接?