Skip to content
The Chat Archive
gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)

code in html with script to showcase the use of rollover and rollback

  1. User

    code in html with script to showcase the use of rollover and rollback

  2. 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:

    1. HTML Structure: We have a div container (#image-container) holding an img element with a placeholder image. You can replace the URL with a path to your image.

    2. CSS:

      • We define the dimensions of #image-container to maintain a consistent size and hide any overflow.
      • The img inside 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.
    3. JavaScript:

      • rolloverEffect: This function scales the image up by changing the CSS transform property to scale(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 the transform property.

    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.