What is the best way to align the progress bar near the element that initiated the task?

I am currently working on a dynamic website with jQuery, utilizing AJAX operations extensively. Whenever an AJAX operation is initiated, a progress bar is displayed in the center of the webpage.

However, I am facing an issue where visitors find it cumbersome to track the progress bar located far away from the clicked element. They have to constantly shift their focus between the element and the progress bar, which can be quite distracting.

What would be the optimal way to position the progress bar near the triggering element?

My initial approach was to set up a handler that executes when the next AJAX request kicks off, like this...

$(document).on('ajaxStart', ()=>{
  $('#parentOfBar').append("<div id=progressBarElem style='display:none;position:absolute;top:50%;left:50%;padding:2px;' ></div>");
  $('#progressBarElem').html('A moment, please wait...');
  $('#progressBarElem').progressbar({value: false});
  $('#progressBarElem').show();
});

Upon launching an AJAX operation, the progress bar emerges at the center of the screen as planned using CSS. However, I aim for it to be displayed next to the element that triggers the AJAX operation, for instance, by clicking it.

I recently discovered that instead of appending the div element, which then becomes the progress bar as a child of a fixed element, I can resolve this by making it a sibling of the active element and removing its position CSS attribute as shown below...

$(document).on('ajaxStart', ()=>{
    $focusedElem = $( document.activeElement ) //the currently focused element
    $focusedElem.parent().append("<div id=progressBarElem style='display:none;top:50%;left:50%;padding:2px;' ></div>");
    $('#progressBarElem').html('A moment, please wait...');
    $('#progressBarElem').progressbar({value: false});
    $('#progressBarElem').show();
});

By implementing this adjustment, the progress bar now appears alongside the triggering element. However, this method may disrupt the layout of neighboring elements when the progress bar manifests.

Do you think this is the most effective solution, or is there a more efficient approach to identify the element that initiated the AJAX request without solely relying on the focused element?

Answer №1

If you want to position a progress bar around an element, you'll need to utilize the click event function. This allows you to specify what action should be taken when the element is clicked. By setting the position of the progress bar relative to the element that was clicked, you can achieve the desired effect. You can achieve this by making adjustments inside the click function using the $(this) keyword to reference the current element's position.

Check out this code snippet that demonstrates progress bar repositioning:

  // Event listener for the click event on the example element
  $('#example-element').click(function(event) {
    // Display the progress bar
    $('#progress-bar').show();
    
    // Capture the position of the clicked element
    var elementPos = $(this).offset();
    var elementWidth = $(this).outerWidth();
    var elementHeight = $(this).outerHeight();
    
    // Calculate the new position for the progress bar
    var progressBarLeft = elementPos.left + (elementWidth / 2) - ($('#progress-bar').outerWidth() / 2);
    var progressBarTop = elementPos.top + (elementHeight / 2) - ($('#progress-bar').outerHeight() / 2);
    
    // Reposition the progress bar
    $('#progress-bar').css({
      left: progressBarLeft,
      top: progressBarTop
    });
  });

Once the AJAX functionality is complete, you can return the progress bar to its original position.

Hopefully, this explanation is helpful to you!

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

Ways to retrieve an array following a function call

