JS做的进度条,如何做的?

JavaScript07

JS做的进度条,如何做的?,第1张

js其实是没法计算到网页的加载进度的。

目前见到的打开页面显示进度的有两种

1、如果是flash做的,那是flash自身的加载进度。

2、如果是js做的,做法比较简单,就是在页面的不同的地方插入script标签,动态改变进度的值。     

<html>

    <head>

        <script>

        var processPer = 0

        window.onload = function(){

            document.getElementById('process').innerHTML = processPer

        }

        </script>

    </head>

    <body>

        <div id="process"></div>

        <div></div>

        ....

        <script>

        processPer = 30

        document.getElementById('process').innerHTML = processPer

        </script>

        <div></div>

        ....

        <div></div>

        ....

        <script>

        processPer = 100

        document.getElementById('process').innerHTML = processPer

        </script>

    </body>

</html>

类似这样的,就可以实现了进度的动态改变。

jquery加载页面的方法(页面加载完成就执行),看下windows.onload$(document).ready之间的区别。

1、$(function(){

$("#a").click(function(){

//adding your code here

})

})

2、$(document).ready(function(){

$("#a").click(function(){

//adding your code here

})

})

3、window.onload = function(){

$("#a").click(function(){

//adding your code here

})

}

html代码为<input

type="button" id="a">点击</input>,且页面需要引用jquery的js文件

一般的加载页面时调用js方法如下:

window.onload = function() {

$("table

tr:nth-child(even)").addClass("even")//这个是jquery代码

}

这段代码会在整个页面的document全部加载完成以后执行。不幸的这种方式不仅要求页面的DOM

tree全部加载完成,而且要求所有的外部图片和资源全部加载完成。更不幸的是,如果外部资源,例如图片需要很长时间来加载,那么这个js效果就会让用户感觉失效了。

但是用jquery的方法:

$(document).ready(function() {

//

任何需要执行的js特效

$("table tr:nth-child(even)").addClass("even")

})

就仅仅只需要加载所有的DOM结构,在浏览器把所有的HTML放入DOM tree之前就执行js效果。包括在加载外部图片和资源之前。

还有一种简写的方式:

$(function() {

// 任何需要执行的js特效

$("table

tr:nth-child(even)").addClass("even")

})