JSFiddle - React, Tailwind, and code Playground
by ckissi
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Button Animation</title>
</head>
<body>
<div class="content">
<!-- Add some content to make the page scrollable -->
<p>Scroll down the page to see the button animation effect.</p>
<div style="height: 2000px;"></div>
<button id="animatedButton" class="button">Click Me</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
/* styles.css */
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
.content {
padding: 20px;
}
.button {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
padding: 10px 20px;
background-color: #007BFF;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.5s ease-in-out;
}
.button.animate {
position: fixed;
top: 20px;
right: 20px;
left: auto;
transform: none;
}
JavaScript
// script.js
document.addEventListener('DOMContentLoaded', function () {
const button = document.getElementById('animatedButton');
window.addEventListener('scroll', function () {
const buttonPosition = button.getBoundingClientRect().bottom;
if (buttonPosition < window.innerHeight) {
button.classList.add('animate');
} else {
button.classList.remove('animate');
}
});
});