Update the styling of a CSS div based on certain conditions

Is it possible to have a centered fullscreen main section at the very center of the webpage?

<div id="main"></div>

The main section will display a default web page:

<script>
$("#main").html('<object data="http://www.myweb.com"/>');
</script>

In addition, there are two hidden side navigation bars (one on the left and one on the right) that can be shown by clicking on buttons. These sidebars contain lists of links. When sidenavLeft is displayed, the main section will shift to the right, and when sidenavRight is shown, the main section will move to the left. By clicking on the links inside the side navigation menus, the default main page can be changed.

I'm trying to figure out how to instruct #main to move to the right when sidenavLeft is displayed and to the left when sidenavRight is shown. I believe CSS conditionals may be needed, but as far as I know, CSS does not support conditionals.

/* When sidenavRight is shown, push the main content to the left */
#main {
   transition: margin-right .5s;
}

/* When sidenavLeft is shown, push the main content to the right */
#main {
   transition: margin-left .5s;
}

What is the best way to achieve this using CSS, jQuery, or JavaScript?

Answer №1

Using jQuery:

if ($("#sidebarLeft").is(':visible')) {
   $("#main").css("transition", "margin-right .5s");
}

A more optimized approach would be:

.transition-right {
   transition: margin-right .5s;
 }

if ($("#sidebarLeft").is(':visible')) {
   $("#main").addClass("transition-right");
}

It's recommended to include this code within your button click handlers, so that triggering the button for show/hide also updates margins accordingly.

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 document inside the database is displayed in italicized text when it is stored in Firebase

try { // establish a new collection named 'pactLists' const pactListsCollection = collection(db, "pactLists"); // used for storing records of created pacts by users. // generate a unique id for the pact and set it as the invite c ...

deciphering JSON objects based on specific keys

Struggling to complete a seemingly straightforward task has left me feeling frustrated. In the header of my HTML page, I have an external car dealer API being called. At the bottom of the page, there is another external .js file containing a series of if- ...

Customizing error messages to display validation errors instead of default text errors in the Django UserCreationForm

I'm encountering an issue with the registration form on my website. The form itself is straightforward, but I'm facing a problem where if a username is already taken or if the two password fields don't match, Django doesn't respond afte ...

The npm outdated -g command is producing an error message that states "Unable to read the length property of undefined"

I am currently facing an issue while trying to check the version status of my npm installed global packages. When I run the command npm outdated -g --depth=0 in the terminal, I encounter the following error: npm ERR! Cannot read property 'length&apos ...

Performing a jQuery Ajax call with parameters to a URL that includes an underscore symbol

While attempting to perform a standard GET Ajax call to a URL such as http://localhost:3000/foo_bar, I encountered an issue. The call, using something like $.ajax({url: "/foo_bar", data: { my: "params" }}), worked perfectly in Chrome but failed in FF. Th ...

Tips for linking two php form files with identical ids and combining their information in a database:

My system consists of two transactions, namely Registration.php and req.php. The data from both transactions is stored in the "new" table within my database. Registration.php is the initial transaction followed by req.php. These PHP files are forms, each w ...

Tips for effectively utilizing Mongoose models within Next.js

Currently, I am in the process of developing a Next.js application using TypeScript and MongoDB/Mongoose. Lately, I encountered an issue related to Mongoose models where they were attempting to overwrite the Model every time it was utilized. Here is the c ...

What is the best way to retrieve JSON data in ReactJS through ExpressJS?

Exploring the realms of reactjs and expressjs, I am seeking guidance on how to extract data from reactjs and store it in a variable. My current achievement includes successfully executing res.send to display the data. app.get('*', (req, res) =& ...

Search with jQuery's fuzzy matching feature, highlighted in bold

I am in search of a plugin or script that can perform fuzzy searches and bold the matching parts. Here is an example with parents > key > results below: "Hello world" => "hel" => Hello world "Hello world" => "hewd" => Hello world I ca ...

Traversing JSON data in a recursive manner without definite knowledge of its size or nesting levels

Currently, I'm working on a chrome app that utilizes local storage. The backend returns JSON data which I then save locally and encrypt all the items within the JSON. I have multiple sets of JSON with different encryption functions for each set. I at ...

Generate a fresh collection of objects all sharing a common identifier within a given array

Looking for help transforming this schedules array to match the desired output. Any ideas? let schedules = [ {day: 'Sunday', time: '5:00 PM'}, {day: 'Monday', time: '4:00 PM'}, {day: 'Monday', time: &ap ...

What could be causing my JSON file not to display when the onclick event is triggered?

I am currently learning JavaScript and JSON, as I am enrolled in a class that focuses on these topics. Our latest project requires us to create a JSON file and use a button along with JavaScript to display its contents. I have spent countless hours trying ...

I am interested in utilizing a variable's value as an ID within a jQuery function

When working with jQuery, I often use a variable to store values. For example: var x = "ID1"; To utilize the value of this variable as an ID in my jQuery functions, I simply concatenate it as shown below: $('#' + ID1).val(); Thank you for you ...

Deleting elements from arrayA that do not exist in arrayB using JavaScript

Seeking guidance on a small project of mine. For example, I have these two arrays: chosenItems = ['apple', 'banana', 'cherry', 'date', 'kiwi'] availableFruits = ['apple', 'cherry', &ap ...

Tips for speeding up your website by preloading versioned files in HTML

Tools like Google Lighthouse and PageSpeed recommend preloading important requests using <link rel=preload> to enhance website performance. When it comes to static files with unchanging filenames, the process is straightforward. However, how can I ...

Having difficulty linking a click event to an Anchor tag within a React component

Here is the code snippet for ComponentA.js: This is the return statement inside the component return ( ...... <a id="MytoolTip" ...... <ComponentB content={ ` <div class="share_cart_tt ...

Integrating external information with components

Currently in my React application, I am fetching a list of stores by directly calling the API through the URL. const getStore = async () => { try { const response = axios.get( 'http://localhost:3001/appointment-setup/storeList& ...

Exploring the Functions of jQuery's setInterval and setTimeout

I'm encountering a small issue with my jQuery script. Essentially, I have a timer that cycles through a series of images on a setInterval. When I click on the right or left controller, it pauses the interval and transitions the images based on user i ...

jQuery functions correctly within jsfiddle, yet it encounters issues when utilized within WordPress

I've been experimenting with a jQuery script to grey out the screen. While it works perfectly on my jsFiddle link provided below, I'm having trouble getting it to work from within my WordPress plugin. Check out the working script on my jsFiddle ...

Enabling JavaScript functions to accept a variable number of arguments

I am looking to enhance the versatility of the function below by enabling it to accept multiple callbacks to other functions if they are specified in the arguments. $(function() { function icisDashBox(colorElem, thisWidth, thisHeight, completeCallBack ...