1

Im using ajax to allow a user to delete something on my page.

What html/css tricks can i use to prevent the user from clicking on anything on the page. What im after is some sort of transparent overlay with a spinner in the center of my screen.

In ajax i know i can use beforesend and success functions to show and hide the loading.

    $.ajax({
            beforeSend : function (){
             $('#loading').show();
            }
            success: function (result) {
            $('#loading').hide();


            }

        });
    }

Ive come across these spinners but they are not full overlays which prevents the user from clicking on anything and they are not centered. http://projects.lukehaas.me/css-loaders/

KayTokyo
  • 137
  • 2
  • 3
  • 12
  • If you can use bootstrap then there already an [answer](http://stackoverflow.com/questions/16487052/how-to-activate-the-bootstrap-modal-backdrop) close to what you are looking for. – TeaCoder Aug 13 '16 at 18:58

5 Answers5

1

As you can see in the source of loader in the LINK you provided in the question, CSS loader is achieved by applying a class to a div. Eg:-

<div class="loader">Loading...</div>

So, you may do:

$.ajax({
  beforeSend : function (){
    $("#some_div_id").addClass("loader");
  },
  success: function (result) {
    $("#some_div_id").removeClass("loader");
  }
});
Abhi
  • 4,123
  • 6
  • 45
  • 77
0

css solution:

body.loading { 
  pointer-events: none;
}
Artem Gorlachev
  • 597
  • 4
  • 9
0

One thing is preventing elements from being a target of pointer events using the pointer-events CSS property:

.no-click {
    pointer-events: none;
}

And then just add this class to elements that should not receive any pointer events (this includes all pointer events, like mouseenter and mouseleave as well). For example, you could add this to the body element to prevent all pointer interaction:

$.ajax({
    beforeSend: function () {
         $("body").addClass("no-click");
    },
    success: function (result) {
         $("body").removeClass("no-click");
    }
});
John Weisz
  • 30,137
  • 13
  • 89
  • 132
  • This has issues on Internet Explorer. See [stackoverflow.com/questions/17441810](http://stackoverflow.com/questions/17441810/pointer-events-none-does-not-work-in-ie9-and-ie10) – dystopiandev Aug 13 '16 at 19:17
-1

try toggling a bootstrap modal containing the spinner during the ajax request:

script

    function sendRequest(){
        $('#modal').modal('toggle');
        $.ajax({
        success: function (result) {
            $('#modal').modal('hide');
        }

    });

}

html

<button id="clickButton" onclick="sendRequest()" class="buttonStyle">click me</button>
<div class="modal fade" id="modal" tabindex="-1" role="dialog">
     <div class="loader"></div>
</div><!-- /.modal -->
Alessandro Messori
  • 1,035
  • 1
  • 14
  • 23
-3

To prevent any users to click anywhere on the page, you need to disable click event handlers. Here's an example of how you could prevent that:

document.oncontextmenu = new Function("return false"); //No right clicks
document.onclick = new Function("return false"); //No left clicks