After a lot of testing and troubleshooting, I finally got my array to function properly in the console. However, when I click the button, the array is displayed on the console but not in my HTML. TS: jogar(){ for(var u=0;u<6;u++){ this.y ...

"Exploring the world of CSS3: Arrow shapes and animated effects

Looking at the image provided, my task is to create and animate it. I was considering using CSS3 border-radius instead of an actual image. Below are the HTML and CSS code that I have so far. The animation I envision involves the arrow gradually appearing ...

How can a loop be created to run indefinitely until the user exits the webpage?

So, on my website I created a little interactive slideshow using Jquery. I would like the animations to keep looping until the user navigates away from the page. Below is the snippet of Jquery code I am using: var t2 = setTimeout(function(){ $("#wantawebs ...

Capturing C# log data for JavaScript interactions

Can anyone provide recommendations on how to capture JavaScript interactions in a browser using C#? I am interested in developing a web crawler that can track these interactions, allowing me to search for potentially harmful calls. ...

Incorporating CSS into an HTML document with Jersey/Grizzly

This is the structure of my project: src/main/java -- main.java src/main/resources -- webapp -- -- css -- -- js -- -- images -- -- pages -- -- -- home.html Currently, my Maven project utilizes Jersey and Grizzly. I am able to call my REST services and lo ...

Library of CSS styling information

Has anyone come across a reliable javascript library that can retrieve the original style (not computed) of a specific element in the DOM? Essentially, I am looking for something that can provide results similar to what is displayed in Firebug's style ...

Bookshelf.js has implemented a new feature where it automatically encrypts passwords with bcrypt every time data is updated

My User Model is var Bookshelf = require('../../db').bookshelf; var bcrypt = require('bcrypt'); var Promise = require('bluebird'); var Base = require('./../helpers/base'); // Custom user model var Custom_User_Mod ...

How to extract a value from a span input textbox using Vue?

I just started using Vue and I'm attempting to create an auto-growing input. I've realized that it's not possible to create a real <input> element that adjusts its size based on its content and allows for value modifications using v-mo ...

What steps should I take to address the issue of sanitizing a potentially harmful URL value that contains a

I've encountered a URL sanitization error in Angular and despite researching various solutions, I have been unable to implement any of them successfully in my specific case. Hence, I am reaching out for assistance. Below is the function causing the i ...

Achieving opacity solely on the background within a div class can be accomplished by utilizing CSS properties such

Whenever I adjust the opacity of my main-body div class, it results in all elements within also becoming transparent. Ideally, I would like only the gray background to have opacity. See below for the code snippet: <div class="main1"> content </di ...

The striped color override feature in Bootstrap appears to be malfunctioning in Internet Explorer 8

I have come across several discussions regarding this topic with various suggestions. My current objective is to make the alternating rows in Bootstrap (2.3.2) appear darker in Internet Explorer 8. To achieve this, I attempted to override the style by impl ...

Unable to reach redux actions through props while using react router

As someone who is new to working with React and Redux, I've been encountering an issue with accessing my Redux actions from the props when using them alongside React Router. Despite trying various configurations, the props only seem to contain the Rea ...

Avoid altering the state directly; utilize setState() to update it instead. Remember to follow react/no-direct-mutation

Here's the code snippet I'm working with: constructor(props) { super(props) this.state = { loginButton: '', benchmarkList: '' } if (props.username == null) { this.state.loginButton = &l ...

propagate the previous state using a variable

Currently, I am in the process of refactoring a codebase but have hit a roadblock. My main aim is to update the state when the onChange event of a select box occurs. Specifically, the parameter searchCriteria in my handleFilterChange function is set to in ...

How to make an AJAX request in jQuery / JavaScript after a specific time interval has passed

Here is the code snippet I'm working with: $('input.myinput').each(function(){ var id = $(this).val(); var myajax = function(){ $.ajax({ url: ajax_url, type: "GET", data: ({ code: id ...

Hidden form in JavaScript does not submit upon clicking the text

My previous question was similar to this but with a smaller example. However, the issue with that code differs from my current code. (If you're curious, here's my previous question: JavaScript Text not submitting form) Currently working on my fi ...

Click on the nearest Details element to reveal its content

Is there a way to create a button that can open the nearest details element (located above the button) without relying on an ID? I've experimented with different versions of the code below and scoured through various discussions, but I haven't be ...

Ways to shorten text on mobile screens using CSS

Is it possible to use CSS to truncate text based on the current screen size or media queries? For example, consider this paragraph: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam porta tortor vitae nisl tincidunt varius. Lorem ipsum dolor ...

Stylesheets may not display correctly in Firefox and IE

Just wanted to say that I recently put together this little website , but I am completely stumped on why the CSS file won't show up in Firefox or IE. I know it's a basic question, but I'm still learning all of this. Thank you so much for a ...

cancel the ongoing ajax request during a specific event

There seems to be an issue with the behavior of clicking on the .personalized class. When clicked, it does not display both #loading_personalized and #divPersonalized elements simultaneously. This results in the execution of an AJAX call even when the pr ...