Modifying the style of an element with jQuery makes it difficult to change it later using CSS

Initially, I have a main panel hidden and set to slide out when the website is running. The CSS styling for this panel is as follows:

.contentPanel {    
    top: calc(40% - 1px);    
    height: 50%;  
    padding:10px;
    font-size:1.2em;
    overflow-y: scroll;
    overflow-x: hidden;
    left: 20%;
   width: 75%;
}

.contentPanel{

   transition: transform 5000ms cubic-bezier(0.095, 0.725, 0.020, 1.005); 
}

As the website starts running, jQuery calculates the window's width and hides the panel by translating it out of view on the X-axis:

  $('.contentPanel').css('left',window.innerWidth+"px");

Then, in theory, CSS is supposed to move the panel back to its correct position by adding the .expanded class:

$('.contentPanel').addClass('expanded');

This should translate the panel back to its original position at 0% on the X-axis:

.expanded {
    transform: translate(0%);
}

However, there is a problem where CSS does not take effect because jQuery styles the .contentPanel directly on its HTML tag, like so:

<div class="contentPanel expanded" style="left: 1366px;">

Even though a translateX(0%) is added in the CSS, nothing appears to happen since the pixel value is given in the HTML attribute.

Answer №1

To simplify the process, I suggest creating a wrapper with 100% width by default that can be used to hide the .contentPanel effortlessly. With this approach, you will only need to toggle the class without having to deal with $.css() at all.

For a visual representation and code demonstration, please check out the following link:http://jsfiddle.net/M8PQR/

Here is an example of how you can implement this:

<div class="wrapper">
    <div class="contentPanel"></div>
</div>

<a class="toggle">show/hide</a>

In terms of JavaScript, you can use the following snippet:

var $wrapper = $('.wrapper');

$('a.toggle').on('click', function() {
    $wrapper.toggleClass('expanded');
});

And for the CSS styling:

.contentPanel {    
    top: calc(40% - 1px);    
    height: 50%;  
    padding:10px;
    font-size:1.2em;
    overflow-y: scroll;
    overflow-x: hidden;
    margin: auto;
   width: 75%;
    background: deepSkyBlue;
}

.wrapper{
    transform: translate(100%);
    transition: transform 5000ms cubic-bezier(0.095, 0.725, 0.020, 1.005);
}

.wrapper.expanded {
    transform: none;
}

.toggle {
    cursor: pointer;
}

Answer №2

In the world of CSS, inline style reigns supreme. To ensure that a class called "expanded" takes priority over any existing left property, clear out the left property before adding the class:

$('.contentPanel').css('left',''); 
$('.contentPanel').addClass('expanded');

Answer №3

Have you considered changing left to auto before applying the .expanded class?

$('.contentPanel').css('left', 'auto').addClass('expanded');

Although, I must say, Michael's solution seems to be the most effective.

UPDATE

Another option is to set .contentPanel with transform: translateX(-100vw), which the original poster appears to prefer.

Viewport Width (VW) units are now widely supported according to current data. However, it's best to avoid using VH for now since it causes issues on mobile Safari.

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 border should not start at the left of the page; instead, I would like it to begin at the letter "T" in "Tech."

I am having an issue with the bottom border starting from the extreme left of the page. I want it to start from the letter "T" in Tech instead. #page-container { width: 1250px; margin: 0 auto; } h2 { font-weight: normal; padding-left: 15px; ...

Changing the font family for Bootstrap Glyphicons on a button

I have recently started using Bootstraps Glyphicons and encountered an issue while trying to insert text inside a button with a glyphicon icon: <button type="button" class="btn btn-default pull-left" data-toggle="collapse" data-bind="attr:{'href&a ...

Retrieving the title property with the power of JavaScript

https://i.sstatic.net/cp7qK.png My journey into JavaScript is just beginning, and I'm currently immersed in a project that involves using facial recognition technology to detect emotions. While the software successfully detects emotions, I am struggl ...

There is zero gutter in Bootstrap

