简体   繁体   English

关于切换开关按钮的问题

[英]Issue on toggling switch button

I am trying to create a custom switch system at this Demo with below code. 我正在尝试使用以下代码在此演示中创建自定义开关系统。

Both #left and #right are functioning perfectly but I have some issues on #sw which is supposed to toggle to left or right. #left#right都可以正常运行,但是我在#sw#sw一些问题,应该将其切换到左或右。 If user selected then click on this must shows left and switch button and so on 如果用户选择了,则单击此按钮必须显示左键和切换按钮,依此类推

 <div class="switchbox">
    <div class="btn-group switch">
        <button type="button" class="btn btn-default" id="left">Left</button>
        <button type="button" class="btn btn-default" id="sw">Switch</button>
        <button type="button" class="btn btn-default" id="right">Right</button>
    </div>
</div>  

And here is the Javascript: 这是Javascript:

var scrollWidth = 49;
var scrollWidthsw = 49;
var pos = 0;
$("p").html(pos);
$("#left").on("click", function () {
    $(".switch").animate({
        left: '-=' + scrollWidth
    }, 300);
    pos = pos - scrollWidth;
    $("p").html(pos);
});
$("#right").on("click", function () {
    $(".switch").animate({
        left: '+=' + scrollWidth
    }, 300);
    pos = pos + scrollWidth;
    $("p").html(pos);

});

$("#sw").on("click", function () {

    if (pos == 0) {
        pos = pos + scrollWidth;
        $(".switch").animate({
            left: '-=' + scrollWidthsw
        }, 300);
    } else {
        $(".switch").animate({
            left: '+=' + scrollWidthsw
        }, 300);
    }
});

Can you please let me know how to fix this? 你能让我知道如何解决这个问题吗?

Please check this updated fiddle . 请检查此更新的小提琴

I have added an extra variable toggle to store the current toggling position. 我添加了一个额外的变量toggle来存储当前的切换位置。 Is that ok? 这可以吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM