As two divs glide across the screen, the top div remains hidden from view

To enhance our user experience, we are tasked with creating a captivating screen saver featuring 3 images.

  • The bottom image will remain fixed in its position.
  • The middle image should move across the screen.
  • Lastly, the top image will move at a faster pace compared to the others.

In order to manage these images effectively, we have set up 3 separate divs as outlined below.

<!DOCTYPE html>
<html>
    <head>
        <title>Moving Screen Saver</title>
        <style>
            html, body {
                position: relative;
                height: 100%;
                width: 100%;
            }

            #bgimg {
                background-image: url("background/moon_bg.png");
                position: absolute;
                background-repeat: no-repeat;
                background-size: cover;
                width: 100%;
                height: 100%;
            }

            .animator {
                width: 100vw;
                height: 100vh;
            }

            #poster {
                background-image: url("posters/gladiator.png");
                position: absolute;
                background-position:right 20px bottom 20px;
                background-repeat: no-repeat;
                width: 100%;
                height: 100%;
                background-color: transparent;
            }
        </style>
    </head>
    <body>
        <div id="bgimg"></div>
        <div class="animator"></div>
        <div id="poster"></div>
        <script>
            var poster = document.getElementById('poster');
            var animate;

            function moveRight()
            {
                poster.style.left = poster.style.left || 0;
                poster.style.left = parseInt(poster.style.left) - 10 + 'px';
                requestAnimationFrame(moveRight);
            }

            moveRight();

            function rollAnimate(element, url) {
                if(!element instanceof HTMLElement)
                    return;
                var canvas = document.createElement('canvas');
                var ctx = canvas.getContext('2d');
                var img = new Image();
                var x = 0;
                var pattern;
                var framesPerSec = 10;

                img.onload = startDrawing;
                img.onerror = console.error;
                img.src = url;
                canvas.style.cssText = 'position:absolute;'

                function startDrawing() {
                    element.insertBefore(canvas, element.firstChild);
                    pattern = ctx.createPattern(img, 'repeat');
                    resize();
                    anim();
                    window.addEventListener('resize', resize, {passive: true});
                }

                function anim() {
                    x = (x - 1) % img.width;
                    ctx.setTransform(1,0,0,1,x,(canvas.height) - (img.height));
                    ctx.fill();
                    setTimeout(function() {
                        requestAnimationFrame(anim);
                        }, 1000 / framesPerSec);
                }

                function resize(evt) {
                    canvas.width = element.offsetWidth;
                    canvas.height = element.offsetHeight;
                    ctx.fillStyle =  pattern;
                    ctx.rect(0, canvas.height - img.height, canvas.width, img.height);
                    ctx.globalCompositeOperation = 'copy';
                }
            }

            rollAnimate(document.querySelector('.animator'), 'buildings/mod_cropped.png');
        </script>
    </body>
</html>
  • lower image - bgimg
  • middle image - animator
  • top image - poster

Currently, the bottom image and the middle image are displaying correctly, but the top image (poster) is not visible. It seems that when attempting to paint the middle image, it is overriding the top image. Can someone offer assistance in resolving this issue?

For reference, here is the corresponding jsfiddle page:

https://jsfiddle.net/n76epste/10/

Answer №1

Upon closer inspection, it appears that all 3 images are being retrieved successfully. However, the problem lies in the fact that the poster quickly moves off the screen and fails to reappear.

  • The bgimg remains stationary and is obscured by the animator (this becomes evident when the animator is removed)
  • The animator is continuously animated and overlaps the bgimage
  • The poster constantly updates with a style attribute of left: -***px", where * represents an increasing number. This movement can briefly be seen when the page is loading if the bgimg and animator are hidden using CSS

Answer №2

The issue was resolved by adjusting the Z-index of the poster dev to the highest level.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to connect a directive's attribute to a dropdown menu in Angular.js?

