Asp.net”頁面載入中”效果實現

來源:互聯網
上載者:User
 

在網上找了好多,實現方式大多是使用定時器

個人覺得定時器會造成一定程度上的假象

(如:定時器還沒結束頁面已經載入完畢,或定時器結束後頁面還沒載入完成)

本方法利用的是javascript,先網頁面裡面寫入一個層,顯示載入框,然後等頁面結束後

在body的onload中寫入事件,隱藏該對話方塊

方法(我的系統是多個頁面,所以寫了一個公用的類common.cs):

在類裡面加入如下函數(也可在每個代碼後檔案中寫入):

#region "頁面載入中效果"
/// <summary>
/// 頁面載入中效果
/// </summary>
public static void initJavascript()
{
HttpContext.Current.Response.Write(" <script language=JavaScript type=text/javascript>");
HttpContext.Current.Response.Write("var t_id = setInterval(animate,20);");
HttpContext.Current.Response.Write("var pos=0;var dir=2;var len=0;");
HttpContext.Current.Response.Write("function animate(){");
HttpContext.Current.Response.Write("var elem = document.getElementById('progress');");
HttpContext.Current.Response.Write("if(elem != null) {");
HttpContext.Current.Response.Write("if (pos==0) len += dir;");
HttpContext.Current.Response.Write("if (len>32 || pos>79) pos += dir;");
HttpContext.Current.Response.Write("if (pos>79) len -= dir;");
HttpContext.Current.Response.Write(" if (pos>79 && len==0) pos=0;");
HttpContext.Current.Response.Write("elem.style.left = pos;");
HttpContext.Current.Response.Write("elem.style.width = len;");
HttpContext.Current.Response.Write("}}");
HttpContext.Current.Response.Write("function remove_loading() {");
HttpContext.Current.Response.Write(" this.clearInterval(t_id);");
HttpContext.Current.Response.Write("var targelem = document.getElementById('loader_container');");
HttpContext.Current.Response.Write("targelem.style.display='none';");
HttpContext.Current.Response.Write("targelem.style.visibility='hidden';");
HttpContext.Current.Response.Write("}");
HttpContext.Current.Response.Write("</script>");
HttpContext.Current.Response.Write("<style>");
HttpContext.Current.Response.Write("#loader_container {text-align:center; position:absolute; top:40%; width:100%; left: 0;}");
HttpContext.Current.Response.Write("#loader {font-family:Tahoma,
Helvetica, sans; font-size:11.5px; color:#000000;
background-color:#FFFFFF; padding:10px 0 16px 0; margin:0 auto;
display:block; width:130px; border:1px solid #5a667b; text-align:left;
z-index:2;}");
HttpContext.Current.Response.Write("#progress {height:5px;
font-size:1px; width:1px; position:relative; top:1px; left:0px;
background-color:#8894a8;}");
HttpContext.Current.Response.Write("#loader_bg
{background-color:#e4e7eb; position:relative; top:8px; left:8px;
height:7px; width:113px; font-size:1px;}");
HttpContext.Current.Response.Write("</style>");
HttpContext.Current.Response.Write("<div id=loader_container>");
HttpContext.Current.Response.Write("<div id=loader>");
HttpContext.Current.Response.Write("<div align=center>頁面正在載入中 ...</div>");
HttpContext.Current.Response.Write("<div id=loader_bg><div id=progress> </div></div>");
HttpContext.Current.Response.Write("</div></div>");
HttpContext.Current.Response.Flush();
}
#endregion
}

之後在需要調用的頁面中在Page_Load()時間調用該方法:

Common.initJavascript();

並在每個頁面body載入完畢後調用隱藏該div的事件

<body onload="remove_loading();">

完畢!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.