フォームやインタラクティブなサイトで使えるON/OFFのトグルスイッチのデザイン7つのご紹介。
シンプルなものなので様々な所で利用できるトグルスイッチ7つを作ってみたので
ぜひぜひコピペして利用してくれたら嬉しいです!
チェックボックスは見えないけど、機能するように中にちゃんと入っているので
バリデーションでチェックボックスのチェックする時やJSでON/OFFの判定する時も便利に作ってあるよ!
iOS風スイッチ
HTML
<div class="toggle"> <input type="checkbox" name="check" /> </div>
CSS
.toggle { position: relative; width: 78px; height: 48px; margin: 40px 60px; border-radius: 50px; overflow: hidden; cursor: pointer; } .toggle input[type=checkbox] { display: none; } .toggle:before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: block; background: #e9e9eb; -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle:after { content: ""; position: absolute; top: 3px; left: 3px; width: 42px; height: 42px; display: block; border-radius: 50px; background: #fff; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.3); -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle.checked:before { background: #35c759; } .toggle.checked:after { left: 33px; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.5); }
jQuery
$(".toggle").on("click", function() { $(".toggle").toggleClass("checked"); if(!$('input[name="check"]').prop("checked")) { $(".toggle input").prop("checked", true); } else { $(".toggle input").prop("checked", false); } });
細いバーのスイッチ
HTML
<div class="toggle"> <input type="checkbox" name="check" /> </div>
CSS
.toggle { position: relative; width: 78px; height: 48px; margin: 40px 60px; cursor: pointer; } .toggle input[type=checkbox] { display: none; } .toggle:before { content: ""; position: absolute; left: 0; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); width: 85%; height: 25%; border-radius: 20px; display: block; background: #e9e9eb; box-shadow: 0 3px 7px -3px rgba(0, 0, 0, 0.2) inset; -webkit-transition: 0.2s ease-in-out; transition: 0.2s ease-in-out; } .toggle:after { content: ""; position: absolute; top: 3px; left: 0px; width: 42px; height: 42px; display: block; border-radius: 50px; background: #fff; box-shadow: 0 3px 12px -4px rgba(0, 0, 0, 0.3); -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle.checked:before { background: #35c759; } .toggle.checked:after { left: 36px; }
jQuery
$(".toggle").on("click", function() { $(".toggle").toggleClass("checked"); if(!$('input[name="check"]').prop("checked")) { $(".toggle input").prop("checked", true); } else { $(".toggle input").prop("checked", false); } });
四角のスイッチ
HTML
<div class="toggle"> <input type="checkbox" name="check" /> </div>
CSS
.toggle { position: relative; width: 78px; height: 48px; margin: 40px 60px; border-radius: 8px; overflow: hidden; cursor: pointer; } .toggle input[type=checkbox] { display: none; } .toggle:before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: block; background: #e9e9eb; -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle:after { content: ""; position: absolute; top: 3px; left: 3px; width: 42px; height: 42px; display: block; border-radius: 6px; background: #fff; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.3); -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle.checked:before { background: #35c759; } .toggle.checked:after { left: 33px; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.5); }
jQuery
$(".toggle").on("click", function() { $(".toggle").toggleClass("checked"); if(!$('input[name="check"]').prop("checked")) { $(".toggle input").prop("checked", true); } else { $(".toggle input").prop("checked", false); } });
赤・緑とON・OFFの付いたスイッチ
HTML
<div class="toggle"> <input type="checkbox" name="check" /> </div>
CSS
.toggle { position: relative; width: 78px; height: 48px; margin: 40px 60px; border-radius: 50px; overflow: hidden; cursor: pointer; } .toggle input[type=checkbox] { display: none; } .toggle:before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: block; background: #f45e55; -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle:after { content: "OFF"; position: absolute; top: 3px; left: 3px; width: 42px; height: 42px; display: block; border-radius: 50px; background: #fff; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.3); -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; text-align: center; padding: 14px 0 0; line-height: 1; font-size: 14px; font-weight: bold; color: #df4c43; letter-spacing: .5px; box-sizing: border-box; } .toggle.checked:before { background: #24e89c; } .toggle.checked:after { content: "ON"; left: 33px; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.5); color: #16d088; padding: 14px 0 0 1px; }
jQuery
$(".toggle").on("click", function() { $(".toggle").toggleClass("checked"); if(!$('input[name="check"]').prop("checked")) { $(".toggle input").prop("checked", true); } else { $(".toggle input").prop("checked", false); } });
赤・緑と丸と棒のスイッチ
HTML
<div class="toggle"> <input type="checkbox" name="check" /> </div>
CSS
.toggle { position: relative; width: 78px; height: 48px; margin: 40px 60px; border-radius: 50px; overflow: hidden; cursor: pointer; } .toggle input[type=checkbox] { display: none; } .toggle:before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: block; background: #f45e55; -webkit-transition: 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); transition: 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); } .toggle:after { content: ""; position: absolute; top: 10px; left: 12px; width: 28px; height: 28px; display: block; border-radius: 50px; border: 6px solid #fff; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.3); -webkit-transition: 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); transition: 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); box-sizing: border-box; } .toggle.checked:before { background: #24e89c; } .toggle.checked:after { left: 46px; width: 0px; background: #fff; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.5); }
jQuery
$(".toggle").on("click", function() { $(".toggle").toggleClass("checked"); if(!$('input[name="check"]').prop("checked")) { $(".toggle input").prop("checked", true); } else { $(".toggle input").prop("checked", false); } });
iOS風スイッチの横にON・OFF
OFF
HTML
<div class="toggle"> <input type="checkbox" name="check" /> <span>OFF</span> </div>
CSS
.toggle { position: relative; width: 78px; height: 48px; margin: 40px 60px; user-select: none; cursor: pointer; } .toggle input[type=checkbox] { display: none; } .toggle span { position: absolute; top: 16px; right: -36px; font-size: 14px; font-weight: bold; line-height: 1; color: #cbcbcb; -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle:before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50px; display: block; background: #e9e9eb; -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle:after { content: ""; position: absolute; top: 3px; left: 3px; width: 42px; height: 42px; display: block; border-radius: 50px; background: #fff; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.3); -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle.checked span { color: #35c759; } .toggle.checked:before { background: #35c759; } .toggle.checked:after { left: 33px; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.5); }
jQuery
$(".toggle").on("click", function() { $(".toggle").toggleClass("checked"); if(!$('input[name="check"]').prop("checked")) { $(".toggle input").prop("checked", true); $(".toggle span").text("ON"); } else { $(".toggle input").prop("checked", false); $(".toggle span").text("OFF"); } });
iOS風スイッチの中にON・OFF
OFF
ON
HTML
<div class="toggle"> <input type="checkbox" name="check" /> <span>OFF</span> <span>ON</span> </div>
CSS
.toggle { position: relative; width: 78px; height: 48px; margin: 40px 60px; border-radius: 50px; overflow: hidden; user-select: none; cursor: pointer; } .toggle input[type=checkbox] { display: none; } .toggle span { position: absolute; top: 18px; font-size: 12px; font-weight: bold; line-height: 1; -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle span:nth-of-type(1) { right: 5px; color: rgba(0, 0, 0, 0.175); } .toggle span:nth-of-type(2) { left: 7px; color: #fff; letter-spacing: 2px; } .toggle:before { content: ""; position: absolute; top: 0; left: 0; width: 100%; height: 100%; border-radius: 50px; display: block; background: #e9e9eb; -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle:after { content: ""; position: absolute; top: 3px; left: 3px; width: 42px; height: 42px; display: block; border-radius: 50px; background: #fff; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.3); -webkit-transition: 0.2s ease-out; transition: 0.2s ease-out; } .toggle.checked:before { background: #35c759; } .toggle.checked:after { left: 33px; box-shadow: 0 9px 28px -6px rgba(0, 0, 0, 0.5); }
jQuery
$(".toggle").on("click", function() { $(".toggle").toggleClass("checked"); if(!$('input[name="check"]').prop("checked")) { $(".toggle input").prop("checked", true); } else { $(".toggle input").prop("checked", false); } });
コメント