如何使用Javascript/CSS创建开/关开关?

如何使用Javascript/CSS创建开/关开关?,javascript,html,css,Javascript,Html,Css,我想要一个滑动开关。左边是关的,右边是开的。当用户切换开关时,我希望“滑块”部分滑到另一侧,并指示它已关闭。然后我可以有一个回调,它将切换开关的状态作为输入,这样我就可以相应地进行操作 知道怎么做吗?使用普通javascript <html> <head> <!-- define on/off styles --> <style type="text/css"> .on { background:blue;

我想要一个滑动开关。左边是关的,右边是开的。当用户切换开关时,我希望“滑块”部分滑到另一侧,并指示它已关闭。然后我可以有一个回调,它将切换开关的状态作为输入,这样我就可以相应地进行操作


知道怎么做吗?

使用普通javascript

<html>

  <head>

     <!-- define on/off styles -->
     <style type="text/css">
      .on  { background:blue; }
      .off { background:red; }
     </style>

     <!-- define the toggle function -->
     <script language="javascript">
        function toggleState(item){
           if(item.className == "on") {
              item.className="off";
           } else {
              item.className="on";
           }
        }
     </script>
  </head>

  <body>
     <!-- call 'toggleState' whenever clicked -->
     <input type="button" id="btn" value="button" 
        class="off" onclick="toggleState(this)" />
  </body>

</html>

使用jQuery UI效果,可以设置过渡动画:

大纲:创建两个元素:滑块/开关和槽作为滑块的父元素。要切换状态,请在“开”和“关”类之间切换滑块元素。在一个类的样式中,将“left”设置为0,将“right”设置为默认值;对于另一个类,执行相反的操作:

<style type="text/css">
.toggleSwitch {
    width: ...;
    height: ...;
    /* add other styling as appropriate to position element */
    position: relative;
}
.slider {
    background-image: url(...);
    position: absolute;
    width: ...;
    height: ...;
}
.slider.on {
    right: 0;
}
.slider.off {
    left: 0;
}
</style>
<script type="text/javascript">
function replaceClass(elt, oldClass, newClass) {
    var oldRE = RegExp('\\b'+oldClass+'\\b');
    elt.className = elt.className.replace(oldRE, newClass);
}
function toggle(elt, on, off) {
    var onRE = RegExp('\\b'+on+'\\b');
    if (onRE.test(elt.className)) {
        elt.className = elt.className.replace(onRE, off);
    } else {
        replaceClass(elt, off, on);
    }
}
</script>
...
<div class="toggleSwitch" onclick="toggle(this.firstChild, 'on', 'off');"><div class="slider off" /></div>

.切换开关{
宽度:。。。;
高度:。。。;
/*根据需要添加其他样式以定位图元*/
位置:相对位置;
}
.滑块{
背景图片:url(…);
位置:绝对位置;
宽度:。。。;
高度:。。。;
}
{
右:0;
}
.slider.off{
左:0;
}
函数替换类(elt、旧类、新类){
var oldRE=RegExp('\\b'+oldClass+'\\b');
elt.className=elt.className.replace(oldRE,newClass);
}
功能切换(elt、on、off){
var onRE=RegExp('\\b'+on+'\\b');
if(在线测试(英语课程名称)){
elt.className=elt.className.replace(开、关);
}否则{
替换类(elt、关闭、打开);
}
}
...

或者,只需将背景图像设置为“开”和“关”状态,这比摆弄定位要容易得多。

你是指类似IPhone复选框的东西吗? 试一试

一旦文件可用于您的站点,激活脚本就非常容易:

$(document).ready(function() {
  $(':checkbox').iphoneStyle();
});
结果:


检查此发电机:

你可以得到各种不同风格的结果和它的css只-没有javascript

2013年的初步答复 如果您不介意与引导相关的内容,那么这是一个非常好的(非官方的)

它使用无线电类型或复选框作为开关。自V.1.8以来,添加了
类型
属性

源代码是

2018年的说明 我不建议现在就使用那些老式的开关按钮,因为它们似乎总是被许多人指向

请考虑看一下。


您可以使用HTML和CSS实现这一点,并将复选框转换为HTML开关

HTML


您可以查看Shield UI的。它很容易使用,如下所示:

<input id="switch3" type="checkbox" value="" />

<script>
  jQuery(function ($) {
    $("#switch3").shieldSwitch({
        onText: "Yes, save it",
        ffText: "No, delete it",
        cls: "large"
    });
  });
</script>

jQuery(函数($){
$(“#开关3”).屏蔽开关({
“是的,保存它”,
ffText:“不,删除它”,
cls:“大型”
});
});

漂亮。我没有iphone,所以我不知道这叫什么为生成带有动画转换的纯CSS3开/关翻转开关提供的服务。值得注意的是,它不是免费用于商业用途的。不确定在开放近5年后,这个问题如何过于宽泛。它清楚地勾勒出了我在寻找什么,并在同一天得到了完美回答这个问题的回复。这个问题仍然有很多活动,在提出这个问题4.5年后,最新的回答被发布。我写了关于如何只使用CSS(还有复选框和收音机)创建开/关开关的文章。链接重定向到无法加载的位置。使用(与www一起使用;感谢SO在我的链接中直观地隐藏了www),而不是使用加载。每次我必须进行切换时,我都使用这种方法。只是一个带有标签和CSS的简单复选框。
      <div class="switch">
        <input id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round"  type="checkbox">
        <label for="cmn-toggle-1"></label>
      </div>
input.cmn-toggle-round + label {
  padding: 2px;
  width: 100px;
  height: 30px;
  background-color: #dddddd;
  -webkit-border-radius: 30px;
  -moz-border-radius: 30px;
  -ms-border-radius: 30px;
  -o-border-radius: 30px;
  border-radius: 30px;
}
input.cmn-toggle-round + label:before, input.cmn-toggle-round + label:after {
  display: block;
  position: absolute;
  top: 1px;
  left: 1px;
  bottom: 1px;
  content: "";
}
input.cmn-toggle-round + label:before {
  right: 1px;
  background-color: #f1f1f1;
  -webkit-border-radius: 30px;
  -moz-border-radius: 30px;
  -ms-border-radius: 30px;
  -o-border-radius: 30px;
  border-radius: 30px;
  -webkit-transition: background 0.4s;
  -moz-transition: background 0.4s;
  -o-transition: background 0.4s;
  transition: background 0.4s;
}
input.cmn-toggle-round + label:after {
  width: 40px;
  background-color: #fff;
  -webkit-border-radius: 100%;
  -moz-border-radius: 100%;
  -ms-border-radius: 100%;
  -o-border-radius: 100%;
  border-radius: 100%;
  -webkit-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  -moz-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  -webkit-transition: margin 0.4s;
  -moz-transition: margin 0.4s;
  -o-transition: margin 0.4s;
  transition: margin 0.4s;
}
input.cmn-toggle-round:checked + label:before {
  background-color: #8ce196;
}
input.cmn-toggle-round:checked + label:after {
  margin-left: 60px;
}

.cmn-toggle {
  position: absolute;
  margin-left: -9999px;
  visibility: hidden;
}
.cmn-toggle + label {
  display: block;
  position: relative;
  cursor: pointer;
  outline: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<input id="switch3" type="checkbox" value="" />

<script>
  jQuery(function ($) {
    $("#switch3").shieldSwitch({
        onText: "Yes, save it",
        ffText: "No, delete it",
        cls: "large"
    });
  });
</script>