How can I create a “Please Wait, Loading…” animation using jQuery?

后端 未结 17 2033
长情又很酷
长情又很酷 2020-11-22 00:07

I would like to place a \"please wait, loading\" spinning circle animation on my site. How should I accomplish this using jQuery?

17条回答
  •  粉色の甜心
    2020-11-22 00:39

    jQuery provides event hooks for when AJAX requests start and end. You can hook into these to show your loader.

    For example, create the following div:

    Loading

    Set it to display: none in your stylesheets. You can style it whatever way you want to. You can generate a nice loading image at Ajaxload.info, if you want to.

    Then, you can use something like the following to make it be shown automatically when sending Ajax requests:

    $(document).ready(function () {
    
        $('#spinner').bind("ajaxSend", function() {
            $(this).show();
        }).bind("ajaxComplete", function() {
            $(this).hide();
        });
    
    });
    

    Simply add this Javascript block to the end of your page before closing your body tag or wherever you see fit.

    Now, whenever you send Ajax requests, the #spinner div will be shown. When the request is complete, it'll be hidden again.

提交回复
热议问题