【问题标题】:Possibly better way to code this hacky toggle button?编写这个 hacky 切换按钮的更好方法可能是什么?
【发布时间】:2016-03-30 15:48:21
【问题描述】:

我有一个已编码的切换按钮,但我认为在我的表单中使用它并不好,因为它是一个非常糟糕的 hacky 代码来选择任一选项。

是否有更好/更有效的方法来编写此切换按钮?我不擅长 jQuery,所以对所提供功能的任何帮助都会有所帮助。

如果还有一种编程方式来向左/向右滑动切换开关而不是单击左/右,那也很棒。

我还附上了这些图片来展示它应该如何运作的行为:
toggle behaviour diagram
current html file(below) button look for left/right toggle buttons

有什么问题,请追问……

<html>
<head>
  <style>
    #toggle-slide {
      border: 4px #303F9F solid;
      border-radius: 5px;
      display: flex;
      width:300px;
      color: white;
      font-weight: 700;
      text-align: center;
      cursor: pointer;
    }
    #toggle-slide div {
      flex:1;
      padding: 10px 20px; 
    }
    #toggle-option-0 {
      background-color:#3F51B5;
    }
    #toggle-option-1 {
      background-color:white;
    }
  </style>  

  <script>
    function toggle() {
      realToggle = document.getElementById('real-toggle');
      if (realToggle.value == 0) {
        realToggle.value=1;
        document.getElementById('toggle-option-0').style.backgroundColor='#3F51B5';
        document.getElementById('toggle-option-1').style.backgroundColor='#FFF';
      } else {
        realToggle.value=0;
        document.getElementById('toggle-option-0').style.backgroundColor='#FFF';
        document.getElementById('toggle-option-1').style.backgroundColor='#3F51B5';        
      }
    }
  </script>
</head>  

<body>
  <div id='toggle-slide' onclick='toggle()'>
    <div id='toggle-option-0' class='active'>Private</div>
    <div id='toggle-option-1'>Public</div>
    <input id='real-toggle' type=hidden name=private value=1 />
  </div>
</body>
</html>

【问题讨论】:

标签: javascript html css


【解决方案1】:

纯 CSS 版本:

在下面的 sn-p 中,有一个隐藏的复选框,当单击 label 中的内容时,该复选框会被选中/取消选中。使用 CSS :checked 选择器,#background 位置从 0% 更改为 50%,颜色从红色变为蓝色。

背景与文本分离,并用position:absolute(便于移动)加上z-index:-1(将其置于字幕后面)设置。在 #background 上添加的 CSS transition 为其位置/颜色的更改设置动画。

.toggle-slide {
      border: 4px #555 solid;
      border-radius: 5px;
      display: flex;
      width: 300px;
      color: white;
      font-weight: 700;
      text-align: center;
      cursor: pointer;
      position: relative;
      -webkit-touch-callout: none; /* iOS Safari */
      -webkit-user-select: none;   /* Chrome/Safari/Opera */
      -khtml-user-select: none;    /* Konqueror */
      -moz-user-select: none;      /* Firefox */
      -ms-user-select: none;       /* IE/Edge */
      user-select: none; 
}

.toggle-slide .subtitle {
      flex: 1;
      padding: 10px 20px; 
}

#background {
      width: 50%;
      height: 100%;
      top: 0;
      left: 0;
      position: absolute;
      z-index: -1;
      background-color: tomato;
      -webkit-transition: all 0.6s; /* Safari */
      transition: all 0.6s;
      -webkit-transition-timing-function: cubic-bezier(0.2,1,0.2,1);
      transition-timing-function: cubic-bezier(0.2,1,0.2,1);
    }

input[type=checkbox] {
      display: none; 
}

#real:checked ~ label #background {
      background-color: skyblue;
      left: 50%;  
}
<input id=real type=checkbox name=real />
<label class=toggle-slide for=real>
    <div id=background></div>
    <div class=subtitle>Private</div>
    <div class=subtitle>Public</div>  
</label>

【讨论】:

  • 投了赞成票,但如果您可以添加一些解释其工作原理的文字会很棒,例如浏览器要求等。
  • 问题现已更新;感谢您的关注;
【解决方案2】:

你完全可以在纯 CSS 中做到这一点,但既然你要求的是 jQuery...

$(document).ready(function() {
  $('.input-button').click(function() {
    if ($('.public').hasClass('selected')) {
      $('.public').removeClass('selected');
      $('.private').addClass('selected');
      $('.slider').stop().animate({
        left: '48%'
      }, 200);
    } else {
      $('.private').removeClass('selected');
      $('.public').addClass('selected');
      $('.slider').stop().animate({
        left: '2%'
      }, 200);
    }
  });
});
html,
body {
  padding: 0;
  margin: 0;
}
.input-button {
  width: 200px;
  height: 40px;
  top: 50%;
  left: 50%;
  margin-left: -100px;
  margin-top: -20px;
  position: absolute;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  color: #FFF;
  background-color: #2E86AB;
  border-radius: 4px;
  line-height: 40px;
  font-family: sans-serif;
  -webkit-box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.2);
  box-shadow: 0px 2px 0px 0px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}
span {
  width: 50%;
  height: 100%;
  float: left;
  text-align: center;
  cursor: pointer;
  -webkit-user-select: none;
}
.input-button div {
  width: 100px;
  height: 85%;
  top: 50%;
  left: 2%;
  transform: translateY(-50%);
  position: absolute;
  background-color: #FFF;
  border-radius: 4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='input-button'>
  <div class='slider'></div>
  <span class='private'>Private</span>
  <span class='public selected'>Public</span>
</div>

【讨论】:

  • 这看起来很棒 Devplex,你对纯 CSS 有什么建议?我一直在寻找纯 CSS 的解决方案
  • 如果可能的话,这也应该适用于所有浏览器
  • @freestock.tk 提供了一个很好的纯 CSS 答案。既然你提到了它,只是认为你可以在 jQuery 中使用它。我通过为其他浏览器添加伪元素对其进行了一些修改,应该是跨浏览器兼容的。您可以删除定位并根据需要更改颜色。那些只是为了一些眼睛糖果:)
  • 您的解决方案没有使按钮与我的表单组内联,如何使按钮内联?
  • 去掉当前定位,在.input-button中添加display:inline-block
【解决方案3】:

这是您尝试创建的一个很好的示例

jQuery on-off-switch.js Plugin

它也是用 jQuery 实现的,并且支持滑动拖拽功能。

How to use the plugin

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    相关资源
    最近更新 更多