简体   繁体   English

Jquery - 如何禁用整个页面

[英]Jquery - How to disable the entire page

I have this ajax event 我有这个ajax事件

function save_response_with_ajax(t){
  var form = $('#edit_'+t);
  var div = $('#loading_'+t);
  $.ajax({
    url: form.attr("action"), 
    type: "POST",    
    data: form.serialize(), 
    cache: false,
    beforeSend: function(){
      form.hide();
      div.show();
    },
    complete: function(){
      div.hide();
      form.show();
    },
    success: function (result) {
    }       
  });
}

And everything works fine, but I want to add (if it's possible) the hability of turning the entire page (the content/body) into gray while before/complete ajax events, like if it were a modal (like this http://jqueryui.com/demos/dialog/#modal but without the dialog) 一切正常,但我想添加(如果可能的话)将整个页面(内容/正文)转换为灰色,而不是之前/完成ajax事件,如果它是一个模态(如此http://) jqueryui.com/demos/dialog/#modal但没有对话框)

Is there a way of doing this? 有办法做到这一点吗?

Thanks in advance 提前致谢

Javier Q. 哈维尔Q.

A way of doing this is having an overlay element which fills the entire page. 这样做的一种方法是使用覆盖元素填充整个页面。 If the overlay element has a semi-transparent background color, it grays out the page completely: http://jsfiddle.net/SQdP8/1/ . 如果overlay元素具有半透明背景颜色,则它会完全灰化页面: http//jsfiddle.net/SQdP8/1/

Give it a high z-index so that it's on top of all other elements. 给它一个高z-index ,使它在所有其他元素之上。 That way, it renders correctly, and it catches all events (and won't pass them through). 这样,它正确呈现,并捕获所有事件(并且不会通过它们)。

#overlay {
    background-color: rgba(0, 0, 0, 0.8);
    z-index: 999;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: none;
}​

you can try 你可以试试

 $("body").append('<div id="overlay" style="background-color:grey;position:absolute;top:0;left:0;height:100%;width:100%;z-index:999"></div>');

then just use 然后只是使用

$("#overlay").remove(); $( “#覆盖”)删除()。

to get rid of it. 摆脱它。

quick & dirty. 快速而肮脏。

This is the complete solution which I am using: 这是我正在使用的完整解决方案:

Following are the sections: 以下是部分:

  1. CSS for overlay. 用于叠加的CSS。 "fixed" is used to cover whole page content, not just screen height and widths. “fixed”用于覆盖整个页面内容,而不仅仅是屏幕高度和宽度。 You can use background color or gif 您可以使用背景颜色或gif

  2. Attaches to "beforeSend" event of jQuery Ajax call. 附加到jQuery Ajax调用的“beforeSend”事件。 Creates the overlay on demand and shows it. 根据需要创建叠加并显示它。

  3. Upon completion of request, it removes the overlay from DOM 完成请求后,它将从DOM中删除叠加层

CSS: CSS:

.request-overlay {
    z-index: 9999;
    position: fixed; /*Important to cover the screen in case of scolling content*/
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: block;
    text-align: center;
    background: rgba(200,200,200,0.5) url('../../Images/submit-ajax-loader.gif') no-repeat center; /*.gif file or just div with message etc. however you like*/
}

JavaScript: JavaScript的:

$.ajax({
                url: '/*your url*/',
                beforeSend: function () {
                    $('body').append('<div id="requestOverlay" class="request-overlay"></div>'); /*Create overlay on demand*/
                    $("#requestOverlay").show();/*Show overlay*/
                },
                success: function (data) {
                    /*actions on success*/
                },
                error: function (jqXhr, textStatus, errorThrown) {
                    /*actions on error*/
                complete: function () {
                    $("#requestOverlay").remove();/*Remove overlay*/
                }
            });

Use jQuery ajaxStart() to append a Div to your document. 使用jQuery ajaxStart()将Div附加到文档中。 Set it to the size of your document with some form of semi-transparent document. 使用某种形式的半透明文档将其设置为文档的大小。 Then remove it on ajaxStop() . 然后在ajaxStop()上删除它。

Try appending an overlay during the "beforeSend" function: 尝试在“beforeSend”功能期间添加叠加层:

$("body").prepend("<div class=\"overlay\"></div>");

$(".overlay").css({
    "position": "absolute", 
    "width": $(document).width(), 
    "height": $(document).height(),
    "z-index": 99999, 
}).fadeTo(0, 0.8);
var modal = $('<div>')
  .dialog({ modal: true });

modal.dialog('widget').hide();

setTimeout(function() { modal.dialog('close'); }, 2000); // to close it

here is a demo: http://jsbin.com/avoyut/3/edit#javascript,html,live 这是一个演示: http//jsbin.com/avoyut/3/edit#javascript,html,live

don't forget to call modal.dialog('close'); 不要忘记调用modal.dialog('close'); to end it all! 结束一切!

this way you get the benefits of the actual dialog modal code, resizing, disabling, etc.. 这样你就可以获得实际对话框模态代码,调整大小,禁用等等的好处。

hope this helps -ck 希望这有助于确定

Today I was looking for a solution which would work for all browsers of IE. 今天我一直在寻找适合所有IE浏览器的解决方案。 I took the code of @pimvdb and @Ash Clarke along with his comment where he mentioned background-color: black; opacity: 0.8; may also work. For IE it will just be completely black. 我拿了@pimvdb@Ash Clarke的代码以及他的评论,他提到了background-color: black; opacity: 0.8; may also work. For IE it will just be completely black. background-color: black; opacity: 0.8; may also work. For IE it will just be completely black. and came to a solution below: 并在下面找到了解决方案:

$("#first-div").prepend("<div class=\"overlay-example\"></div>");

var height1 = $("#first-div").height();
var width1 = $("#first-div").width();

$(".overlay-example").css({
    "background-color": "black",
    "z-index": "9999999",
    "position": "absolute",
    "width": width1,
    "height": height1,
    "display": "none"
}).fadeTo(0, 0.0001);

Tested in IE8, IE9 above. 在IE8,IE9上面测试过。 Could not check for IE7. 无法检查IE7。 Will be glad to update my soulution in case you find it wrong. 如果您发现错误,将很高兴更新我的洗礼。 (it would help me also to update my solution :)) (这也有助于我更新我的解决方案:))

Thank you @pimvdb and @Ash Clarke 谢谢@pimvdb@Ash Clarke

Demo 演示

You may want to give the user some indication that something is happening, too, not just a blank/gray screen. 您可能希望向用户提供某些事情正在发生的指示,而不仅仅是空白/灰色屏幕。 I would suggest some sort of loading gif, see this , for example. 我会建议某种加载gif,例如,看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM