Safari和IE / Edge中的复选框ON / OFF开关

时间:2018-05-22 05:18:01

标签: html css

在Chrome中,firefox,歌剧,勇敢,好吧,在Safari中 - 过渡没有好处。 在Edge,Internet Esplorer - 外观不起作用,看起来像复选框 如何在Edge / IE浏览器中重复safari的过渡和渲染?

.mytoggle {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: 80px;
  height: 30px;
  display: inline-block;
  position: relative;
  border-radius: 15px;
  overflow: hidden;
  outline: none;
  border: none;
  cursor: pointer;
  background-color: #bdc3c7;
  -webkit-transition: .25s ease-in-out;
  -moz-transition: .25s ease-out;
  -o-transition: .25s ease-out;
  transition: .25s ease-out;
}

.mytoggle:before {
  width: 22px;
  height: 22px;
  left: 4px;
  top: 4px;
  border-radius: 11px;
  z-index: 2;
  content: "ON OFF";
  display: block;
  position: absolute;
  background: #7f8c9a;
  font-size: 14.99px;
  font-weight: 700;
  text-transform: uppercase;
  text-indent: -32px;
  word-spacing: 40px;
  color: #fff;
  white-space: nowrap;
  -webkit-transition: .25s ease-out;
  -moz-transition: .25s ease-out;
  -o-transition: .25s ease-out;
  transition: .25s ease-out;
}

.mytoggle:checked {
  background-color: #2A3542;
}

input.mytoggle:focus {
  outline: 0;
}

.mytoggle:checked:before {
  background-color: #41cac0;
  color: #41cac0;
  left: 54px;
}
<input type="checkbox" checked="checked" class="mytoggle" style="" />

3 个答案:

答案 0 :(得分:1)

直接设置复选框样式很麻烦,您可以使用复选框+标签黑客,在其中将标签链接到复选框,并将样式应用于标签而不是复选框本身:

.mytoggle {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  top: -99999px;
}

.mytoggle + label {
  font-family: arial;
  vertical-align: middle;
  width: 80px;
  height: 30px;
  display: inline-block;
  position: relative;
  border-radius: 15px;
  overflow: hidden;
  outline: none;
  border: none;
  cursor: pointer;
  background-color: #bdc3c7;
  -webkit-transition: .25s ease-in-out;
  -moz-transition: .25s ease-out;
  -o-transition: .25s ease-out;
  transition: .25s ease-out;
}

.mytoggle + label:before {
  width: 22px;
  height: 22px;
  left: 4px;
  top: 4px;
  border-radius: 11px;
  z-index: 2;
  content: "ON OFF";
  display: block;
  position: absolute;
  background: #7f8c9a;
  font-size: 14.99px;
  font-weight: 700;
  text-transform: uppercase;
  text-indent: -32px;
  word-spacing: 40px;
  color: #fff;
  white-space: nowrap;
  -webkit-transition: .25s ease-out;
  -moz-transition: .25s ease-out;
  -o-transition: .25s ease-out;
  transition: .25s ease-out;
}

.mytoggle:checked + label {
  background-color: #2A3542;
}

input.mytoggle:focus + label {
  outline: 0;
}

.mytoggle:checked + label:before {
  background-color: #41cac0;
  color: #41cac0;
  left: 54px;
}
<input type="checkbox" checked="checked" class="mytoggle" id="mycheckbox" />
<label for="mycheckbox" />

答案 1 :(得分:1)

&#13;
&#13;
input { opacity:0}
input+label {
  width: 80px;
  height: 30px;
  display: inline-block;
  position: relative;
  border-radius: 15px;
  overflow: hidden;
  outline: none;
  border: none;
  cursor: pointer;
  background-color: #bdc3c7;
  -webkit-transition: .25s ease-in-out;
  -moz-transition: .25s ease-out;
  -o-transition: .25s ease-out;
  transition: .25s ease-out;
}

input+label:before {
  width: 22px;
  height: 22px;
  left: 4px;
  top: 4px;
  border-radius: 11px;
  z-index: 2;
  content: "ON OFF";
  display: block;
  position: absolute;
  background: #7f8c9a;
  font-size: 14.99px;
  font-weight: 700;
  text-transform: uppercase;
  text-indent: -32px;
  word-spacing: 40px;
  color: #fff;
  white-space: nowrap;
  -webkit-transition: .25s ease-out;
  -moz-transition: .25s ease-out;
  -o-transition: .25s ease-out;
  transition: .25s ease-out;
}

input:checked+label{
  background-color: #2A3542;
}

input.mytoggle:focus {
  outline: 0;
}

input:checked+label:before {
  background-color: #41cac0;
  color: #41cac0;
  left: 54px;
}
&#13;
<input type="checkbox" name="abc" id="abc" checked="checked"  style="" />
<label for="abc" class="mytoggle"></label>
&#13;
&#13;
&#13;

我刚添加了一个输入复选框的标签,在Internet Explorer 11中测试过。我没有Safari浏览器来测试它,所以如果这个答案符合您的要求,您可以测试并告知。

答案 2 :(得分:1)

请试试这个。

&#13;
&#13;
	$(document).ready(function(e) {
		$(".ra-box").click(function(){
        	$(this).toggleClass('checked');
		});
    });
&#13;
input[type=checkbox] { opacity: 0; }
.ra-box { 
	display: inline-block;
	margin: 0;
	padding: 6px;
	cursor: pointer;
	border-radius: 15px;
	overflow: hidden;
    background: #2a3542;
    width: 70px;
    height: 18px;
	position: relative;
}
.ra-box:before {
	height: 20px;
	width: 20px;
	background: #41cac0;
	color: #41cac0;
	content: 'ON';
	text-indent: -40px;
	position: absolute;
	top: 5px;
	right: 5px;
	border-radius: 50%;
}
.ra-box:after {
	height: 20px;
	width: 20px;
	background: #7f8c9a; 
	color: #fff;	
	content: 'OFF';
	position: absolute;
	top: 5px;
	border-radius: 50%;
	text-indent: 30px;
	left: 85px;
}

/* Check Box Check Change background color */
.ra-box.checked:before { right: 85px; }
.ra-box.checked:after { left: 5px;}
.ra-box.checked { background: #bdc3c7; }

.ra-box,.checked,.ra-box.checked,.ra-box:before,.ra-box:after,.ra-box.checked:before,.ra-box.checked:after {
	-webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
   -ms-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ra-box">
	<input type="checkbox" id="toggle-1">
</div>

<br>
<br>

<div class="ra-box">
	<input type="checkbox" id="toggle-1">
</div>
&#13;
&#13;
&#13;