今日在学习JavaScript所有写个通用的进度条,防止网页假死。让用户更清楚地知道此网页正在进行加载或者处理一些事情,所有加载进度条是一个网站的必要性。
在网页中Page_load首先要加载此进度条,如果处理完之后再进行关闭
protected void Page_Load(object sender, EventArgs e)
{
initJavascript();
}
//加载进度条的实体方法
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("}}");
//隐藏loading
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>");
////描绘loading样式
HttpContext.Current.Response.Write("<style>");
HttpContext.Current.Response.Write("#loader_container {text-align:center; position:absolute; top:30%; 100%; left: 0;}");
HttpContext.Current.Response.Write("#loader {font-family:Tahoma, Helvetica, sans; font-size:15.5px; color:#000000; background-color:#FFFFFF; padding:10px 0 16px 0; margin:0 auto; display:block; 130px; border:1px solid #5a667b; text-align:left; z-index:2;}");
HttpContext.Current.Response.Write("</style>");
//显示在页面上的loading
HttpContext.Current.Response.Write("<div id=loader_container>");
HttpContext.Current.Response.Write("<div id=loader>");
HttpContext.Current.Response.Write("<div align=center><a><img src="+"loading.gif"+"/></a></div>");
HttpContext.Current.Response.Write("</div></div>");
//缓冲页面上的输出
HttpContext.Current.Response.Flush();
}
//关闭进度条的实体方法实际上是进行DIV图层的隐藏
public static void UnloadJavascript()
{
HttpContext.Current.Response.Write(" <script language=JavaScript type=text/javascript>");
HttpContext.Current.Response.Write("remove_loading();");
HttpContext.Current.Response.Write(" </script>");
}