Picture loading using Ajax placed above the content area

Utilizing the Ajax Loading image referenced at this link to display a loading image during various stages of an ajax request (begin, complete, success).

I'm interested in learning how to position the image above all content and center it on the screen, similar to loading images commonly seen on websites.

<div id="ajaxloader" style="display: none;">
<h:graphicImage value="/resources/img/gload.gif"></h:graphicImage>
</div>

Ajax script

<script>
    jsf.ajax.addOnEvent(function(data) {
        var ajaxstatus = data.status; // Can be "begin", "complete" and "success"
        var ajaxloader = document.getElementById("ajaxloader");

        switch (ajaxstatus) {
            case "begin": // This is called right before ajax request is sent.
                ajaxloader.style.display = 'block';
                break;

            case "complete": // This is called after ajax response is received.
                ajaxloader.style.display = 'none';
                break;

            case "success": // This is called when ajax response is successfully processed.
                // NOOP.
                break;
        }
    });
</script>

Answer №1

Consider trying a mix of position:fixed and transform: translate. I am not familiar with the h:graphicImage element, so the example provided uses a standard img tag. The key aspect is the inline style on the ajaxloader div:

var ajaxloader = document.getElementById("ajaxloader");
var i = 0;

function toggleLoader() {
  var displayStyle;
  if (++i % 2) {
    displayStyle = 'block';
  } else {
    displayStyle = 'none';
  }
  ajaxloader.style.display = displayStyle;
}

document.getElementById("showLoader").addEventListener('click', toggleLoader);
<div id="ajaxloader" style="display: none; position: fixed; top: 50%; left: 50%; transform:translate(-50%, -50%); z-index: 1;">
<img src="https://placeholdit.imgix.net/~text?txtsize=14&txt=Loading...&w=100&h=100"/>
</div>
<button id="showLoader">Toggle load graphic</button>

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

Obtaining specific Google Image Search results depending on certain criteria

I am working on a project to create a web application that can suggest outfits based on the weather of a specific location. Here is where I am at with the project so far: https://codepen.io/anchpags/pen/bMpjxX <html> <head> <link rel=" ...

The "pointer" cursor style is simply not functioning in any way

Here is the angular template code I am working with: <!-- Modal --> <ng-template #levelsmodal let-c="close" let-d="dismiss"> <div class="modal-header"> Select the levels you want to show in the table and chart ...

ASP.Net ImageButton fails to trigger onclick event

I am currently experimenting with two ImageButton controls - one is created programmatically within a GridView_RowDataBound function, and the other is hardcoded on the client-side as shown below: Static ImageButton (Functional and triggers ImageButton_Cli ...

The header menu is not appearing on the gray bar in Internet Explorer

Having some issues with IE8, specifically on the website . The website header is not displaying correctly in IE8, but it works fine in Chrome and Firefox. Another issue is that the white area of the site is not 960px width as intended... ...

Executing JavaScript code upon successful form submission: An overview

I need help with my Asp.Net MVC web application. I am trying to implement a feature where some code runs on the successful response of an API method called upon form submission. Here is the current code snippet: @using (Html.BeginForm("APIMethod", "Confi ...

Jquery double-click Event Not Functioning Properly

I've been attempting to toggle the visibility of my footer navbar while also changing the chevron icon. When the navbar is hidden, I want the chevron pointing up to be displayed, and when the navbar is shown, I want the chevron pointing down to be dis ...

Issue with undefined Sass variable when using Global Sass variable in Vue 3 with Vite: [sass] Undefined variable error

https://i.sstatic.net/7K0uF.png Greetings! I have embarked on the journey of migrating a Vue application from Webpack to Vite. To kick things off, I am starting with a small project. However, I seem to be encountering an error: '[sass] Undefined var ...

Frequently refreshing a page to display the most recent information without needing to reload the entire

I am seeking a solution for updating the comments section on my website live or every 30 seconds. The comments are fetched from a MySQL database using PHP: <?php $link = mysql_connect('localhost', 'root', ''); ...

Changing the size of various types of images

Is there a way in JavaScript to automatically resize and fill in a block with fixed width using different images with various dimensions? I came across something similar, but it was in AS2. I'm unsure if it can be translated to JavaScript. var _loc3 ...

Delete the line-height property from the initial line

I have created text that is aligned using the "justify" option, and I would like to add some leading to the text. However, I want the first line of the text to remain at x:0px y:0px. Does anyone know how to remove the line-height from the first line of a ...

Troubleshooting the "h is undefined" issue with jQuery's ajaxForm

In my project, I have implemented a form that is displayed in an ajaxed modal. This form is used for updating user details. When the modal is loaded, a JavaScript function is called: teamCreate: function() { $j("#step1,form#createEditTeam").show(); $j("#s ...

Uploading Files Using Ajax to a PHP Class

Having some trouble with my file upload using AJAX to post to an object-oriented PHP class. Here's the AJAX code: var handleUpload = function(event) { event.preventDefault(); event.stopPropagation(); var fileInput = document.getElement ...

How can I use JavaScript to access my templates directory in Django 3?

Perhaps I am taking the wrong approach here. My goal is to dynamically replace the content within the <nav> element using jQuery.load() when a specific <div> is clicked by the user. However, I am encountering difficulty in accessing the HTML fi ...

Why does the result of parsing in Python through Ajax PHP differ from what is displayed on the screen?

Attempting to extract search results from a specific page proved to be challenging. After selecting the date range of 20-Nov to 22-Nov and filtering by the "財經" news category on the website, only 47 results were visible. However, running python php cod ...

Having trouble getting the sorting/search function to work with a click event to display content. It seems to only work with a single item - possibly an issue with the

Creating a function that involves multiple boxes with a search function. The search function is working for the most part, but when clicking on a result, certain content should display. function filterFunction() { var input, filter, ul, li, a, i; ...

Is it feasible to navigate through submenus using only the [Tab] key in CSS?

Is there a way to activate a submenu in a styled menu using only the [Tab] key so that the submenu becomes visible? I attempted to use the :focus pseudo-class ul li a:focus + ul li a ... and when t ...

Bind event listener exclusively for click event handlers

I have a scenario where I am using 'addEventListener' to handle the opening of a menu (genres-container) only after the transition on another menu (mobile-nav) has completed. The issue I am facing is that even after closing everything and trying ...

What is the difference between handling ongoing responses in jQuery.ajax using "success:" and ".done"?

After spending a few weeks working with jQuery and AJAX, I stumbled upon two distinct ways to 'continue' the script post call completion: success: and .done. The jQuery documentation provides a summary as follows: .done(): Description: Add handl ...

Hero Sticky Transition with 100vhDivElement

My code is giving me some trouble. I want the div with text to stay at the bottom of the hero section with a transparent background. When that div reaches the top, it should stick with a black background. The issue is that I have set it in the javascript v ...

Non-responsive image in Bootstrap 4

My struggle is with making my website fully responsive. Whenever I use the img tag with a width of 100% on a large screen, the image fits perfectly, but when the window is resized, a horizontal scroll appears, creating white spaces and causing the image ...