Sup, guys!
Im learning css, and im trying to figure out how to animate a div with a separate button
I just can’t figure out how to use the button as a trigger
Here’s an example
<!-- html -->
<button id="mybutton">click me!</button><br>
<h1><div id="mydiv">hi</div></h1>
And my css
#mydiv{
border: 1px solid black;
height: 100px;
width: 100px;
background: green;
}
I didn’t add the keyframes and animations here, but what im trying to do when i press the button the div or "hi" slides from the bottom to top or something as long as it’s an animation
2
Answers
The HTML sets up a button and a container for the sliding div. Styling ensures the sliding div starts at the bottom, transitions on the transform property create the sliding effect, and the slide-up class moves the div upward. Clicking the button toggles the slide-up class, animating the div up, and a timeout returns it to the original position after the animation duration.
You’re going to need to use javascript to add an event listener to the button and apply the animation to the div.
CSS
HTML
JS
I made a JSFiddle for you to look at and build off of