Muitas vezes desejamos personalizar nossos formulários para fazer alternativas cada vez mais amigáveis para os usuários. Um item que fica interessante um botão do estilo interruptor para ativar e desativar algo.
Nesse tutorial, criaremos um botão estilo interruptor, onde mostrar como OFF e ON, quando você clica, utilizando o CSS3.
Vamos começar pelo html:
<div class="switch"> <input type="checkbox" name="option" id="option" /> <label for="option"><span></span></label> </div>
Criamos uma div.switch que engloba o nosso botão, e dentro da div, temos input, pra fazer o efeito de marcar botão, como ligado e desligado. E também vamos adicionar label, pra definir o corpo do botão, e o span que será o interruptor.
No CSS, vamos usar pseudo-classe before (antes), after (depois), e conjunto com a propriedade content, para adicionar um conteúdo dos elementos. Vamos utilizar também, background gradiente linear, sua sintaxe seria assim:
background: linear-gradient(COR INICIAL, COR FINAL);
Vamos agora o CSS.
.switch{ width:180px; height:50px; margin:0 auto; position:relative; }
.switch input{ display:block; width:100%; height: 100%; position:absolute; top:0; left:0; z-index: 10; opacity: 0; cursor: pointer; }
.switch label{ display: block; width: 100%; height: 100%; background: -moz-linear-gradient(#750000, #B20000); background: -ms-linear-gradient(#750000, #B20000); background: -o-linear-gradient(#750000, #B20000); background: -webkit-linear-gradient(#750000, #B20000); background: linear-gradient(#750000, #B20000); border-radius: 40px; }
.switch label:after{ content: ""; position: absolute; top: -10px; right: -10px; bottom: -10px; left: -10px; z-index: -1; background: -moz-linear-gradient(#EEEEEE, #AAAAAA); background: -ms-linear-gradient(#EEEEEE, #AAAAAA); background: -o-linear-gradient(#EEEEEE, #AAAAAA); background: -webkit-linear-gradient(#EEEEEE, #AAAAAA); background: linear-gradient(#EEEEEE, #AAAAAA); border-radius: inherit; }
.switch label span{ display: block; width: 60%; height: 100%; background: #C0C0C0; position: absolute; z-index: 2; right: 41%; top: 0; background: -moz-linear-gradient(#E0E0E0, #A0A0A0); background: -ms-linear-gradient(#E0E0E0, #A0A0A0); background: -o-linear-gradient(#E0E0E0, #A0A0A0); background: -webkit-linear-gradient(#E0E0E0, #A0A0A0); background: linear-gradient(#E0E0E0, #A0A0A0); border-radius: inherit; }
.switch label span:after { content: ""; position: absolute; left: 15%; top: 25%; width: 70%; height: 50%; background: -moz-linear-gradient(#F7F7F7, #CCCCCC); background: -ms-linear-gradient(#F7F7F7, #CCCCCC); background: -o-linear-gradient(#F7F7F7, #CCCCCC); background: -webkit-linear-gradient(#F7F7F7, #CCCCCC); background: linear-gradient(#F7F7F7, #CCCCCC); border-radius: inherit; }
Usaremos a pseudo-classe :before, para adicionar o conteúdo antes do span, com o content;
.switch label span:before { content: "off"; position: absolute; top: 50%; margin-top: -12px; right: -50%; text-transform: uppercase; font-weight: bold; font-family: Arial, sans-serif; font-size: 24px; color: #fff; }
.switch input:checked ~ label { background: -moz-linear-gradient(#004010, #1A6600); background: -ms-linear-gradient(#004010, #1A6600); background: -o-linear-gradient(#004010, #1A6600); background: -webkit-linear-gradient(#004010, #1A6600); background: linear-gradient(#004010, #1A6600); }
Selecionaremos o elemento span dentro do label, precedito (~) quando o input estiver marcado (:checked);
.switch input:checked ~ label span{ right: -2px; }
Selecionaremos o elemento span:before dentro do label, precedito (~) quando o input estiver marcado (:checked);
.switch input:checked ~ label span:before { content: "on"; right: 115%; }
.switch{ width:180px; height:50px; margin:0 auto; position:relative; } .switch input{ display:block; width:100%; height: 100%; position:absolute; top:0; left:0; z-index: 10; opacity: 0; cursor: pointer; } .switch label{ display: block; width: 100%; height: 100%; background: -moz-linear-gradient(#750000, #B20000); background: -ms-linear-gradient(#750000, #B20000); background: -o-linear-gradient(#750000, #B20000); background: -webkit-linear-gradient(#750000, #B20000); background: linear-gradient(#750000, #B20000); border-radius: 40px; } .switch label:after{ content: ""; position: absolute; top: -10px; right: -10px; bottom: -10px; left: -10px; z-index: -1; background: -moz-linear-gradient(#EEEEEE, #AAAAAA); background: -ms-linear-gradient(#EEEEEE, #AAAAAA); background: -o-linear-gradient(#EEEEEE, #AAAAAA); background: -webkit-linear-gradient(#EEEEEE, #AAAAAA); background: linear-gradient(#EEEEEE, #AAAAAA); border-radius: inherit; } .switch label span{ display: block; width: 60%; height: 100%; background: #C0C0C0; position: absolute; z-index: 2; right: 41%; top: 0; background: -moz-linear-gradient(#E0E0E0, #A0A0A0); background: -ms-linear-gradient(#E0E0E0, #A0A0A0); background: -o-linear-gradient(#E0E0E0, #A0A0A0); background: -webkit-linear-gradient(#E0E0E0, #A0A0A0); background: linear-gradient(#E0E0E0, #A0A0A0); border-radius: inherit; } .switch label span:after { content: ""; position: absolute; left: 15%; top: 25%; width: 70%; height: 50%; background: -moz-linear-gradient(#F7F7F7, #CCCCCC); background: -ms-linear-gradient(#F7F7F7, #CCCCCC); background: -o-linear-gradient(#F7F7F7, #CCCCCC); background: -webkit-linear-gradient(#F7F7F7, #CCCCCC); background: linear-gradient(#F7F7F7, #CCCCCC); border-radius: inherit; } .switch label span:before { content: "off"; position: absolute; top: 50%; margin-top: -12px; right: -50%; text-transform: uppercase; font-weight: bold; font-family: Arial, sans-serif; font-size: 24px; color: #fff; } .switch input:checked ~ label { background: -moz-linear-gradient(#004010, #1A6600); background: -ms-linear-gradient(#004010, #1A6600); background: -o-linear-gradient(#004010, #1A6600); background: -webkit-linear-gradient(#004010, #1A6600); background: linear-gradient(#004010, #1A6600); } .switch input:checked ~ label span{ right: -2px; } .switch input:checked ~ label span:before { content: "on"; right: 115%; }
Agora vamos ver o efeito em funcionamento: Clique Aqui
Se você deseja conhecer mais sobre os poderes do CSS3, temos um curso sobe medida pra você.
Nos últimos anos, testemunhamos uma revolução na forma como os sites são criados, impulsionada pelo…
A animação 3D tem sido uma forma poderosa de contar histórias e criar experiências visuais…
Nos últimos anos, a inteligência artificial (IA) tem revolucionado a forma como criamos e editamos…
Nos últimos anos, a integração da inteligência artificial (IA) na criação de sites tem revolucionado…
Nos últimos anos, a edição de vídeo evoluiu rapidamente com o avanço da tecnologia, e…
A dublagem de filmes desempenha um papel fundamental na indústria cinematográfica, permitindo que obras de…
This website uses cookies.