Is the scrollWidth affected by changes in the width of a div element?

One question arises: could the scrollWidth value possibly change even if the content remains constant, but the width of the div is altered (such as during a window resize event)? It seems unlikely, yet I am experiencing unexpected issues.

If it should indeed change, then there seems to be an issue with the scrollWidth not updating correctly when the div width changes.

In my scenario, there is a situation where a child div's width is set to the scrollWidth of its parent div through jQuery. There is also a panel placed adjacent to the parent div that can expand or collapse, resulting in changes to the parent div's width. Strangely, the scrollWidth of the parent div does not adjust accordingly when the panel opens or closes (which is somewhat understandable). However, upon triggering events like a window resize, the child div fails to resize properly based on the updated parent div width, leading to overflow issues (as depicted in the image). The scrollWidth property only updates when the parent element is toggled off/on with the #openAttrTable button in this case, and whether the panel is displayed affects the initial width of the parent div.

Essentially, I require the scrollWidth property to dynamically update every time the width of the parent div is modified, regardless of the div already being instantiated.

Several buttons manage the opening and closing of the side panel and attribute table, setting the scroll width accordingly based on their states. Upon clicking #display-map-info, manual adjustments are necessary by adding or subtracting pixels from scrollWidth to ensure proper alignment of the drag bar within the available space. The direct assignment of scrollWidth without pixel adjustments causes the drag bar to overlap other elements within the div (refer to the image).

// Script for animating the layers panel slide-out
$('#display-map-info').click(function () {
    $('#map-functions').toggle("slide", { direction: "right" }, 500);
    $('#map-functions').toggleClass("visible");
    // Adjusts the width if the attribute table is open and the layers panel is expanding
    if ($("#attribute-table").hasClass("open") && $("#map-functions").hasClass("visible")) {
        $("#attribute-table").animate({ width: '-=250px' }, 500);
        var width = $("#attribute-table")[0].scrollWidth;
        $("#attrTable-handle").css("width", width);
        $("#attrTable-handle").animate({ width: '-=250px' }, 500);
    }
    // Adjusts the width if only the attribute table is open
    else if ($("#attribute-table").hasClass("open")) {
        $("#attribute-table").animate({ width: '+=250px' }, 500);
        var width = $("#attribute-table")[0].scrollWidth + 250;
        $("#attrTable-handle").css("width", width);
    }
});
// Opens/closes the attribute table on button click and adjusts the width accordingly
$("#openAttrTable").click(function () {
    if ($("#map-functions").hasClass("visible")) {
        $("#attribute-table").css("width", "100%").css("width", "-=300px");
        $("#attribute-table").toggleClass("open");
        $("#attribute-table").toggle();
        var width = $("#attribute-table")[0].scrollWidth;
        $("#attrTable-handle").css("width", width);
    }
    else {
        $("#attribute-table").css("width", "100%").css("width", "-=50px");
        $("#attribute-table").toggleClass("open");
        $("#attribute-table").toggle();
        var width = $("#attribute-table")[0].scrollWidth;
        $("#attrTable-handle").css("width", width);
    }
});

// Resize function to adjust the attribute table width based on the panels' states
$(window).resize(function () {
    if ($("#map-functions").hasClass("visible") && $("#attribute-table").hasClass("open")) {
        $("#attribute-table").css("width", "100%").css("width", "-=300px");
        var width = $("#attribute-table")[0].scrollWidth;
        $("#attrTable-handle").css("width", width);
    }
    else if ($("#attribute-table").hasClass("open")) {
        $("#attribute-table").css("width", "100%").css("width", "-=50px");
        var width = $("#attribute-table")[0].scrollWidth;
        $("#attrTable-handle").css("width", width);
    }
});

https://i.sstatic.net/OXK2b.jpg

#map-functions {
  position: absolute;
  display: flex;
  flex-direction: column;
  width: 250px;
  top: 115px;
  right: 50px;
  bottom: 0;
  padding: 9px;
  background-color: #fff;
  border: 5px solid orange;
  overflow-x: hidden;
}

#attribute-table {
    display: none;
    position: fixed;
    left: 0;
    height: 250px;
    bottom: 0 !important;
    top: auto !important;
    padding-top: 7px;
    background-color: white;
    z-index: 31;
}

#attrTable-handle {
    height: 5px;
    background-color: orange;
    top: 0;
}

HTML Structure:

<div class="visible" id="map-functions"></div>
<div id="attribute-table">
   <div id="attrTable-handle"></div>
</div>

Answer №1

Discovered a solution by utilizing the .width() method. Unexpectedly, this method retrieves the entire width of the div instead of just the visible portion on the screen. It functions similarly to scrollWidth, but it consistently updates rather than only in specific scenarios.

$(window).resize(function () {
    if ($("#map-functions").hasClass("visible") && $("#attribute-table").hasClass("open")) {
        $("#attribute-table").css("width", "100%").css("width", "-=300px");
        //Adjust drag-bar width
        var width = $("#attribute-table").width();
        $("#attrTable-handle").css("width", width);
    }
    else if ($("#attribute-table").hasClass("open")) {
        $("#attribute-table").css("width", "100%").css("width", "-=50px");
        //Adjust drag-bar width
        var width = $("#attribute-table").width();
        $("#attrTable-handle").css("width", width);
    }
});

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

