0

我正在尝试创建一个只有 CSS/Angular 的http://www.givainc.com/labs/ibutton_jquery_plugin.htm版本

我的目标是通过调整左边距并仅保持容器足够宽以显示 1 来完全隐藏绿色或红色部分。当我调整 slideToggle 容器的宽度时,我似乎无法阻止红色部分包裹。

http://jsfiddle.net/vGBgB/2/

<div class="slideToggle" ng-click="toggle" name="active" onlabel="Active" offlabel="Inactive" style="height: 34px; width: 115px;">
    <input type="checkbox">
    <span class="stOn" style="width: 50px;">Active</span>
    <span class="stHandle">| | |</span>
    <span class="stOff">Inactive</span>
</div>

这是我的 CSS(仅发布,因为 SO 需要它)

.slideToggle {
    cursor: pointer;
    overflow: hidden;
    white-space: nowrap;
}
.slideToggle > input[type=checkbox] {
    display: none;
}
.slideToggle > .stOff,
.slideToggle > .stOn {

    float: left;
    height: 22px;
    padding: 4px 10px 0;

    color: #fff;
    text-align: center;
    white-space: nowrap;
    font-size: 11px;
    line-height: 18px;
    font-weight: bold;
    text-transform: uppercase;
    text-shadow: 0 1px #808080;
}
.stHandle {;
    float: left;
    min-width: 25px;
    background: linear-gradient(to bottom, #fdfdfd 0%,#ebebeb 99%);
    padding: 2px 1px 2px 0;

    border: 1px solid #8f8f8f;
    border-radius: 3px;
    color: #c6c6c6;
    letter-spacing: -1px;
    text-align: center;
}
.slideToggle > .stOn {
    float: left;
    margin-right: -1px;
    background: linear-gradient(to bottom, #8ab056 0%,#a2c46b 100%);
    border: 1px solid #88a75c;
    border-right: none;
    border-top-left-radius: 3px;
    border-bottom-left-radius: 3px;
}
.slideToggle > .stOff {
    float: left;
    margin-left: -1px;
    background: linear-gradient(to bottom, #d4786a 0%,#e39080 100%);
    border: 1px solid #bd6e61;
    border-left: none;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
}
    .slideToggle > .stOff::after {
        content: "";
        clear: both;
    }
4

2 回答 2

0

我的建议是调整您的 HTML 布局。既然我们正在尝试创建滑动效果,为什么不设置您的 HTML 以实际滑动您的按钮内容。用于overflow: hidden;隐藏您不需要的任何额外内容。这也允许您设置非常具体的宽度和高度以帮助保持一致性。

我们将使用容器来指定按钮的总宽度和高度以及活动或非活动跨度。然后我们将使用另一个容器来保存所有 3 个跨度的总宽度。然后我们所要做的就是添加margin-left:到第二个容器以在第一个容器中“滑动”它。 Overflow:hidden;是我们这里真正的冠军。

这是一个 JSFiddle,我在其中包含了一些示例 jquery 来证明它可以工作。您将需要调整代码以匹配您的复选框要求等。如果您认为 jquery 是更好的解决方案,请随意使用 jquery。

http://jsfiddle.net/D6EWF/1/

HTML:

<div class="container">

    <div id="button-container" class="">
        <span class="red">Inactive</span>
        <span id="button" class="button"> | | | </span>
        <span class="green">Active</span>
    </div>

</div>

CSS:

.container {
    width: 100px;
    height: 40px;
    background: orange;
    overflow: hidden;
}

#button-container {
    width: 170px;
    -webkit-transition: all 0.5s ease-in-out;
    -moz-transition: all 0.5s ease-in-out;
    -ms-transition: all 0.5s ease-in-out;
    -o-transition: all 0.5s ease-in-out;
    transition: all 0.5s ease-in-out;
}

.active {
    margin-left: -70px;
}

.red, .button, .green { 
    display: block;
    height: 20px;
    float: left;
}

.red, .green {
    width: 50px;
    padding: 10px;
}

.button {
    text-align: center;
    width: 30px;
    background: #ccc;
    padding: 10px 0;
    cursor: pointer;
}

.red {
    background: red;
}

.green {
    background: green;
}
于 2013-08-21T15:08:05.367 回答
0

HTML

<span class="switcher">
        <input type="checkbox" id="chk1">
        <label for="chk1">
            <span class="on">ON</span>
            <span class="btn">| | |</span>
            <span class="off">OF</span>
        </label>
    </span>

CSS

.switcher{
            width: 100px;
            overflow: hidden;
            display: block;
            border: 1px #000 solid;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 4px;
        }
        .switcher input[type="checkbox"]{
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            z-index: 2;
        }
        .switcher input[type="checkbox"]:checked+label{margin-left:0;}
        .switcher label{
            display: block;
            position: relative;
            z-index: 1;
            width: 170%;
            font:14px/20px Arial, Helvetica, sans-serif;
            overflow: hidden;
            margin-left: -70%;
        }
        .switcher label .on,
        .switcher label .off{
            float: left;
            width: 40%;
            text-align: center;
            color: #fff;
            background: rgb(169,219,128);
            background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2E5ZGI4MCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM5NmM1NmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
            background: -moz-linear-gradient(top, rgba(169,219,128,1) 0%, rgba(150,197,111,1) 100%);
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(169,219,128,1)), color-stop(100%,rgba(150,197,111,1)));
            background: -webkit-linear-gradient(top, rgba(169,219,128,1) 0%,rgba(150,197,111,1) 100%);
            background: -o-linear-gradient(top, rgba(169,219,128,1) 0%,rgba(150,197,111,1) 100%);
            background: -ms-linear-gradient(top, rgba(169,219,128,1) 0%,rgba(150,197,111,1) 100%);
            background: linear-gradient(to bottom, rgba(169,219,128,1) 0%,rgba(150,197,111,1) 100%);
            -webkit-border-radius: 4px 0 0 4px;
            -moz-border-radius: 4px 0 0 4px;
            border-radius: 4px 0 0 4px;
        }
        .switcher label .off{
            background: rgb(255,48,25);
            background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZmMzAxOSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNjZjA0MDQiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
            background: -moz-linear-gradient(top, rgba(255,48,25,1) 0%, rgba(207,4,4,1) 100%);
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,48,25,1)), color-stop(100%,rgba(207,4,4,1)));
            background: -webkit-linear-gradient(top, rgba(255,48,25,1) 0%,rgba(207,4,4,1) 100%);
            background: -o-linear-gradient(top, rgba(255,48,25,1) 0%,rgba(207,4,4,1) 100%);
            background: -ms-linear-gradient(top, rgba(255,48,25,1) 0%,rgba(207,4,4,1) 100%);
            background: linear-gradient(to bottom, rgba(255,48,25,1) 0%,rgba(207,4,4,1) 100%);
            -webkit-border-radius: 0 4px 4px 0;
            -moz-border-radius: 0 4px 4px 0;
            border-radius: 0 4px 4px 0;
        }
        .switcher label .btn{
            float: left;
            text-align: center;
            width: 22%;
            margin: 0 -1%;
            position: relative;
            background: rgb(238,238,238);
            background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2VlZWVlZSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNlZWVlZWUiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
            background: -moz-linear-gradient(top, rgba(238,238,238,1) 0%, rgba(238,238,238,1) 100%);
            background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(238,238,238,1)), color-stop(100%,rgba(238,238,238,1)));
            background: -webkit-linear-gradient(top, rgba(238,238,238,1) 0%,rgba(238,238,238,1) 100%);
            background: -o-linear-gradient(top, rgba(238,238,238,1) 0%,rgba(238,238,238,1) 100%);
            background: -ms-linear-gradient(top, rgba(238,238,238,1) 0%,rgba(238,238,238,1) 100%);
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
            border-radius: 4px;
        }
于 2013-08-21T14:08:23.160 回答