zoukankan      html  css  js  c++  java
  • [转]页面加载时,显示页面加载的进度

    /// <summary>
    /// 根据别人提供的JS,于是我就写成了通用的方法,并且可以根据自己的
    /// 需求随意改变进度条的长度大小信息与颜色和样式,如果其他人还有更
    /// 好的方法展示,希望能告诉我,一起讨论。
    /// 联系QQ:260511433
    /// 邮箱:luoye0732@163.com
    /// --------------------------------------------------------------
    /// 页面加载进度条 应用程序   
    /// --------------------------------------------------------------
    /// Description: 页面加载进度条 应用程序
    /// --------------------------------------------------------------
    /// Author:tanyi 2008-6-3
    /// Modify:modify 2008-6-3
    /// --------------------------------------------------------------
    /// </summary>

    public class PagePlan
    {
        private string _bordercolor = "#5a667b";
        private string _plancolor = "#8894a8";
        private string _backgroundcolor = "#ffffff";
        private string _fontcolor = "#000000";
        private string _planbgcolor = "#e4e7eb";
        private int _planHeight = 5;

        /// <summary>
        /// 初始化属性
        /// </summary>
        public PagePlan()
        {
            this._bordercolor = "#5a667b";
            this._plancolor = "#8894a8";
            this._backgroundcolor = "#ffffff";
            this._fontcolor = "#000000";
            this._planbgcolor = "#e4e7eb";
            this._planHeight = 5;
        }

        /// <summary>
        /// 边框颜色
        /// </summary>
        public string BorderColor
        {
            get { return _bordercolor; }
            set { _bordercolor = value; }
        }

        /// <summary>
        /// 进度条颜色
        /// </summary>
        public string PlanColor
        {
            get { return _plancolor; }
            set { _plancolor = value; }
        }

        /// <summary>
        /// 背景颜色
        /// </summary>
        public string BackGroundColor
        {
            get { return _backgroundcolor; }
            set { _backgroundcolor = value; }
        }

        /// <summary>
        /// 字体颜色
        /// </summary>
        public string FontColor
        {
            get { return _fontcolor; }
            set { _fontcolor = value; }
        }

        /// <summary>
        /// 进度条背景颜色
        /// </summary>
        public string Planbgcolor
        {
            get { return _planbgcolor; }
            set { _planbgcolor = value; }
        }

        /// <summary>
        /// 进度条的高度
        /// </summary>
        public int PlanHeight
        {
            get { return _planHeight; }
            set { _planHeight = value; }
        }

        /// <summary>
        /// 进度加载
        /// </summary>
        public void LoadPlan()
        {
            LoadPlan(string.Empty, 0);
        }

        /// <summary>
        /// 进度加载
        /// </summary>
        /// <param name="planInfo">进度信息</param>
        public void LoadPlan(string planInfo)
        {
            LoadPlan(planInfo, 0);
        }

        /// <summary>
        /// 进度加载
        /// 测试进度效果如下:
        /// (1)在Page_Load中引用using System.Threading;
        /// (2)在页面刚加载时调用PageLoadPlan();
        /// (3)中间调用Thread.Sleep(10000);
        /// (4)最后页面加载完调用PageUnloadPlan();
        /// </summary>
        /// <param name="planInfo">进度信息</param>
        /// <param name="with">长度</param>
        public void LoadPlan(string planInfo, int with)
        {
            int loaderMinWith = with < 130 ? 130 : with;
            int loader_bgMinWith = loaderMinWith - 130 + 113;
            int posValue = loader_bgMinWith - 35;
            int planHeight = PlanHeight;
            int planOutsideHeigth = planHeight + 2;

            if (planInfo.Trim().Length <= 0) planInfo = "页面正在加载中 ...";

            System.Text.StringBuilder htmlPlanContent = new System.Text.StringBuilder();
            htmlPlanContent.Append("<script language='javaScript' type='text/javascript'>");
            htmlPlanContent.Append("var t_id = setInterval(animate,20);");
            htmlPlanContent.Append("var pos = 0;var dir = 2;var len = 0;");
            htmlPlanContent.Append("function animate(){");
            htmlPlanContent.Append("var elem = document.getElementById('progress');");
            htmlPlanContent.Append("if(elem != null) {");
            htmlPlanContent.Append("if (pos==0) len += dir;");
            htmlPlanContent.Append("if (len>32 || pos>");
            htmlPlanContent.Append(posValue);
            htmlPlanContent.Append(") pos += dir;");
            htmlPlanContent.Append("if (pos>");
            htmlPlanContent.Append(posValue);
            htmlPlanContent.Append(") len -= dir;");
            htmlPlanContent.Append("if (pos>");
            htmlPlanContent.Append(posValue);
            htmlPlanContent.Append(" && len==0) pos=0;");
            htmlPlanContent.Append("elem.style.left = pos;");
            htmlPlanContent.Append("elem.style.width = len;");
            htmlPlanContent.Append("}}");
            htmlPlanContent.Append("function remove_loading(){");
            htmlPlanContent.Append("this.clearInterval(t_id);");
            htmlPlanContent.Append("var targelem = document.getElementById('loader_container');");
            htmlPlanContent.Append("targelem.style.display = 'none';");
            htmlPlanContent.Append("targelem.style.visibility = 'hidden';");
            htmlPlanContent.Append("}");
            htmlPlanContent.Append("</script>");
            htmlPlanContent.Append("<style>");
            htmlPlanContent.Append("#loader_container {text-align:center; position:absolute; top:40%; 100%; left: 0;}");
            htmlPlanContent.Append("#loader {font-family:Tahoma, Helvetica, sans; font-size:11.5px; color:");
            htmlPlanContent.Append(FontColor);
            htmlPlanContent.Append("; background-color:");
            htmlPlanContent.Append(BackGroundColor);
            htmlPlanContent.Append("; padding:10px 0 16px 0; margin:0 auto; display:block; ");
            htmlPlanContent.Append(loaderMinWith);
            htmlPlanContent.Append("px; border:1px solid ");
            htmlPlanContent.Append(BorderColor);
            htmlPlanContent.Append("; text-align:left; z-index:2;}");
            htmlPlanContent.Append("#progress {height:");
            htmlPlanContent.Append(planHeight);
            htmlPlanContent.Append("px; font-size:1px; 1px; position:relative; top:1px; left:0px; background-color:");
            htmlPlanContent.Append(PlanColor);
            htmlPlanContent.Append(";}");
            htmlPlanContent.Append("#loader_bg {background-color:");
            htmlPlanContent.Append(Planbgcolor);
            htmlPlanContent.Append("; position:relative; top:8px; left:8px; height:");
            htmlPlanContent.Append(planOutsideHeigth);
            htmlPlanContent.Append("px; ");
            htmlPlanContent.Append(loader_bgMinWith);
            htmlPlanContent.Append("px; font-size:1px;}");
            htmlPlanContent.Append("</style>");
            htmlPlanContent.Append("<div id='loader_container'>");
            htmlPlanContent.Append("<div id='loader'>");
            htmlPlanContent.Append("<div align='center'>");
            htmlPlanContent.Append(planInfo);
            htmlPlanContent.Append("</div>");
            htmlPlanContent.Append("<div id='loader_bg'><div id='progress'> </div></div>");
            htmlPlanContent.Append("</div></div>");

            System.Web.HttpContext.Current.Response.Write(htmlPlanContent.ToString());
            System.Web.HttpContext.Current.Response.Flush();
        }

        /// <summary>
        /// 进度卸载
        /// </summary>
        public void UnloadPlan()
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript' type='text/javascript'>");
            System.Web.HttpContext.Current.Response.Write("remove_loading();");
            System.Web.HttpContext.Current.Response.Write("</script>");
        }
    }

    调用方式:
    protected void Page_Load(object sender, EventArgs e)
    {

        PagePlan plan = new PagePlan();
        plan.LoadPlan();//有多个方法重载,自己可以按需求调用
        //其他页面代码 ...
        plan.UnloadPlan();
        //去掉plan.UnloadPlan();调用方法,或者在ASPX的HTML页面的最下面加入如下代码:
        //<script language='javaScript' type='text/javascript'>remove_loading();</script>
    }

    该文章经过本人整理所得,欢迎转载,转载时请加上本文地址;本文基于署名 2.5 中国大陆许可协议发布,请勿演绎或用于商业目的,但是必须保留本文的署名张志涛(包含链接如您有任何疑问或者授权方面的协商,请给我留言
  • 相关阅读:
    从前端回到了我的本专业网络
    相对定位与绝对定位的理解
    table( 表格)以及列表的使用
    使用editplus编写HTML页面为什么设置了UTF-8仍然中文乱码
    复习--3--对于第三堂课的总结--将两个页面相互用超链接链接到一起
    前端学习笔录--2--HTML篇--有点麻烦的加载图片
    前端学习笔录--1--HTML篇
    sublime text 有毒--无法使用快捷键利用浏览器打开HTML文件
    sublime text 插件
    sublime写网页代码,里面的中文字符会出现乱码
  • 原文地址:https://www.cnblogs.com/zhangzt/p/1676467.html
Copyright © 2011-2022 走看看