Exploring the functionality of jQuery when posting a two-dimensional array

I am working with a 2-dimensional JavaScript array called 'records' and I need to insert its data into a database. I am using jQuery's post method with the following code: var data={"records" : records}; $.post('new_puplic_insert.php&a ...

Show the WooCommerce checkout shipping fields and eliminate the "Choose a different delivery address?" checkbox

Is there a method to eliminate the checkbox labeled "Ship to a different address?" on the woocommerce checkout page while still displaying the shipping fields? I have attempted the following: add_filter( 'woocommerce_cart_needs_shipping_address', ...

Expanding the padding within a box results in an increase in the overall height of the table row that encapsulates it

My goal is to create a table displaying marks with a box around each mark that expands slightly when hovered over to indicate activity. Below is the code I am using: .container { position: absolute; width: 80%; left: 50%; transform: translateX(-5 ...

Achieve a full-page height navigation bar using CSS

Currently in the process of building a website utilizing CSS and HTML, I have successfully added a navigation bar to the left side of the page using this specific CSS code. However, an issue arises when the screen is scrolled down as the navigation bar doe ...

Automobile box sliding smoothly in a horizontal motion, gliding gracefully from left to

I currently have three boxes that are functioning as expected. However, I am looking to automate this process to run every 30 seconds without requiring a 'click' event. You can refer to this fiddle http://jsfiddle.net/ykbgT/8493/. Any suggestions ...

Execute Two Functions When OnClientClick is Triggered

This is a snippet of my current code in progress <asp:LinkButton ID="lnkDel1" Text="Delete" runat="server" OnClientClick="return confirm('Delete this alert?','Confirm');" onClick="lnkDelete1_Click" CssClass="del_lnk" CommandArgumen ...

Problems with select tag change events

I encountered an issue with select tag onChange events. When I select a value from the select tag, it should display in a textbox that is declared in the script. It works perfectly when I remove the class "input" from the select tag, but I prefer not to re ...

The dimensions of the div are fixed and contains some text

Can someone help me with my coding issue? I created a custom drop-down menu, but the problem is that my content div does not expand to 100% of the width. It seems to be stuck at 160px. Here is the CSS code snippet: .dropdown-content { display: none; po ...

Background image not looping in SQL database filepath

I have an image uploaded to a specific folder, and the file path is stored in a table along with other information such as title and description. I want to display this information within a repeated div element on the webpage. While the other variables loo ...

Utilizing Node.js to dynamically inject variables in SASS compilation

I am currently working on an application where I need to dynamically compile SASS before rendering it on the client side (a caching system is in the works, so no worries there). At the moment, my tool of choice is node-sass and so far, everything is runnin ...

Fetching JSON data with Ajax is successful, but the information is not being displayed

I am struggling to showcase Json data from a URL in a table using ajax. The aim is for the code to iterate through the data and present it neatly in a table. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/a ...

Storing Form Input in Browser's Local Memory

I am currently working on a form section where individuals can input their email addresses. However, I have encountered a couple of issues: (1) After submitting an email address, the page refreshes. While I understand that this is inevitable without usin ...

jQuery cycle hovers above all other elements on the page

Hello, I'm currently experiencing an issue with replacing my Flash video with a jQuery cycle on massageeducator.com's home page. Even though the cycle is functioning properly, the images are not staying within the editable region as intended - in ...

What is the best way to trigger the download of an image file created by PHP to a user's computer?

My PHP code (upload.php) allows users to upload an image from index.html, resize it, add a watermark, and display it on the same page. Users can download the watermarked image by using the 'Save image as...' option. The resized image is saved in ...

What is the best way to retrieve AJAX response, specifically data, and store it in a PHP variable located beneath

Is there a way to store the data received from an AJAX response in a PHP variable located below this specific div? Essentially, I am looking for a method to access the AJAX response directly in PHP. <script> $(document).ready(function(){ ...

Revising the $.parseJSON Function in jQuery 1.9.1 to Match jQuery 1.8.3's Implementation

With the latest version of jQuery, 1.9.0, there have been changes in the implementation of $.parseJSON which is causing some issues for us. We relied on how earlier versions of jQuery handled null and empty strings, where it would return a null value inste ...

As soon as I shrink the browser window, the HTML content becomes restricted and the div elements elegantly slide across the

Encountering this issue for the second time has been quite perplexing. Initially, I attributed it to my lack of experience in the field as a rookie intern when seeking assistance from colleagues at work with no luck in resolving it. The problem arises when ...

Tips for Retrieving and Modifying Bounds in Google Maps to Add or Remove Markers from a List

I'm currently facing a challenge where I need to retrieve the Bounds and Change Bounds in order to add and remove Marker information on my Added List below my Google Map. Similarly, with the Zoom change, I need to make adjustments because I am utiliz ...

Adjusting Card Size both Vertically and Horizontally in Bootstrap

After struggling to align my bootstrap cards for what feels like forever, I've decided to seek help by creating this post. Hopefully someone can assist me! Initially, the cards were all the same width and stacked correctly, but the height was not con ...

Issue with the exit animation on D3.js bar chart not functioning as expected

Within my D3.js bar chart, I want to implement a functionality where clicking on a specific p tag labeled: click here to remove bar would trigger the removal of the leftmost bar. To achieve this, I envision the bar flying off to the left side of the sc ...