iOS喜欢使用复选框的切换按钮,不同的浏览器看起来都不一样

时间:2023-01-19 17:05:46

I am kinda new to web designing. Trying to create a toggle iOS like button but the issue is with its appearance on different browsers. It looks different in different browsers. As I tested it it works properly on Chrome.

我对网页设计有点陌生。试图创建一个类似于iOS的切换按钮,但问题在于它在不同浏览器上的外观。它在不同的浏览器中看起来不同。当我测试它时,它在Chrome上正常工作。

Can somebody help me to make it look same on all browsers? Sorry for the broken english?

有人能帮我把它在所有浏览器上看起来都一样吗?对不起,我的英语出了问题。

jsfiddle Link : http://jsfiddle.net/racy2408/Let1y8yr/

jsfiddle链接:http://jsfiddle.net/racy2408/Let1y8yr/

Given below is CSS I tried:

下面是我尝试过的CSS:

body {
  margin: 30px;
  text-align: center;
}
input[type=checkbox],
input[type=radio] {
  display: inline-block;
  width: 50px;
  height: 30px;
  position: relative;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  -webkit-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  -o-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
  border-radius: 15px;
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  -o-border-radius: 15px;
  background-color: #fff;
  padding: 1px;
  margin: 0px;
  transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transform: scale(1);
  -webkit-transform: scale(1);
  /*Adjust size here*/
  -moz-transform: scale(1);
  -o-transform: scale(1);
}
input[type=checkbox]:checked,
input[type=radio]:checked {
  box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
  -webkit-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
  -moz-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
  -o-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
}
input[type=checkbox]:after,
input[type=radio]:after {
  content: " ";
  position: absolute;
  width: 28px;
  height: 28px;
  cursor: pointer;
  border-radius: 15px;
  -webkit-border-radius: 15px;
  -moz-border-radius: 15px;
  -o-border-radius: 15px;
  box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  -webkit-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  -o-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
  background-color: #fff;
  /* background-image: url(../images/toggle.png);
	background-repeat: no-repeat;
	background-size: contain; */
  transition: all 0.2s ease-in 0.2s;
  -webkit-transition: all 0.2s ease-in 0.2s;
  -moz-transition: all 0.2s ease-in 0.2s;
  -o-transition: all 0.2s ease-in 0.2s;
}
input[type="checkbox"]:checked:after,
input[type="radio"]:checked:after {
  left: 23px;
}
input[type=checkbox]:focus,
input[type=checkbox]:active,
input[type=radio]:focus,
input[type=radio]:active {
  outline: 0;
}
<h1>Checkbox :</h1> 
<input type="checkbox" />
<input type="checkbox" checked />

<h1>Radio :</h1>

<input type="radio" name="group" />
<input type="radio" name="group" checked />
<input type="radio" name="group" />

1 个解决方案

#1


3  

