Write me an HTML page that has a picture in the background and buttons. The buttons and the picture move up, down, right and left depending on the position of the mouse on the screen.
Assistant
Sure! Below is an HTML page that includes a background image and buttons. The background image and buttons will move up, down, left, and right depending on the mouse's position on the screen, creating an interactive effect.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive Background and Buttons</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
/* Background image */
background-image: url('your-image.jpg'); /* Replace with the path to your image */
background-size: cover;
background-position: center;
transition: background-position 0.1s;
}
.button {
position: absolute;
width: 100px;
height: 50px;
/* Style the buttons */
background-color: #3498db;
color: white;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: transform 0.1s;
}
</style>
</head>
<body>
<!-- Buttons -->
<button class="button" id="button1">Button 1</button>
<button class="button" id="button2">Button 2</button>
<button class="button" id="button3">Button 3</button>
<script>
// JavaScript code to handle mouse movement
document.addEventListener('mousemove', function(e) {
var x = e.clientX;
var y = e.clientY;
var width = window.innerWidth;
var height = window.innerHeight;
// Calculate the movement offset (-10 to 10)
var moveX = (x - width / 2) / (width / 2) * 10;
var moveY = (y - height / 2) / (height / 2) * 10;
// Move the background image
document.body.style.backgroundPosition = `${50 - moveX}% ${50 - moveY}%`;
// Move the buttons
var buttons = document.getElementsByClassName('button');
for (var i = 0; i < buttons.length; i++) {
var button = buttons[i];
button.style.transform = `translate(${moveX * 5}px, ${moveY * 5}px)`;
}
});
// Function to position buttons
function positionButtons() {
var width = window.innerWidth;
var height = window.innerHeight;
// Get buttons
var button1 = document.getElementById('button1');
var button2 = document.getElementById('button2');
var button3 = document.getElementById('button3');
// Position button1
button1.style.left = `${width * 0.3 - 50}px`; // Subtract half of button width to center
button1.style.top = `${height * 0.4 - 25}px`; // Subtract half of button height to center
// Position button2
button2.style.left = `${width * 0.5 - 50}px`;
button2.style.top = `${height * 0.5 - 25}px`;
// Position button3
button3.style.left = `${width * 0.7 - 50}px`;
button3.style.top = `${height * 0.6 - 25}px`;
}
// Position buttons on load and when the window is resized
window.addEventListener('load', positionButtons);
window.addEventListener('resize', positionButtons);
</script>
</body>
</html>
Instructions:
Replace the Background Image:
In the CSS section, find background-image: url('your-image.jpg');.
Replace 'your-image.jpg' with the path to your desired background image file. This can be a relative path or an absolute URL.
Save the File:
Save this code in a text file with an .html extension, for example, interactive.html.
Open in a Web Browser:
Open the .html file in a modern web browser (such as Chrome, Firefox, or Edge) to see the interactive effect.
How It Works:
Background Movement:
The background-position of the body is adjusted based on the mouse position.
As the mouse moves, a JavaScript function calculates offsets and updates the background position, creating a smooth panning effect.
Button Movement:
The buttons are absolutely positioned on the screen.
The transform CSS property is used to move the buttons relative to their original position.
The movement is proportional to the mouse position, giving a parallax effect where buttons move slightly differently from the background.
Event Listeners:
mousemove event listener detects mouse movement and triggers the background and button adjustments.
load and resize event listeners ensure buttons are correctly positioned when the page loads and when the window size changes.
Styling:
Basic styles are applied to the buttons for appearance.
Transitions are added to make movements smooth (transition: background-position 0.1s; and transition: transform 0.1s;).
Customizations:
Adjust Movement Sensitivity:
The multipliers in the moveX and moveY calculations can be adjusted to make the movement more or less pronounced.
javascript
var moveX = (x - width / 2) / (width / 2) * 10; // Change '10' to a different number
var moveY = (y - height / 2) / (height / 2) * 10;
Similarly, adjust the multiplication factors in button.style.transform.