What are some strategies to enhance the responsiveness of this JQuery code?

I'm facing an issue while trying to create a progress bar that dynamically adjusts based on the device's width.

In this particular scenario, whenever the device width changes, the progress bar's width remains static, causing it to break when switching to landscape mode.

It's worth mentioning that I have 2 progress bars stacked on top of each other.

UPDATE: A functional solution has been implemented, see below

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div_block-1545-2104" class="ct-div-block progress-bar"></div>
<div id="div_block-1545-2104" class="ct-div-block progress-bar2"></div>
$carousel.on("scroll.flickity", function (event, progress) {
    const $progressBar = $(".progress-bar");
    const divWidth = $(".progress-bar2").width();

    progress = Math.max(0, Math.min(1, progress));
    $progressBar.width(progress * divWidth);
});

Answer №1

Managing changes in width based on device properties such as landscape or portrait orientation is most effectively handled through CSS, but it is essential to connect with the window-resize event. Consider utilizing throttle/debounce techniques as well, as these events like resize and scroll may trigger repeatedly in quick succession.

$(window).on('resize', function(e){ $progressBar.width( Math.max(0,Math.min(1,progress))*divWidth); } )

For better understanding:

var $progressBar = $('.progress-bar'),
    divWidth = $('.progress-bar2').width();

$carousel.on('scroll.flickity', function(event, progress){
    console.debug( "the scroll.flickity event fired." );
} );
$(window).on('resize', function(e){ 
    console.debug( "the window:resize event fired. [throttle!!?!]" );
    progress = Math.max( 0, Math.min(1, progress));
    $progressBar.width(progress * divWidth);
} )

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

Instructions on how to load an HTTP file inside a Bootstrap modal

Is it possible to load a file located at an http:// address into a modal window? Something like this: <a class="btn btn-info btn-large" data-toggle="modal" href="#http://localhost/login.html"> <i class="icon-user icon-white"></i> Login ...

Utilize Mongoose to seamlessly integrate online shopping cart items into MongoDB

I am facing an issue while trying to insert a shopping cart of items in the form of a JSON object into a MongoDB collection using a mongoose schema. Although the customer's ID is successfully stored (extracted from the User DB), unfortunately, the ca ...

Adjust image source based on media query (CSS or JavaScript)

Is there a way to update the image src based on a media query with a maximum width of 515px? I'm considering using JavaScript, jQuery, or even AngularJS if there is an event equivalent to a CSS media query that can achieve this. I want to have differ ...

Issue encountered when attempting to pass more than one route parameter in a URL path

My goal is to retrieve data based on the values of 3 fields ("nkk", "nik", and "nama"). In the voters-ctrl: getVotersByParams = async (req, res) => { const nkk = req.params.nkk const nik = req.params.nik const nama = req.params. ...

Converting this Jquery function to vanilla Javascript

I'm currently encountering difficulties reworking this slideshow feature using Javascript. Due to certain project limitations, I am required to switch back to pure JS. document.querySelectorAll(".fadein img:not(:first-child)").forEach(function(element ...

What is the best method for compressing and decompressing JSON data using PHP?

Just to clarify, I am not attempting to compress in PHP but rather on the client side, and then decompress in PHP. My goal is to compress a JSON array that includes 5 base64 images and some text before sending it to my PHP API. I have experimented with l ...

Is there a way to determine completion of page loading in an AngularJS application using JavaScript?

I am currently in the process of crafting Robot Framework tests to address specific use cases for an external AngularJS application. One of my requirements is the utilization of Python 3.5+ and SeleniumLibrary instead of the outdated Selenium2Library. In ...

Exploring the world of HTML5 speed testing: getting started

Can anyone provide guidance on how to begin with an HTML5 bandwidth test? I am trying to replicate a flash-based version and any recommendations would be greatly appreciated. ...

Persistent banner designed to accompany blog posts

I've been attempting to insert a banner ad that runs along the left side of my blog post. Unfortunately, all my efforts so far have caused it to display above the content, pushing the blog post down the page. You can view the live link here. Here i ...

Utilizing a jQuery click event to modify the placement of a table

I have a request regarding the tables within the #middlebox. I would like to be able to rearrange the table positions by clicking on the list items. For instance, if the current order of the tables is starter -> soup -> seafood, clicking on the #seaf ...

javascript utilizing underscorejs to categorize and aggregate information

Here is the data I have: var dates = [ {date: "2000-01-01", total: 120}, {date: "2000-10-10", total: 100}, {date: "2010-02-08", total: 100}, {date: "2010-02-09", total: 300} ]; My goal is to group and sum the totals by year like this. ...

Issue with the pluses and minuses in the Bootstrap accordion

Can someone explain why the panel-header displays all "-" instead of "+"? When I click on the panel, it changes all "-" to "+". This happens consistently, not just the first time the page loads. My CSS code: .panel-heading a:after { font-family: &ap ...

Limit the application of the map() function to a single column within a 2-dimensional array in JavaScript when using Find and Replace

I have been using Google Sheets for my work Recently, I came across a find and replace function that I found here. This function works perfectly when the array is generated from a single column of data. However, there are certain cases where I need to us ...

Utilizing PHP and jQuery to dynamically populate Google Maps with multiple markers

I am currently working on integrating a Google map with a database to dynamically display multiple markers using PHP and MySQL. Below is the code I have been using: <?php //This section retrieves data from the database for later use in jQuery //CREATE ...

django, the X-CSRFToken in the request header is improperly configured

Essentially, I have managed to successfully send a CSRF token to the frontend and store it in the cookie section of the application tab within the developer console of the browser with this code: @method_decorator(ensure_csrf_cookie) def get(self, ...

A marker popup in React with Leaflet closes immediately upon clicking when using leaflet-pixi-overlay

Currently, I am developing a leaflet map using React and PixiOverlay for better performance while drawing markers. However, I have encountered an issue with handling popups while working on the code below: The Marker's click event triggers correctly ...

Are you seeing empty squares in place of Font Awesome icons?

If the Font Awesome icons are displaying as blank squares in all browsers despite correctly linking the stylesheet and attempting to install the package through npm which results in a npm ERR! code E401, it can be frustrating. Even after checking the brows ...

arrange a collection within an array containing keys as strings

I am facing an issue with sorting an array of objects. I need to sort the 'list' by 'score' in descending order. var list =[{ '440684023463804938': { score: 6, bonuscount: 2 }, '533932209300832266': { score: 20, b ...

Encountering the error message "Uncaught Promise (SyntaxError): Unexpected end of JSON input"

Below is the code snippet I am using: const userIds: string[] = [ // Squall '226618912320520192', // Tofu '249855890381996032', // Alex '343201768668266496', // Jeremy '75468123623614066 ...

Leveraging the existing URL (including a unique identifier) for a jQuery AJAX call

After searching through similar questions on SO, I couldn't find anyone facing the same issue. I am using jQuery to make an ajax request without specifying the entire url parameter. If, for example, I am on the page www.foo.com/bar and want to send a ...