2012-02-28 40 views
23

Quiero animar bordes de un elemento usando CSS3, ya sea en estado hover o en estado normal. ¿Alguien puede proporcionarme un fragmento de código para esto o puede guiar?Color de borde animado CSS3

Puedo hacer esto usando jQuery pero buscando alguna solución pura de CSS3.

Respuesta

53

Puede usar un CSS3 transition para esto. Echar un vistazo a este ejemplo:

http://jsfiddle.net/ujDkf/1/

Este es el código principal:

#box { 
    position : relative; 
    width : 100px; 
    height : 100px; 
    background-color : gray; 
    border : 5px solid black; 
    -webkit-transition : border 500ms ease-out; 
    -moz-transition : border 500ms ease-out; 
    -o-transition : border 500ms ease-out; 
    transition : border 500ms ease-out; 
} 

#box:hover { 
    border : 10px solid red; 
} 
0

Si necesita la transición a correr infinitamente, pruebe el siguiente ejemplo:

#box { 
 
    position: relative; 
 
    width: 100px; 
 
    height: 100px; 
 
    background-color: gray; 
 
    border: 5px solid black; 
 
    display: block; 
 
} 
 

 
#box:hover { 
 
    border-color: red; 
 
    animation-name: flash_border; 
 
    animation-duration: 2s; 
 
    animation-timing-function: linear; 
 
    animation-iteration-count: infinite; 
 
    -webkit-animation-name: flash_border; 
 
    -webkit-animation-duration: 2s; 
 
    -webkit-animation-timing-function: linear; 
 
    -webkit-animation-iteration-count: infinite; 
 
    -moz-animation-name: flash_border; 
 
    -moz-animation-duration: 2s; 
 
    -moz-animation-timing-function: linear; 
 
    -moz-animation-iteration-count: infinite; 
 
} 
 

 
@keyframes flash_border { 
 
    0% { 
 
    border-color: red; 
 
    } 
 
    50% { 
 
    border-color: black; 
 
    } 
 
    100% { 
 
    border-color: red; 
 
    } 
 
} 
 

 
@-webkit-keyframes flash_border { 
 
    0% { 
 
    border-color: red; 
 
    } 
 
    50% { 
 
    border-color: black; 
 
    } 
 
    100% { 
 
    border-color: red; 
 
    } 
 
} 
 

 
@-moz-keyframes flash_border { 
 
    0% { 
 
    border-color: red; 
 
    } 
 
    50% { 
 
    border-color: black; 
 
    } 
 
    100% { 
 
    border-color: red; 
 
    } 
 
}
<div id="box">roll over me</div>

+2

Mientras que este enlace puede responder a la pregunta, es mejor incluir las partes esenciales de la respuesta aquí y proporcionar el enlace de referencia. Las respuestas de solo enlace pueden dejar de ser válidas si la página vinculada cambia. - [De la crítica] (/ review/low-quality-posts/17804187) – robinCTS

+2

Heya @Tahir. Como aviso, las respuestas deben ser autocontenidas; si bien pueden referirse a fuentes externas como referencias, la información de respuesta no debe existir únicamente en sitios externos. Debido a eso, traje tu código JSFiddle a la respuesta misma, convertí el SCSS a CSS y renombré el nombre de la animación a 'flash_border' (porque el nombre' flash' estaba en conflicto con algo del lado de Stack Overflow). –

+0

@ChrisForrence Gracias amigo – Tahir

2

Puede probar esto también ...

button { 
 
    background: none; 
 
    border: 0; 
 
    box-sizing: border-box; 
 
    margin: 1em; 
 
    padding: 1em 2em; 
 
    box-shadow: inset 0 0 0 2px #f45e61; 
 
    color: #f45e61; 
 
    font-size: inherit; 
 
    font-weight: 700; 
 
    position: relative; 
 
    vertical-align: middle; 
 
} 
 
button::before, button::after { 
 
    box-sizing: inherit; 
 
    content: ''; 
 
    position: absolute; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.draw { 
 
    -webkit-transition: color 0.25s; 
 
    transition: color 0.25s; 
 
} 
 
.draw::before, .draw::after { 
 
    border: 2px solid transparent; 
 
    width: 0; 
 
    height: 0; 
 
} 
 
.draw::before { 
 
    top: 0; 
 
    left: 0; 
 
} 
 
.draw::after { 
 
    bottom: 0; 
 
    right: 0; 
 
} 
 
.draw:hover { 
 
    color: #60daaa; 
 
} 
 
.draw:hover::before, .draw:hover::after { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
.draw:hover::before { 
 
    border-top-color: #60daaa; 
 
    border-right-color: #60daaa; 
 
    -webkit-transition: width 0.25s ease-out, height 0.25s ease-out 0.25s; 
 
    transition: width 0.25s ease-out, height 0.25s ease-out 0.25s; 
 
} 
 
.draw:hover::after { 
 
    border-bottom-color: #60daaa; 
 
    border-left-color: #60daaa; 
 
    -webkit-transition: border-color 0s ease-out 0.5s, width 0.25s ease-out 0.5s, height 0.25s ease-out 0.75s; 
 
    transition: border-color 0s ease-out 0.5s, width 0.25s ease-out 0.5s, height 0.25s ease-out 0.75s; 
 
}
<section class="buttons"> 
 
    <button class="draw">Draw</button> 
 
    </section