gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)
code in html with script to showcase the use of rollover and rollback
- User
code in html with script to showcase the use of rollover and rollback
- Assistant
To demonstrate rollover and rollback effects using HTML, CSS, and JavaScript, we can create a simple example where an image changes when hovered over (rollover) and reverts back when the mouse is moved away (rollback). Here is a basic implementation:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Rollover and Rollback Example</title> <style> #image-container { width: 300px; /* Set your desired width */ height: 200px; /* Set your desired height */ overflow: hidden; } #image-container img { width: 100%; height: 100%; transition: transform 0.3s ease; } </style> <script> function rolloverEffect(element) { element.style.transform = 'scale(1.1)'; // This scales the image up } function rollbackEffect(element) { element.style.transform = 'scale(1)'; // This resets the image scale } </script> </head> <body> <div id="image-container"> <img src="https://via.placeholder.com/300x200.png" alt="Placeholder Image" onmouseover="rolloverEffect(this)" onmouseout="rollbackEffect(this)"> </div> </body> </html>Explanation:
-
HTML Structure: We have a
divcontainer (#image-container) holding animgelement with a placeholder image. You can replace the URL with a path to your image. -
CSS:
- We define the dimensions of
#image-containerto maintain a consistent size and hide any overflow. - The
imginside the container is set to fill the container, maintaining the rollover and rollback effects within bounds. transition: transform 0.3s ease;allows for smooth animation during the rollover effect.
- We define the dimensions of
-
JavaScript:
rolloverEffect: This function scales the image up by changing the CSStransformproperty toscale(1.1)when the mouse hovers over it.rollbackEffect: This function scales the image back to its original size when the mouse leaves the image by reverting thetransformproperty.
This code presents a simple and effective way to add interactivity to images with rollover and rollback animations. Feel free to adjust the scaling factor and transition timings to suit your unique design needs.
-