2016-03-02 79 views
1

我需要使該切換效果在任何單個複選框上工作。例如,我會檢查第二個(樣式)複選框,它會自動切換,其他所有人都不會受到影響。 Thx非常需要幫助。CSS:複選框切換效果僅適用於第一個複選框

https://jsfiddle.net/99asehku/

HTML:

<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
    <label class="onoffswitch-label" for="myonoffswitch"> 
     <span class="onoffswitch-inner"></span> 
     <span class="onoffswitch-switch"></span> 
    </label> 
</div><br><br> 
<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
    <label class="onoffswitch-label" for="myonoffswitch"> 
     <span class="onoffswitch-inner"></span> 
     <span class="onoffswitch-switch"></span> 
    </label> 
</div><br><br> 
<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch1" class="onoffswitch-checkbox1" id="myonoffswitch1" checked> 
    <label class="onoffswitch-label1" for="myonoffswitch1"> 
     <span class="onoffswitch-inner1"></span> 
     <span class="onoffswitch-switch1"></span> 
    </label> 
</div> 

CSS:

所有ID的
.onoffswitch { 
    position: relative; width: 58px; 
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none; 
} 
.onoffswitch-checkbox { 
    display: none; 
} 
.onoffswitch-label { 
    display: block; overflow: hidden; cursor: pointer; 
    border: 2px solid #999999; border-radius: 20px; 
} 
.onoffswitch-inner { 
    display: block; width: 200%; margin-left: -100%; 
    transition: margin 0.3s ease-in 0s; 
} 
.onoffswitch-inner:before, .onoffswitch-inner:after { 
    display: block; float: left; width: 50%; height: 10px; padding: 0; line-height: 10px; 
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold; 
    box-sizing: border-box; 
} 
.onoffswitch-inner:before { 
    content: "ANO"; 
    padding-left: 10px; 
    background-color: #34A7C1; color: #FFFFFF; 
} 
.onoffswitch-inner:after { 
    content: "NE"; 
    padding-right: 10px; 
    background-color: #EEEEEE; color: #999999; 
    text-align: right; 
} 
.onoffswitch-switch { 
    display: block; width: 10px; margin: 0px; 
    background: #FFFFFF; 
    position: absolute; top: 0; bottom: 0; 
    right: 44px; 
    border: 2px solid #999999; border-radius: 20px; 
    transition: all 0.3s ease-in 0s; 
} 
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner { 
    margin-left: 0; 
} 
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch { 
    right: 0px; 
} 
+0

我已經自由地評論了你的JSFiddle鏈接,因爲你故意繞過堆棧溢出的*** JSFiddle鏈接必須伴隨代碼***警告通過將鏈接放置在代碼塊中。我還投票決定關閉這個問題,因爲你在問題本身沒有提供任何代碼。 –

+0

對不起,我沒有爲自己翻譯這個錯誤信息,我迫切需要得到解決這個問題的任何提示。我很高興看到消除這種負面聲譽。 Thx –

+0

好得多。我收回了我的近距離投票。 –

回答

-1

首先必須是唯一的。不要爲相同的元素使用相同的ID。

並使for爲所有不同。

.onoffswitch { 
 
    position: relative; width: 58px; 
 
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none; 
 
} 
 
.onoffswitch-checkbox { 
 
    display: none; 
 
} 
 
.onoffswitch-label { 
 
    display: block; overflow: hidden; cursor: pointer; 
 
    border: 2px solid #999999; border-radius: 20px; 
 
} 
 
.onoffswitch-inner { 
 
    display: block; width: 200%; margin-left: -100%; 
 
    transition: margin 0.3s ease-in 0s; 
 
} 
 
.onoffswitch-inner:before, .onoffswitch-inner:after { 
 
    display: block; float: left; width: 50%; height: 10px; padding: 0; line-height: 10px; 
 
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold; 
 
    box-sizing: border-box; 
 
} 
 
.onoffswitch-inner:before { 
 
    content: "ANO"; 
 
    padding-left: 10px; 
 
    background-color: #34A7C1; color: #FFFFFF; 
 
} 
 
.onoffswitch-inner:after { 
 
    content: "NE"; 
 
    padding-right: 10px; 
 
    background-color: #EEEEEE; color: #999999; 
 
    text-align: right; 
 
} 
 
.onoffswitch-switch { 
 
    display: block; width: 10px; margin: 0px; 
 
    background: #FFFFFF; 
 
    position: absolute; top: 0; bottom: 0; 
 
    right: 44px; 
 
    border: 2px solid #999999; border-radius: 20px; 
 
    transition: all 0.3s ease-in 0s; 
 
} 
 
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner { 
 
    margin-left: 0; 
 
} 
 
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch { 
 
    right: 0px; 
 
}
<div class="onoffswitch"> 
 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
 
    <label class="onoffswitch-label" for="myonoffswitch"> 
 
     <span class="onoffswitch-inner"></span> 
 
     <span class="onoffswitch-switch"></span> 
 
    </label> 
 
</div><br><br> 
 
<div class="onoffswitch"> 
 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch1" checked> 
 
    <label class="onoffswitch-label" for="myonoffswitch1"> 
 
     <span class="onoffswitch-inner"></span> 
 
     <span class="onoffswitch-switch"></span> 
 
    </label> 
 
</div><br><br> 
 
<div class="onoffswitch"> 
 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2" checked> 
 
    <label class="onoffswitch-label" for="myonoffswitch2"> 
 
     <span class="onoffswitch-inner"></span> 
 
     <span class="onoffswitch-switch"></span> 
 
    </label> 
 
</div>

+0

Thx ketan我會嘗試 –

+0

@FilipCZ樂意提供幫助。你只能接受一個答案。不是多個。 – ketan

-1

你必須給每個複選框交換機的名稱和ID唯一。

看這個:jsfiddle;最後一個有自己的ID和名字,而第二個有相同的ID和第一個名字。 (當然,CSS代碼必須被重複或修改規則,以適用於每個新的ID)

<div class="onoffswitch"> 
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
<label class="onoffswitch-label" for="myonoffswitch"> 
    <span class="onoffswitch-inner"></span> 
    <span class="onoffswitch-switch"></span> 
</label> 





+0

完美的silviagreen,我也很高興你幫助了我。 –

+0

爲什麼downvote? – silviagreen