js滚动的进度条的3种编程方式

2016-02-19 15:18 33 1 收藏

今天图老师小编要向大家分享个js滚动的进度条的3种编程方式教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - Web开发 】

第一种方式,函数式:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"      
html xmlns="http://www.w3.org/1999/xhtml"      
head      
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /      
title无标题文档/title      
/head      
      
body      
div id="allBox" style=" width:500px; margin-top:5px; height:20px; border:#CDCDCD 1px solid;display:inline;overflow:hidden; float:left; position:relative; text-align:center"  
  div id="sonbox" style="height:16px; background-color:#e0f3fa; border:#ff6600 1px solid; position:absolute; top:1px; left:1px;z-index:1; width:400px"/div      
  strong style=" text-align:center;width:500px; POSITION: absolute; left:0; top:0;Z-INDEX: 2;"你的经验是1000,下级是2000/strong      
/div  
script language="javascript"  
var numberwidth=0;   
var allWidth=document.getElementById("allBox").scrollWidth;   
var loadWidth=document.getElementById("sonbox").scrollWidth;   
var doScroll=window.setInterval((function(){   
    numberwidth+=1;   
    if(numberwidth=((loadWidth/allWidth)*100)){   
        window.clearInterval(doScroll);   
        }else{   
        document.getElementById("sonbox").style.width=numberwidth+"%";   
        }   
    }),10);   
/script  
      
/body      
/html  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

第二种方式,数组式:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"      
html xmlns="http://www.w3.org/1999/xhtml"      
head      
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /      
title无标题文档/title      
/head      
      
body  
div id="allBox" style=" width:500px; margin-top:5px; height:20px; border:#CDCDCD 1px solid;display:inline;overflow:hidden; float:left; position:relative; text-align:center"  
  div style="height:16px; background-color:#e0f3fa; border:#ff6600 1px solid; position:absolute; top:1px; left:1px;z-index:1; width:400px"/div      
  strong style=" text-align:center;width:500px; POSITION: absolute; left:0; top:0;Z-INDEX: 2;"你的经验是1000,下级是2000/strong      
/div  
script type="text/javascript"  
var loading = new Object(); //定义一个图片展示的对   
loading = {   
    loadWidth: 0,   
    auto: "",   
    speed: 10,   
    spWidth:0,   
  
    doScroll: function(dd) {   
        loading.spWidth = (document.getElementById(dd).getElementsByTagName("div")[0].scrollWidth / document.getElementById(dd).scrollWidth) * 100;   
        loading.auto = setInterval(function() { loading.dos(dd) }, loading.speed)   
    },   
    dos: function(aa) {   
        //alert(loading.spWidth());   
        loading.loadWidth += 1;   
        //document.getElementById("ggg").innerHTML=document.getElementById(aa).getElementsByTagName("div")[0].scrollWidth+"-------"+document.getElementById(aa).scrollWidth;   
        if (loading.loadWidth = loading.spWidth) {   
            clearInterval(loading.auto);   
        } else {   
            document.getElementById(aa).getElementsByTagName("div")[0].style.width = loading.loadWidth + "%";   
        }   
    }   
};   
loading.doScroll("allBox");   
/script  
/body      
/html

第三种方式,面向对象:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"      
html xmlns="http://www.w3.org/1999/xhtml"      
head      
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /      
title无标题文档/title      
/head      
      
body
div id="ggg"/div  
div id="allBox" style=" width:500px; margin-top:5px; height:20px; border:#CDCDCD 1px solid;display:inline;overflow:hidden; float:left; position:relative; text-align:center"  
  div style="height:16px; background-color:#e0f3fa; border:#ff6600 1px solid; position:absolute; top:1px; left:1px;z-index:1; width:400px"/div      
  strong style=" text-align:center;width:500px; POSITION: absolute; left:0; top:0;Z-INDEX: 2;"你的经验是1000,下级是2000/strong      
/div  
div id="Div1" style=" width:200px; margin-top:5px; height:20px; border:#CDCDCD 1px solid;display:inline;overflow:hidden; float:left; position:relative; text-align:center"  
  div style="height:16px; background-color:#e0f3fa; border:#ff6600 1px solid; position:absolute; top:1px; left:1px;z-index:1; width:100px"/div      
  strong style=" text-align:center;width:200px; POSITION: absolute; left:0; top:0;Z-INDEX: 2;"你的经验是1000,下级是2000/strong      
/div  
script type="text/javascript"  
//进度条的封装类 
function loadingScroll(boxDiv,Speed){   
    this.boxId=boxDiv;//外层的id   
    this.scrollSpeed=Speed;//速度   
}   
  
loadingScroll.prototype.doScroll = function() {   
    var scrollWi = 0;//执行时的宽度   
    var HimalayaN=document.getElementById(this.boxId).getElementsByTagName("div")[0] ;   
    var HimalayaW=document.getElementById(this.boxId);   
    var pointW=(HimalayaN.scrollWidth/HimalayaW.scrollWidth)*100;//原始的宽度比例   
    var doScr=window.setInterval(function() {   
        scrollWi += 1;   
        if (scrollWi = pointW) {   
            window.clearInterval(doScr);   
        } else {   
            HimalayaN.style.width = scrollWi + "%";   
        }   
    }, this.scrollSpeed);   
}   
var ddddd = new loadingScroll("allBox", 10);   
var aaaaa = new loadingScroll("Div1", 10);   
ddddd.doScroll();   
aaaaa.doScroll();   
/script  
      
/body      
/html

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1609328.html

延伸阅读
标签: Web开发
今天有人问我,163邮箱那样的Javascript加载进度条是如何实现的。 我不知道,不过实现一个不难,因为script /有onload和onreadystatechange。还有就是,我们有Atlas。 Atlas中有个类:Sys.ScriptLoader,它的作用就是在页面中依次地加载多个Script文件。在实现之前,先来分析一下这个类的代码。     1Sys.Sc...
带文字的进度条 作者:重庆建设集团一所 唐星彬 下载本文示例源代码 在下载安装的过程中,我们经常会看到带有文字的进度条,它能给人一种直观的概念。在VC中也有进度条的控件,但它不能显示文字。今有一CProgressCtrl类的派生类CTextProgressCtrl就有完成这样的工作。两种控件运行情况的对比如下: ...
《无尽的进度条Progress Quest》部分法术效果攻略 提示1:《无尽的进度条》 为半即时回合制游戏,即那种战斗开始后敌我都有行动条,走到头才能行动的回合制。 瞬移:有一定几率让你无视速度,立刻行动,几率随法术等级上升。 兔子的正义铁拳:召...
标签: Web开发
CSS:  程序代码 style #graphbox{ border:1px solid #e7e7e7; padding:10px; width:250px; background-color:#f8f8f8; margin:5px 0; } #graphbox h2{ color:#666666; font-family:Arial; font-size:18px; font-weight:700; } .graph{ position:relative; background-color:#F0EFEF; border:1px solid #cccccc; padding:2px; font-size:1...
加载外部文件的进度条(看帮助文档整理),只适合那些不愿看帮助文档的新手。我只不过加了一些注释。 为加载图像文件或 SWF 文件创建进度条 创建一个名为 loadImage.fla 的新 Flash 文档。 选择"修改""文档",在宽度文本框中键入 700,在高度文本框中键入 500,从而更改文档的尺寸。 在时间轴中选择第 1 帧,然后在"动作"面板中键入下面的代...

经验教程

996

收藏

50
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部