Toggle button Using pure css

空扰寡人 提交于 2019-12-08 05:57:27

问题


I have a toggle button which I am trying using a checkbox and pure css. The end result I am trying to get is something like this https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_switch

But on trying it from different sources I ended up doing it something like this

http://jsfiddle.net/8wb570ma/34/

.slide-btn-alt .slide-btn-content .slide-btn-handle {
  position: static;
  width: 50%;
  border-radius: 0;
  background: none;
  text-align: center;
  -webkit-transform: translateX(0);
  -ms-transform: translateX(0);
  transform: translateX(0);
}

The problem I am facing is that I am not able to translate the direction, What I checked on the w3schools shows On/checked with text on left and slider on right whereas Off/unChecked with text on right and slider on left. Whereas what I ended up making shows entirely opposite behavior.

Is this because my button is like?

OFF||Handle||On


回答1:


If I understand correctly what you want, then you can do with a slight variant of the W3Schools method.

To show what's going on, here is a bare-bones version with minimal changes from the original:

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: attr(data-off);
  line-height:26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  content: attr(data-on);
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}
<label class="switch">
  <input type="checkbox">
  <span class="slider" data-off="Off" data-on="On"></span>
</label>

And with some more CSS to make it look more like your example:

.slide-btn-alt {
  position: relative;
  display: inline-block;
  width: 108px;
  height: 34px;
}

.slide-btn-alt input {display:none;}

.slide-btn-content {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #2b99d6;
  text-transform:uppercase;
  -webkit-transition: .3s;
  transition: .3s;
}

.slide-btn-content::before {
  position: absolute;
  content: attr(data-off);
  font: 900 10px/34px 'Montserrat-Bold', sans-serif;
  color: white;
  width: 50%;
  left: 0;
  bottom: 0;
  text-align: center;
  -webkit-transition: .3s;
  transition: .3s;
}

.slide-btn-content::after {
  position: absolute; content: '';
  height: 34px;
  left: 49%;
  bottom: 0;
  border-left: 2px solid rgba(238, 238, 239, 0.2);
}

input:checked + .slide-btn-content {
  background-color: #485679;
}

input:focus + .slide-btn-content {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slide-btn-content::before {
  content: attr(data-on);
  -webkit-transform: translateX(54px);
  -ms-transform: translateX(54px);
  transform: translateX(54px);
}
<label class="slide-btn-alt">
  <input type="checkbox">
  <span class="slide-btn-content" data-off="Off" data-on="On"></span>
</label>


来源:https://stackoverflow.com/questions/52269432/toggle-button-using-pure-css

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!