Code for Moving Button:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Moving Login Button</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f9;
margin: 0;
}
.container {
background: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
position: relative;
width: 300px;
height: 300px;
overflow: hidden;
}
input[type="email"],
input[type="password"] {
display: block;
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
padding: 10px 20px;
font-size: 16px;
color: #fff;
background: #007bff;
border: none;
border-radius: 5px;
cursor: pointer;
position: absolute;
transition: top 0.4s ease, left 0.4s ease;
}
button:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>Login Form</h2>
<input type="email" id="email" placeholder="Email" />
<input type="password" id="password" placeholder="Password" />
<button id="login" style="top: 60%; left: 50%; transform: translate(-50%, 0);">Login</button>
</div>
<script>
const emailField = document.getElementById("email");
const passwordField = document.getElementById("password");
const loginButton = document.getElementById("login");
const container = loginButton.closest(".container");
function isFormComplete() {
return emailField.value.trim() !== "" && passwordField.value.trim() !== "";
}
function moveButton() {
const containerRect = container.getBoundingClientRect();
const buttonRect = loginButton.getBoundingClientRect();
const maxX = containerRect.width - buttonRect.width;
const maxY = containerRect.height - buttonRect.height;
const randomX = Math.random() * maxX;
const randomY = Math.random() * maxY;
loginButton.style.left = `${randomX}px`;
loginButton.style.top = `${randomY}px`;
}
loginButton.addEventListener("mouseover", () => {
if (!isFormComplete()) {
moveButton();
}
});
loginButton.addEventListener("click", () => {
if (isFormComplete()) {
alert("Logged in successfully!");
}
});
</script>
</body>
</html>