inputs aren't supposed to be able to add before and after psuedo-elements because they aren't containers (e.g. an <input> can't have child elements).

输入不能在psuedo元素之前和之后添加,因为它们不是容器(例如,不能有子元素)。

There is some more information in the comment here: https://*.com/a/4660434/4338477

这里有更多的信息:https://*.com/a/4660434/4338477

You could add labels to get the same effect though (this works in Firefox and IE9+):

你可以添加标签来获得同样的效果(这在Firefox和IE9+中都是有效的):

http://jsfiddle.net/8okjwLxx/2/

http://jsfiddle.net/8okjwLxx/2/

<h1>Checkbox :</h1> 

<input id="checkbox1" type="checkbox" />
<label for="checkbox1"></label>
<input id="checkbox2" type="checkbox" checked />
<label for="checkbox2"></label>

<h1>Radio :</h1>

<input id="radio1" type="radio" name="group" />
<label for="radio1"></label>

<input id="radio2" type="radio" name="group" checked />
<label for="radio2"></label>

<input id="radio3" type="radio" name="group" />
<label for="radio3"></label>
body {
    margin: 30px;
    text-align: center;
}

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

input[type=checkbox] + label,
input[type=radio] + label {
    display: inline-block;
    width: 50px;
    height: 30px;
    position: relative;
    cursor: pointer;
    appearance:         none;
    -webkit-appearance: none;
    -moz-appearance:    none;
    box-shadow:         inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -webkit-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -moz-box-shadow:    inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -o-box-shadow:      inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    border-radius:         15px;
    -webkit-border-radius: 15px;
    -moz-border-radius:    15px;
    -o-border-radius:      15px;
    background-color: #fff;
    padding: 1px;
    margin: 0px;
    transition:         all 0.3s ease-out;
    -webkit-transition: all 0.3s ease-out;
    -moz-transition:    all 0.3s ease-out;
    -o-transition:      all 0.3s ease-out;  
    transform:         scale(1);
    -webkit-transform: scale(1); /*Adjust size here*/
    -moz-transform:    scale(1);
    -o-transform:      scale(1);
}

input[type=checkbox]:checked + label,
input[type=radio]:checked + label {
    box-shadow:         inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -webkit-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -moz-box-shadow:    inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -o-box-shadow:      inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
}

input[type=checkbox] + label:after,
input[type=radio] + label:after {
    left: 0;
    content: " ";
    position: absolute;
    width: 28px;
    height: 28px;
    cursor: pointer;
    border-radius:         15px;
    -webkit-border-radius: 15px;
    -moz-border-radius:    15px;
    -o-border-radius:      15px;
    box-shadow:         0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -webkit-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -moz-box-shadow:    0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -o-box-shadow:      0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    background-color: #fff;
    /* background-image: url(../images/toggle.png);
    background-repeat: no-repeat;
    background-size: contain; */
    transition:         all 0.2s ease-in 0.2s;
    -webkit-transition: all 0.2s ease-in 0.2s;
    -moz-transition:    all 0.2s ease-in 0.2s;
    -o-transition:      all 0.2s ease-in 0.2s;
}

input[type=checkbox]:checked + label:after,
input[type=radio]:checked + label:after {
    left: 23px;
}

input[type=checkbox]:focus,
input[type=checkbox]:active,
input[type=radio]:focus,
input[type=radio]:active {
    outline: 0;
}

#1


3  

inputs aren't supposed to be able to add before and after psuedo-elements because they aren't containers (e.g. an <input> can't have child elements).

输入不能在psuedo元素之前和之后添加,因为它们不是容器(例如,不能有子元素)。

There is some more information in the comment here: https://*.com/a/4660434/4338477

这里有更多的信息:https://*.com/a/4660434/4338477

You could add labels to get the same effect though (this works in Firefox and IE9+):

你可以添加标签来获得同样的效果(这在Firefox和IE9+中都是有效的):

http://jsfiddle.net/8okjwLxx/2/

http://jsfiddle.net/8okjwLxx/2/

<h1>Checkbox :</h1> 

<input id="checkbox1" type="checkbox" />
<label for="checkbox1"></label>
<input id="checkbox2" type="checkbox" checked />
<label for="checkbox2"></label>

<h1>Radio :</h1>

<input id="radio1" type="radio" name="group" />
<label for="radio1"></label>

<input id="radio2" type="radio" name="group" checked />
<label for="radio2"></label>

<input id="radio3" type="radio" name="group" />
<label for="radio3"></label>
body {
    margin: 30px;
    text-align: center;
}

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

input[type=checkbox] + label,
input[type=radio] + label {
    display: inline-block;
    width: 50px;
    height: 30px;
    position: relative;
    cursor: pointer;
    appearance:         none;
    -webkit-appearance: none;
    -moz-appearance:    none;
    box-shadow:         inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -webkit-box-shadow: inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -moz-box-shadow:    inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    -o-box-shadow:      inset 0px 5px 40px -13px rgba(0, 0, 0, 0.75);
    border-radius:         15px;
    -webkit-border-radius: 15px;
    -moz-border-radius:    15px;
    -o-border-radius:      15px;
    background-color: #fff;
    padding: 1px;
    margin: 0px;
    transition:         all 0.3s ease-out;
    -webkit-transition: all 0.3s ease-out;
    -moz-transition:    all 0.3s ease-out;
    -o-transition:      all 0.3s ease-out;  
    transform:         scale(1);
    -webkit-transform: scale(1); /*Adjust size here*/
    -moz-transform:    scale(1);
    -o-transform:      scale(1);
}

input[type=checkbox]:checked + label,
input[type=radio]:checked + label {
    box-shadow:         inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -webkit-box-shadow: inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -moz-box-shadow:    inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
    -o-box-shadow:      inset 0px 0px 0px 20px #53d76a, inset 0px 5px 40px -13px rgba(132, 132, 132, 1);
}

input[type=checkbox] + label:after,
input[type=radio] + label:after {
    left: 0;
    content: " ";
    position: absolute;
    width: 28px;
    height: 28px;
    cursor: pointer;
    border-radius:         15px;
    -webkit-border-radius: 15px;
    -moz-border-radius:    15px;
    -o-border-radius:      15px;
    box-shadow:         0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -webkit-box-shadow: 0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -moz-box-shadow:    0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    -o-box-shadow:      0px 2px 2px 1px rgba(0, 0, 0, 0.2);
    background-color: #fff;
    /* background-image: url(../images/toggle.png);
    background-repeat: no-repeat;
    background-size: contain; */
    transition:         all 0.2s ease-in 0.2s;
    -webkit-transition: all 0.2s ease-in 0.2s;
    -moz-transition:    all 0.2s ease-in 0.2s;
    -o-transition:      all 0.2s ease-in 0.2s;
}

input[type=checkbox]:checked + label:after,
input[type=radio]:checked + label:after {
    left: 23px;
}

input[type=checkbox]:focus,
input[type=checkbox]:active,
input[type=radio]:focus,
input[type=radio]:active {
    outline: 0;
}