How can i combine a css animation and a button?🤔🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 16

How can i combine a css animation and a button?🤔🤔

I want to make that when I click on the button the animation should start.PLEASE TELLL

25th Feb 2017, 2:58 PM
Nikhil
Nikhil - avatar
10 Answers
+ 4
This do the trick: <input type="button" value="anim!"> <div></div> <style> div{ height:10px; background-color:red; } input[type=button]:focus+div { animation-name:hi; animation-duration:5s } @keyframes hi{ from {width:0px;} to {width:200px;} 0% {background-color:red;} 50% {background-color:blue;} 100% {background-color:green;} } </style> ... obviously, you need to better target if you have others elements in page ^^ And the default of this pure css way, is that the button must be before the element you want animate, and be parent or 'brother' of it ( not child, or 'nephew' )... ( limitation of css selectors ) And unfortnally, to repeat the animation, you need to make button keep focus before regiving to it ( you need to click somewhere else before re-clicking the button ), and you stop brutally the animation if you click outside the button before ending animate :P Really, in the case of button use interactivity, you need often to handle that with JS... ( through click event, adding a class or the animation property, an remove it when animation end: there's also events for css animation ;) )
25th Feb 2017, 6:02 PM
visph
visph - avatar
+ 20
button:active{animation-name:ani;....} @keyframes ani{.....}
25th Feb 2017, 3:00 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 13
hey visph your code is absolutely the one I wanted But how can I make the line to stop where the animation stopped. like it starts from 0px and ends to 200px but after the animation stops not comeback to its initial position(I.e. 0px).
26th Feb 2017, 3:09 AM
Nikhil
Nikhil - avatar
+ 13
I did this in my code using Jquery https://code.sololearn.com/W13b9ype9wAM/?ref=app
8th Apr 2017, 1:03 PM
Michael Foster
Michael Foster - avatar
+ 12
Can you do this in this code:- HTML-- <div></div> CSS-- div{ height:10px; background-color:red; animation-name:hi; animation-duration:5s} @keyframes hi{ from {width:0px;} to {width:200px;} 0% {background-color:red;} 50% {background-color:blue;} 100% {background-color:green;} } IT GIVES A LINE MOvING BUT I WANT TO START IT WITH A BUTTON.
25th Feb 2017, 3:07 PM
Nikhil
Nikhil - avatar
+ 10
Offtopic: How is it possible that questions gets widely more 'thumb up' than answers @@ ... ( this doesn't required answer, it's just a personnal thinking ^^ )
8th Apr 2017, 12:24 PM
visph
visph - avatar
+ 10
Yes sure... I know it's because of perversion by missuse of them ( I wrote that's doesn't required answer ;) )... But you prefer mark as 'best answer' a likestormed one ( I respect @VH anyway ), rather than the one I provided and you have commented as "hey visph your code is absolutly the one I wanted" or the complementary one I wrote for your second one more accurate question :P ( I don't really care for that specifically for this thread and my own case, but for a globaly best good working of SoloLearn Q&A section ^^ )
8th Apr 2017, 12:52 PM
visph
visph - avatar
+ 7
You mean that questions are found having more likes as compared to answers ? I think b'coz of likestorm I asked a question "Do you like the Likestorm" The one who answered this got it....In likestorm everybody tries to give as much notifications as he/she can by giving likes to his /her posts and codes
8th Apr 2017, 12:39 PM
Nikhil
Nikhil - avatar
+ 7
Sorry man !! I just saw the top answer and marked it as best(that answer was right so i thought to mark as "best" ) but now i saw all your help to me thanks for that 😁😁
8th Apr 2017, 12:57 PM
Nikhil
Nikhil - avatar
+ 4
You can change the style applied to the div before and after animation... Before, set 'div' rules as you want. After, set the 'input:focus+div' rules. But 'before' state, will remain the after-after, when button will lost focus ^^ ( you can handle all that stuff more properly with JS ) You can also try the expriemental property 'animation-fill-mode', but as an 'experiemental' one, it's not supposed to be implemented in all brothers, or by same ways if it is...
26th Feb 2017, 3:44 AM
visph
visph - avatar