Modify the background color of one div based on the visibility of another div

My carousel consists of three divs representing a Twitter post, a Facebook post, and a LinkedIn post. These are contained within another div called #social-media-feeds.

I am curious if it is feasible to adjust the background color of #social-media-feeds depending on which div in the carousel is currently displayed.

For instance, when the Twitter div is showing, I would like the background color of #social-media-feeds to be #00aced. Similarly, when the Facebook div is displayed, the background color should change to #3b5998, and when the LinkedIn div is visible, the background color should become #007bb5.

If this customization is possible, I would greatly appreciate some guidance. Thank you!

Answer №1

This code may not be perfect, but it gets the job done. It's hard to say what your code looks like, so I hope this example can offer some guidance:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            html, body {
                height: 100%;
                padding: 0px;
            }
            #social-media-feeds {
                display: inline-block;
                height: 100%;
                width: 100%;
            }
            .post {
                display: none;
            }
            #leftButton {
                position: absolute;
                top: 50%;
                left: 0%;
                height: 30px;
                width: 60px;
                text-align: right;
                background-color: gray;
                cursor: pointer;
            }
            #rightButton {
                position: absolute;
                top: 50%;
                right: 0%;
                height: 30px;
                width: 60px;
                background-color: gray;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div id="leftButton"><</div>
        <div id="rightButton">></div>
        <div id="social-media-feeds">
            <div id="facebook" class="post">Facebook</div>
            <div id="twitter" class="post">Twitter</div>
            <div id="linkedIn" class="post">LinkedIn</div>
        </div>
        <script>
            var socialMediaFeedsDiv = document.getElementById('social-media-feeds');
            var backgroundColors = ["#3b5998", "#00aced", "#007bb5"];
            var posts = document.getElementsByClassName('post');
            var index = 0;
            posts[index].style.display = 'inline-block';
            socialMediaFeedsDiv.style.backgroundColor = backgroundColors[index];    
            var leftButton = document.getElementById('leftButton');
            leftButton.addEventListener('click', function() {
                posts[index].style.display = 'none';
                index--;
                if (index < 0) {
                    index = posts.length - 1;
                }
                posts[index].style.display = 'inline-block';
                socialMediaFeedsDiv.style.backgroundColor = backgroundColors[index];
            });
            var rightButton = document.getElementById('rightButton');
            rightButton.addEventListener('click', function() {
                posts[index].style.display = 'none';
                index++;
                if (index > (posts.length - 1)) {
                    index = 0;
                }
                posts[index].style.display = 'inline-block';
                socialMediaFeedsDiv.style.backgroundColor = backgroundColors[index];
            });
        </script>
    </body>
</html>

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

The problem of JSON data being displayed instead of redirecting is persistent

Here's a unique AJAX Login solution that I developed. $(function() { $('#loginform').submit(function(){ var uname = $('#username').val(); var password = $('#password').val(); $("#loading").show(); ...

Animate the sliding of divs using the JavaScript animation function

I've designed some boxes that function similar to notifications, but now I want to smoothly slide them in from the left instead of just fading in. I know that I need to use .animate rather than .fadeIn to achieve this effect. The code snippet I&apos ...

Implementing Default Language in Next.js 14 for Static Export without URL Prefix: A Step-by-Step Guide

Currently, I am in the process of developing a website using Next.js 14, with the intention of exporting it as a static site for distribution through a CDN (Cloudflare Pages). The website I am working on requires support for internationalization (i18n) to ...

Using setTimeout with drag and drop functionality in HTML

I need a setTimeout function to be triggered only after dragging and dropping an image into a division, not before the drop occurs. ...

Uploading video files using XMLHttpRequest encountered an error on a particular Android device

One of our testers encountered an issue where they failed to upload a video file (.mp4) on a specific Android mobile phone. Interestingly, the same file uploaded successfully on an iPhone and another Android device. To investigate further, I attempted to ...

Achieving functionality with a fixed header

I am struggling with the scroll functionality and smooth transition in my code for a sticky header. The scroll doesn't work smoothly, especially when the fixed header is activated. The #top-nav-wrapper barely scrolls as expected: <script> $(doc ...

Combining React with a jQuery plugin

Utilizing the jQuery nestable plugin in my React App has been a lifesaver for meeting my business needs. Despite being aware of the potential complications that arise from mixing jQuery with React, I couldn't find the exact functionality I required in ...

The setInterval function will run just one time when triggered by an onclick event

After watching a YouTube tutorial on creating desktop notifications, I had an idea to use it as a reminder tool. For example, having a specific reminder appear as a desktop notification every 30 minutes when triggered by clicking a button. However, the cod ...

Using asynchronous import statements in Vue allows for more efficient loading of components

I am using Vue and have a method in my file called load.js. async function loadTask(x) { return await x; // Some async code } export { loadTask }; In one of my Vue components, I call the method but encounter an issue where the use of await prevents the ...

Using Vuejs to dynamically render a select dropdown based on the initial selection made in the first dropdown menu

I am facing an issue with two drop-down menus. The second menu's choices need to be filtered and displayed based on the selection made in the first drop-down. The RoomID value from the first drop-down is used to filter an array of objects for the seco ...

Adding content to a paragraph using Jquery

I have 4 sets of data associated with a click-bind event. My goal is to retrieve the value from a hidden field in each set and display it in a paragraph elsewhere on the page when the corresponding checkbox is clicked. Currently, I'm focused on gettin ...

Can anyone point out where the mistake lies in my if statement code?

I've encountered an issue where I send a request to a page and upon receiving the response, which is a string, something goes wrong. Here is the code for the request : jQuery.ajax({ url:'../admin/parsers/check_address.php', meth ...

Mobile device scrolling glitch

I'm currently working on my website, which can be found at . After testing it on mobile devices, I came across an issue that I just can't seem to fix. For instance, when viewing the site on a device with 768px width, you can scroll to the righ ...

JS Emphasis on Scrolling Div

I'm facing an issue with a button that opens a scrollable div. When I try to use the arrow keys on the keyboard, they do not scroll the div as expected. Interestingly, if I click anywhere on the div, then I am able to scroll using the arrow keys. I ...

Having trouble sending values via POST request in Node.js using Express

Currently, I am in the process of learning how to use Express (v4) with Node.js. My main goal right now is to create a basic REST API. This API specifically focuses on one endpoint: /orders. The main functionality I am trying to achieve is the ability to r ...

tips on sending multiple data- id in a modal window

Let's say I have multiple order IDs $order_id='12345566778'; $prod_id='126778899'; $sell_id='373462562363'; When I select a particular order ID, I want to pass it to a PHP variable located in the modal body of a popup. ...

Creating HTML code from a template using different programs

Currently, I am in the process of developing a website using only HTML, CSS, and JS without the need for a backend. It is a straightforward site designed for presenting information. Each page follows a standard template consisting of a header, content area ...

Question: How can I use the jQuery datepicker to ensure that the selected date remains in the

I recently created a webpage using a combination of HTML, PHP, and jQuery. The PHP script is designed to load all data where the date matches the one selected in the text input field generated by the jQuery datepicker. However, I encountered a problem whe ...

Applying CSS transition delays to multiple images

I'm having trouble implementing a transition delay in my CSS. I've tried adding it in various places but it doesn't seem to be working as expected. This is my CSS: .imageBox { position: relative; float: left; transition ...

Encountering an issue while attempting to make changes and test a copied GitHub library - npm ERROR! version cannot be located

I'm new to the world of Github forking and pull requests. My goal is to fork a repository, make some changes, and test them out on a project before submitting a pull request. I've already forked the repository and made modifications, but I' ...