Is it possible to use the bootstrap grid system without any gutters? I want to have 3 columns in one row like this: http://prntscr.com/6gpmhm. I have shared the markup for the CSS I am using, which is the default CSS of the framework. <section class ...

Switching effortlessly between Fixed and Relative positioning

As I work on creating a unique scrolling experience, I aim to have elements stop at specific points and animate before returning to normal scroll behavior once the user reaches the final point of the page. Essentially, when div X reaches the middle of the ...

How can I access a child div in the parent element that I am

<script type="text/javascript"> $(".inventory-notes").bind("click", function() { if($(this).text().length > 0) { alert($(this).text()); alert($(this).closest(".inventory-notes").children().text()); } }); </script> ex ...

Error in Google Translate API AttributeError

Despite changing the gtoken on googletrans stopped working with error 'NoneType' object has no attribute 'group', I am still encountering the "AttributeError: 'NoneType' object has no attribute 'group'" error. Howeve ...

Connect the scroll wheel and then proceed to scroll in the usual way

There are two div elements with a height and width of 100%. One has the CSS property top:0px, while the other has top 100%. I have implemented an animation that triggers when the mousewheel is used to scroll from one div to the other. The animation functi ...

Animation unveiling of text slides

I have been working on designing a unique button for my personal diet app project that involves a sliding text effect when the mouse hovers over it. I'm almost there, but I've encountered a major issue that seems impossible to solve. I want the s ...

Searching with jQuery autocomplete and displaying the results within a div

I have recently started using jQuery Auto Complete for the first time and managed to get it working. However, I am now looking to make some edits but finding it a bit challenging. My goal is to utilize the auto complete feature to search through a product ...

Having trouble passing variables via URL when using PHP and AJAX

I've encountered an issue while using AJAX to automatically update a web page every 5000 milliseconds. The problem arises when trying to retrieve data from the URL using $_GET or $_POST, as it only returns a value of 1. Below is an example code snippe ...

Instructions for altering the hue of a canvas square when the cursor hovers over it

I want to implement a feature where the color of a tile changes when the user hovers their mouse over it, giving it a whitened effect. The tileset I am using consists of 32x32 tiles. Below are the scripts for reference. MAP.JS function Map(name) { ...

Problem with IE8 compatibility in CSS

I'm currently working on a new website, but I'm encountering an issue with its display in IE8. The social bar is not aligning correctly and is floating down instead of staying to the right like it does in other browsers. This misalignment is caus ...

Ways to sort data based on dropdown selection in MVC Razor

I am facing a requirement where I have a list of vendors that are displayed on the page load. Additionally, there is a dropdown menu which acts as a filter for these vendor records when its value changes. This is my view: <div class="half-left"> ...

Bootstrap does not have the capability to add additional background layers

I'm currently working on building a website using Bootstrap, and I have three backgrounds stacked on top of each other within separate divs. However, I'm facing an issue where only the first background is showing up and the second and third backg ...

What methods can be used to center a Google map on a specific location?

I am facing an issue with three map canvases on different divs. Despite setting the center to the same position in all three of them using JavaScript, the second and third maps do not seem to reflect that center. Does anyone have any insights on what cou ...

Eliminating padding from columns results in the appearance of a horizontal scrollbar

I needed to eliminate the padding from the bootstrap grid column classes. So I went ahead and did just that .col-x { padding: 0; } However, this action ended up causing the entire layout to break as a horizontal scrollbar appeared. I've put to ...

Guide to setting the ng-selected value within AngularJS

I am currently working on a project where I have a select element in my AngularJS application. I am populating the options using JSON values with ng-repeat, and I want to display the first value from the JSON as selected by default. I have tried two diffe ...

Combining and consolidating JSON attributes

I am working with a JSON object that has multiple instances of the email property, like so: accounts": [ { "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="61120e0c04030e051821050e0c">[email protected]</a& ...

JS Code from a JS Fiddle failing to run in a web browser examination

I have been experimenting with a fantastic Expand/Collapse JavaScript function that I came across on JS Fiddle, but unfortunately, it's not working as expected when testing it in a browser. Can anyone provide some guidance on what might be causing the ...