Within a dropdown, I have a selection of templates that are connected to $scope.templates: [{"id":1, "name":"Test 1",{"id":2, "name":"Test 2"}]. Furthermore, there is a directive in place, <editor data-template="1"></editor> The goal is to ...

Tips for adding an asterisk to the label of a Material-UI switch component

I am trying to include an asterisk symbol in the label by passing a required prop with a property, but it doesn't seem to be functioning correctly. <FormControlLabel control={ <Switch onChange={event => ...

What could be causing this excessive lag?

I've been developing a new website, but the "buttons" I'm using seem to be causing significant lag. I need help resolving this issue. You can view the website here: The problematic code snippet is shown below: <td> <a href="templi ...

Refresh the calendar to retrieve updated information

Utilizing the "events" elements of fullcalendar to retrieve data dynamically has been successful so far and I am satisfied with it. However, I am facing a challenge in passing a GET/POST parameter to the PHP page and refreshing the call to incorporate the ...

Node.js causing issues with retrieving data from REST Calls

I am trying to make a REST API call from my PHP and Node.js application to a specific URL provided by the client, which is expected to return a JSON object. While I am able to successfully retrieve data using PHP, I'm encountering issues with my Node ...

What Causes the Misalignment Between My Image and Text?

I am trying to randomly select a slide from the list of slides when the page loads, and then display it in the hero section of a website. However, I am facing an issue where the Image seems to be out of sync with the Text & Button (for example, displaying ...

Tips for altering the browser tab color on a PC or Mac with HTML or JavaScript

While I am aware of the method to change theme colors on mobile devices using <meta name="theme-color" content=" #0000ffbb" />, I prefer browsing on my laptop. Are there ways to customize browser tab appearance using HTML or JS fo ...

SapUI5: Implementing a toggle functionality to display/hide one list item based on another list item's action

UI5 is a versatile framework with numerous possibilities, but sometimes I find myself struggling to implement ideas that would be easier in traditional HTML. Here's the scenario: I want to create a List with ListItems that display cities like Berlin, ...

Is there a way to create a new prettyphoto window by clicking on a link within the original prettyphoto window?

I have an HTML table that is dynamically constructed on the server side using AJAX. The table is displayed using prettyphoto, everything works fine up to this point. However, the last column of the table contains a link that should open an image using pret ...

The division element nested within a select tag

HTML: <div class="row"> <div class="form-group"> <div class="col-xs-10"> <label for="class_code_reservation">Class Code</label> <select type="text" class="form-control" id="class_code_reservation" na ...

Issues of unfulfilled requirements in the recent npm (and node) updates

After updating npm and node to the latest versions, I am encountering the following errors and warnings: $ npm --version 2.4.1 $ node --version v0.10.36 $ npm install > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f895 ...

How can you make a Tempory Drawer wider in Material Components Web?

Increasing the Width of a Temporary Drawer in Material-components-web Greetings! I am currently facing an issue with Material-components-web In the provided demo here, I would like to extend the width of the Temporary drawer to accommodate additional co ...

Experiencing difficulties integrating react-moveable with NEXTjs: Error encountered - Unable to access property 'userAgent' as it is undefined

I've been grappling with this problem for the past few hours. I have successfully implemented react-moveable in a simple node.js app, but when I attempt to integrate it into a NEXTjs app, an error crops up: TypeError: Cannot read property 'userAg ...

Is it advisable to avoid using `&apos;` for escaping single quotes?

According to the insights shared in conversations about the popularity of single quotes in HTML and Jquery embedded quote in attribute, the Wikipedia page on HTML highlights that: The use of the single-quote character ('), as a way to quote an attri ...

Error message received from Express middleware: "Unable to modify headers after they have been sent."

I have created middleware for my Node Express app to perform the following tasks: Checking all incoming requests to determine if they are from a bot Allowing requests for raw resources to proceed Serving an HTML snapshot if the request is from a bot How ...

Using MongoDB and NodeJS to retrieve data from a promise

When I make a request to the database using promises, my goal is to extract the values of "latitude" and "longitude" for all documents and store them in an array. This is how I am currently approaching it: const promises = [ dbo.collection("users").f ...

Utilize Google Maps API to showcase draggable marker Latitude and Longitude in distinct TextFields

I am currently utilizing the Google Maps example for Draggable markers. My objective is to showcase the latitude and longitude values within separate TextFields, where the values dynamically change as I drag the marker. Once the user stops dragging the mar ...

Is there a way to save a base64 image to an excel file?

I need assistance with exporting Excel Charts from NVd3 using Angularjs. Here is the code I have been trying: (jsfiddle) <button id="myButtonControlID">Export Table data into Excel</button> <div id="divTableDataHolder"> <table> ...

Is there a specific HTML tag available to instruct the browser to cache my HTML and/or CSS files for future use?

Up to this point, my research has indicated that enabling JavaScript and CSS browser caching typically requires server side settings such as .htaccess. Is there any HTML tag or configuration within the HTML page or JavaScript that can instruct the browse ...

Code functions properly on local host but encounters errors when deployed to live server

My comment system was working fine on localhost using PHP, AJAX and JavaScript. However, after transferring my website to a live server, the code does not seem to be functioning properly. Here is an example of the script: <script type="text/javascript ...