我刚刚找到了解决这个问题的可行方法。我在 Chrome 中进行 Microsoft MVC 2 开发,然后验证我的网页在 IE 7 中正常工作。我使用的是 jQuery 1.4.3 和 blockUI v2.31。默认情况下,blockUI 在 Chrome 中非常流畅,而在 IE 中又短又断断续续。
所以,这是我所做的要点。
我有一个进行 jQuery $.ajax 调用的函数,就在调用之前我阻塞了 UI,然后当调用完成时我解除阻塞 UI。有时此操作会持续几秒钟,并且块 UI 对话框在 Chrome 和 IE 上看起来都很棒,因为动画 GIF 显示了进度。但有时操作执行得非常快,并且块 UI 对话框在 IE7 中显得断断续续和突然(在 Chrome 中仍然看起来很棒)。
解决方案是在隐藏块 UI 对话框的函数中设置稍长的延迟,例如 1.2 秒而不是 200 毫秒。由于无论如何我的大部分操作都需要几秒钟,这几乎不会引起注意,并且使两种浏览器的 UI 都感觉流畅。
例如,在我的客户端代码中调用此函数并更新页面元素:
function ajaxLoadElement(url, elementName, loadData, complete) {
// block the UI
ajaxWaitCursor();
$.ajax({
url: url,
data: loadData,
success: function(data, textStatus, jqXHR) {
// asynchronous call was successful: unblock the UI
ajaxNormalCursor();
$('#' + elementName).html(data);
if (complete != null) {
complete();
}
},
error: function(jqXHR, textStatus) {
// asynchronous call encountered an error: unblock the UI and display the error
ajaxNormalCursor();
displayAjaxLoadError(jqXHR);
}
});
}
这是我用于阻止和解除阻止 UI 的辅助函数:
///
/// Shows a jQuery busy popup with a 20 second timeout
///
function ajaxWaitCursor() {
var imgPath = getImageResource("Content/Images/ajax-loading.gif");
var img = '<img src="' + imgPath + '" style="margin-top: 5px;" />';
$.blockUI(
{
message: img,
fadeIn: 400,
fadeOut: 400,
timeout: 20000,
showOverlay: true,
textAlign: 'center',
centerY: true,
centerX: true,
css: {
border: '',
padding: '5px',
backgroundColor: '#000',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
'border-radius': '10px',
opacity: 0.5,
color: '#fff'
},
overlayCSS: { opacity: 0.1 }
});
}
///
/// Hides the jQuery busy popup; to make this smooth on IE, give it
/// a reasonable amount of time to remain visible, in case the operation
/// was really short.
///
/// In this case, wait for 1.2 seconds before fading out the dialog
///
function ajaxNormalCursor() {
setTimeout("$.unblockUI()", 1200);
}