iphone风格复选框

时间:2013-02-12 06:14:20

标签: jquery css css3 css-transitions

我想创建一个像!

这样的复选框
<label id="sliderLabel">
    <input type="checkbox" />
    <span id="slider">
        <span id="sliderOn">SELECTED</span>
        <span id="sliderOff">SELECT</span>
        <span id="sliderBlock"></span>
    </span>
</label>

但是当我增加复选框的宽度时。它不起作用。这是我的代码jsfiddle

2 个答案:

答案 0 :(得分:4)

 jsfiddle.net/RUvhw/3/

我假设你自己没有做到这一点?你只需要稍微玩一下这些值,直到你得到所需的尺寸......

答案 1 :(得分:1)

您需要修改宽度和左侧值。我已经分叉并更新了您的问题/答案。

http://jsfiddle.net/Pfxe6/

p {
    margin: 10px;
}

#sliderLabel {
    border: 1px solid #a2a2a2;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;    
    border-radius: 3px;
    cursor: pointer;
    display: block;
    height: 30px;
    margin: 20px auto;
    overflow: hidden;
    position: relative;
    width: 200px;
}
#sliderLabel input {
    display: none;
}
#sliderLabel input:checked + #slider {
    left: 0px;
}
#slider {
    left: -100px;
    position: absolute;;
    top: 0px;
    -moz-transition: left .25s ease-out;
    -webkit-transition: left .25s ease-out;
    -o-transition: left .25s ease-out;
    transition: left .25s ease-out;
}
#sliderOn, #sliderBlock, #sliderOff {
    display: block;
    font-family: arial, verdana, sans-serif;
    font-weight: bold;
    height: 30px;
    line-height: 30px;
    position: absolute;
    text-align: center;
    top: 0px;
}
#sliderOn {
    background: #3269aa;
    background: -moz-linear-gradient(top,  #3269aa 0%, #82b3f4 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#3269aa), color-stop(100%,#82b3f4));
    background: -webkit-linear-gradient(top,  #3269aa 0%,#82b3f4 100%);
    background: -o-linear-gradient(top,  #3269aa 0%,#82b3f4 100%);
    background: -ms-linear-gradient(top,  #3269aa 0%,#82b3f4 100%);
    background: linear-gradient(top,  #3269aa 0%,#82b3f4 100%);
    color: white;
    left: 0px;
    width: 100px;
}
#sliderBlock {
    background: #d9d9d8;
    background: -moz-linear-gradient(top,  #d9d9d8 0%, #fcfcfc 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#d9d9d8), color-stop(100%,#fcfcfc));
    background: -webkit-linear-gradient(top,  #d9d9d8 0%,#fcfcfc 100%);
    background: -o-linear-gradient(top,  #d9d9d8 0%,#fcfcfc 100%);
    background: -ms-linear-gradient(top,  #d9d9d8 0%,#fcfcfc 100%);
    background: linear-gradient(top,  #d9d9d8 0%,#fcfcfc 100%);
    border: 1px solid #a2a2a2;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radisu: 3px;
    height: 28px;
    left: 100px;
    width: 100px;
}
#sliderOff {
    background: #f2f3f2;
    background: -moz-linear-gradient(top,  #8b8c8b 0%, #f2f3f2 50%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#8b8c8b), color-stop(50%,#f2f3f2));
    background: -webkit-linear-gradient(top,  #8b8c8b 0%,#f2f3f2 50%);
    background: -o-linear-gradient(top,  #8b8c8b 0%,#f2f3f2 50%);
    background: -ms-linear-gradient(top,  #8b8c8b 0%,#f2f3f2 50%);
    background: linear-gradient(top,  #8b8c8b 0%,#f2f3f2 50%);
    color: #8b8b8b;
    left: 200px;
    width: 100px;
}