Using transitions with delay, you can do. See an example:
a { display:block; position:relative; background:#ffc; width:300px; height:180px }
a::before, a::after { content:""; display:block; box-sizing:border-box;
position:absolute;z-index:5; pointer-events: none;
width:0; height:0; opacity:0; border:2px solid red }
a::before { border-left:none; border-bottom:none; left:0; top:0;
transition:width .5s linear 1.5s, height .5s linear 1s, opacity .1s 2s }
a::after { border-top:none; border-right:none; right:0; bottom:0;
transition:width .5s linear .5s, height .5s linear, opacity .1s 1s }
a:hover::before, a:hover::after { width:100%; height:100%; opacity:1 }
a:hover::before { transition:width .5s linear, height .5s linear .5s, opacity .1s }
a:hover::after { transition:width .5s linear 1s, height .5s linear 1.5s, opacity .1s 1s }
<a>Ponha o ponteiro do mouse aqui</a>
Basically we created two pseudoelements, the ::before
with the upper right, and the ::after
with the lower left.
We use absolute position in pseudoelements and display: block
to ensure that the element occupies the entire area of the Parent, relative position to limit the area of pseudoelements.
The z-index:5
causes edge elements to always appear above the main element, and the pointer-events:none
prevents them from "stealing" the mouse click of the main element.
As the edges would appear in the corners before animation, we used the opacity
to hide them until the right moment.
Finally, to have the animation drawing and "disdaining" when the mouse leaves the element, we apply transition
different s within the element, and the element:hover
.
To change the speed, remember to change all times in the same ratio. The current animation takes 2 seconds. For example, if you want it to last just one second, you have to change it in that ratio:
Tudo que for .5s por .25s
1s por .5s
1.5s por .75s
2s por 1s
.1s você pode manter, pois é pra ser "quase instantâneo" mesmo.
Just keep the ratio for other speeds.
Perfect, friend! Many thanks and congratulations for the reply.
– Tiago P.C
@Tiagop. C was a "puzzle" in the beginning, until I imagined how to solve, but I think in the end it gave to make a cool balance between complexity and result. I hope it will be useful, at least as an example.
– Bacco
I tried to do it in a way that you can use independent of the element size, and that accepts edge thicknesses as you think best (as long as it is not too thick), but if you have any doubts about any extra adjustment, comment here.
– Bacco
That’s magic. :)
– zentrunix
Then I’m gonna go through this code Nice and easy
– hugocsl