0

I'm working on a web app for tablets, and I've styled a set of of divs and a label for my checkbox so that it can look like a switch. (JsFiddle here.)

It works fine on iPad 2 & 3, however when I'm viewing it on Samsung Galaxy Tab 2 it only changes state when I turn the tablet from portrait to landscape or vice versa

I've also tried it without the transition, and still the same problem.

Is there any way I can get the effect I want without Javascript?

SO won't let me just link to jsfiddle so here is the code:

HTML:

<label class="switch" for="test">
    <input type="checkbox" id="test">
    <div class="track">
        <div class="knob"></div>
    </div>
</label>​

CSS:

label.switch > .track {
    width: 80px;
    height: 52px;
    border-radius: 60px;
    box-shadow: 0 1px 3px black inset, 0 -1px 1px rgba(255,255,255,0.15);
    /*border: 5px solid #666;*/
    display:inline-block;
    cursor:pointer;
    background: #333;
    background: -moz-linear-gradient(top,  rgba(0,0,0,0.65) 0%, rgba(0,0,0,0) 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0)));
    background: -webkit-linear-gradient(top,  rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%);
    background: -o-linear-gradient(top,  rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%);
    background: -ms-linear-gradient(top,  rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%);
    background: linear-gradient(to bottom,  rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%);
    transition: background .5s;
    -webkit-transition: background .5s;
    -moz-transition: background .5s;
    -o-transition: background .5s;
    -ms-transition: background .5s;
}

label.switch > .track > .knob {
    height: 54px;
    width: 54px;
    border-radius: 50px;
    margin-top:-1px;
    box-shadow: 0 2px 3px black, 0 1px 0 rgba(255, 255, 255, 0.5) inset, 0 0 0 5px rgba(237,237,237,1) inset;
    transition: margin-left .25s;
    -webkit-transition: margin-left .25s;
    -moz-transition: margin-left .25s;
    -o-transition: margin-left .25s;
    -ms-transition: margin-left .25s;
    background: rgb(191,191,191);
    background: -moz-linear-gradient(top,  rgba(191,191,191,1) 0%, rgba(246,246,246,1) 53%, rgba(255,255,255,1) 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(191,191,191,1)), color-stop(53%,rgba(246,246,246,1)), color-stop(100%,rgba(255,255,255,1)));
    background: -webkit-linear-gradient(top,  rgba(191,191,191,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
    background: -o-linear-gradient(top,  rgba(191,191,191,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
    background: -ms-linear-gradient(top,  rgba(191,191,191,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
    background: linear-gradient(to bottom,  rgba(191,191,191,1) 0%,rgba(246,246,246,1) 53%,rgba(255,255,255,1) 100%);
}


label.switch > input {
    display:none;
}

label.switch > input:checked ~ .track {
    background: #D96200

}

label.switch > input:checked ~ .track > .knob {
    margin-left:26px;
}
​
4

1 回答 1

0

Android 平板电脑(至少是三星 Galaxy Tab 2)附带旧版 Chrome 作为默认浏览器。

此浏览器无法识别通用同级选择器( ~)。这就是动画不工作的原因。

解决方案是为相邻兄弟选择器( +) 更改所有出现的通用兄弟选择器。

于 2012-10-31T18:42:08.937 回答