【问题标题】:create switch with labels element使用标签元素创建开关
【发布时间】:2017-08-15 20:09:48
【问题描述】:

我想创建这样的开关,我只能从两个选项中选择一个

我可以做这个,但不知道如何覆盖它们。 .

 <button>Курьер</button><button>Самовывоз</button>

css

  button{
                margin-top: 15px; 
                width: 50%;
                height: 25px;
                border-radius: 20px;
                border: none;
                background:
                linear-gradient(to right, #2BD563, #0183D3) no-repeat; 
                color:#fff;
                .reg;
                text-transform: uppercase;
    }

谁能帮忙?

提前致谢!

【问题讨论】:

  • 使用类作为活动按钮并通过Javascript添加类。
  • @AbdullahAlemadi 彼此重叠怎么样,一个应该“高于”第二个。就我而言,它们看起来像排成一排。
  • 啊哈,我明白你想要什么了。我会回答的

标签: html css button switch-statement


【解决方案1】:

利用 div, 有一个主容器 div,里面有一个 div 用于颜色栏,还有 2 个 div 用于文本:

let pos = 0;
document.getElementById("container").onclick = function() {
  if (pos == 0)
    pos = 100;
  else
    pos = 0;
  document.getElementById("colour").style.left = pos + "px";
}
#container {
  position: absolute;
  height: 30px;
  width: 200px;
  background: grey;
  border-radius: 15px;
}

#colour {
  position: absolute;
  height: 30px;
  width: 100px;
  left: 0px;
  background: green;
  border-radius: 15px;
  transition: left 0.2s;
}

#text1 {
  position: absolute;
  height: 30px;
  width: 100px;
  left: 0px;
  text-align: center;
}

#text2 {
  position: absolute;
  height: 30px;
  width: 100px;
  right: 0px;
  text-align: center;
}
<div id="container">
  <div id="colour"></div>
  <div id="text1">hello</div>
  <div id="text2">goodbye</div>
</div>

通过控制颜色栏的“左”属性,您可以对其进行动画处理,我包含了一些快速 JS 来向您展示如何执行类似的操作。

【讨论】:

    【解决方案2】:

    您可以使用一个checkbox 在按钮之间切换。要将第一个按钮放在第二个按钮上,您可以在第二个按钮上使用否定 z-index

    .field {
      position: relative;
      display: inline-block;
      padding: 2px;
      margin: 0;
    }
    
    #check {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      opacity: 0;
      z-index: 2;
    }
    button {
      background: white;
      border: 1px solid blue;
      border-radius: 40px;
      padding: 3px 35px;
      transition: all 0.3s ease-in;
    }
    #check:checked ~ button:last-of-type,
    #check ~ button:first-of-type{
      background: blue;
      color: white;
    }
    #check:checked ~ button:first-of-type {
      background: white;
      color: black;
    }
    button:last-of-type {
      transform: translateX(-20px);
      z-index: -1;
    }
    <div class="field">
      <input type="checkbox" name="" id="check">
      <button>Курьер</button>
      <button>Самовывоз</button>
    </div>

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 1970-01-01
      • 2010-10-21
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多