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

Navigating the process of transferring a function to a functional component within React

Hey there, I'm fairly new to using React and TypeScript and I am facing a small issue with moving a function from the file App.tsx to a functional component called WordAddingForm.tsx. Any help would be greatly appreciated! Let's start with my Ap ...

Utilizing JavaScript to assign class names to dynamically created elements from objects

I specialize in javascript and am working on adding the CSS class .is-slideUp to each card__item element created from a data object, in order to achieve a sliding-up animation effect. Although the .is-slideUp class name appears in the console, it does not ...

Newcomers to Visual Studio Code facing a problem with images not displaying

As a beginner in all aspects, I am currently facing an issue with displaying an image on Visual Studio Code. The problem arose when I tried to create a separate folder for the image, which resulted in a cascade of subfolders that only made things worse eac ...

Enable the Chrome extension to interact with the RESTAPI

As I develop a Chrome extension and Rest API, my current focus is on their integration. However, I have some security concerns that need to be addressed. The purpose of the extension is to retrieve data from the API. This data is not personalized for any ...

What could be the reason for Angular to merge the element at index 0 of an array into a subarray instead of doing

After setting up the Array in my oninit function, I encountered an issue where one part of the array was functioning as intended while the other returned an error. this.tests = [{ status: 0, testresults: [{ name: 'test ...

Using Material-UI HOC with props传递

One of my custom components is calling another custom component created using withStyles. Here is the code snippet: import {FormControl, InputBase, InputLabel, withStyles} from "@material-ui/core"; import React from "react"; export const CustomInput = wit ...

Merging the outcomes of a JSON call

Presently, I have an async function that returns a JSON response in the form of an array containing two objects. Please refer to the screenshot. How can I merge these objects to obtain: [{resultCount: 100, results: Array(100)}] I attempted the followin ...

Creating a Stylish CSS Table Utilizing DIV Elements: Preventing the Top Row from Scrolling

Utilizing a pure DIV table with the display: table, table-row, table-cell format for styling, I have managed to make the entire table scroll except for the header row. However, I am in search of methods that can prevent the header row (1st row) from scroll ...

magnify within a div with a set width

Looking for a way to make a div both zoomable and scrollable within a fixed-width container using transform: scale(aScaleRatio) for zooming in/out. Check out this demo for achieving the zoom effect. However, the inner div seems to have trouble aligning to ...

The Socket IO server fails to broadcast a message to a designated custom room

I am currently working on developing a lobby system that allows players to invite each other using Socket io version 4. The process involves the client sending a request to create and join a room, followed by emitting messages to other clients in the same ...

Unable to set CSS image as background

Currently, I am in the process of developing a website using HTML 5, CSS, and JavaScript. However, I am encountering an issue with image manipulation on the site. I am attempting to make the image the background but every method I try seems to be lacking a ...

Removing buttons from a table row dynamically

Below is how I am adding the Button to Element: (this.sample as any).element.addEventListener("mouseover", function (e) { if ((e.target as HTMLElement).classList.contains("e-rowcell")) { let ele: Element = e.target as Element; let ro ...

Dealing with callback errors: error handling is anticipated

In my Vue web application, I have enabled ESLint and encountered an issue with the following code: myApi.get('products/12').then((prodResponse) => { state.commit('ADD_PRODUCT', {product: prodResponse.data}) }, error => { cons ...

The issue with Mongoose populate failing to populate

I'm facing an issue with populating my users' car inventory. Each car has a userId attached to it upon creation, but for some reason, the population process isn't working as expected, and no errors are being displayed. Let's take a loo ...

"Troubleshooting a JSON structure containing a complex array of objects with multiple layers

I've structured a complex array with JSON objects to allow users to customize background images and create unique characters. Below is the main code snippet: var newHead; var newBody; var newArm; var masterList = [ { {"creatureName":"Snowman ...

Utilizing React Native to dynamically generate buttons through a loop

I am currently working on retrieving data from the Eventbrite API. The information I am hoping to extract is the event names, which will then be added to a list. Within the render function, I aim to dynamically create buttons based on the number of event ...

What is the reason behind jQuery appending a closing tag to the end of the text when adding a table dynamically?

$('#all_locations').append("<table>"); $('#all_locations').append("<tr><th>City</th></tr>"); $.each(data, function(i, item){ $('#all_locations').append("<tr>"); $('#all_locatio ...

When interacting with the iframe in an Ionic3 app, it suddenly crashes

Greetings! I have integrated a flipping book URL inside an iframe: <ng-container> <iframe [src]="eUrl" id="flipping_book_iframe" frameborder="0" allowfullscreen="allowfullsc ...

The function generalChannel.send does not exist

I've recently started working on a discord bot and I'm facing an issue with getting it to greet everyone when it's turned on. Using the bot.channels.get method, I am able to locate the channel successfully, but when it comes to sending a mes ...

SignalR gets stuck on the 'Initiating start request' screen, halting all progress

SignalR has been causing some strange behavior for me lately. After doing some refactoring, I started experiencing connectivity issues. It seems like my code was just lucky to work before because it didn't follow the recommended practices. For example ...