js怎么实现进度条!

js怎么实现进度条!,第1张

<!DOCTYPE "> 

<html xmlns="http://www.w3.org/1999/xhtml" > 

<head> 

<title>进度条</title> 

<style type="text/css"> 

  body{ 

    text-align:center 

  } 

  .graph{ 

    width:450px 

    border:1px solid #F8B3D0 

    height:25px 

  } 

  #bar{ 

        display:block 

        background:#FFE7F4 

        float:left 

        height:100% 

        text-align:center 

    } 

    #barNum{ 

        position:absolute 

    } 

</style> 

<script type="text/javascript"> 

function $(obj){ //封装方法,相当于jQuery

    return document.getElementById(obj) 

function go(){ 

    $("bar").style.width = parseInt($("bar").style.width) + 1 + "%" 

    $("bar").innerHTML = $("bar").style.width 

    if($("bar").style.width == "100%"){ 

        window.clearInterval(bar) //进度为100时清除定时器

    } 

var bar = window.setInterval("go()",50) //设置定时器

window.onload = function(){ 

    bar 

</script> 

</head> 

<body> 

<div class="graph"> 

<strong id="bar" style="width:1%"></strong> 

</div> 

</body> 

</html>

1、引入bootstrap.css和jquery.js

2、点击按钮后获取文件列表,添加到FormData,调用open函数指定类型与URL地址,在发起请求send();

3、监听onreadystatechange事件,当服务器回应后,把传回来的数据转换成JSON字符串,修改img的URL地址,让图片文件显示在页面中;

4、创建xhr对象开启监听文件上传进度,e.lengthComputable是true则计算进度条百分比,把结果给进度条;

5、进度条完成后,修改颜色,移除类添加类

HTML

JS

var jsload = {

img: [ 'about.png', 'applyBtn.png', 'background2.png', 'bgImg.png', 'bird.cman.png', 'borad2.png',

'choujiang.png','entry.png','f_0.png','f_1.png','f_2.png','f_3.png',

'f_4.png','f_5.png','f_6.png','f_7.png','f_8.png','f_9.png',

'fxFBfont.png','ground.2.png','logo.png','pgBar.png','pgBg.png','pipe2.png',

'raffle.png','rank.png','rankBtn.png','ruleBtn.png','shareButton.png',

'shareImg.png','sureBtn.png','tap.png','titleImg.png','trymore.png'

],

count: 1,

go: 1,

init: function () {

var _this = this

$.get('dom.txt', function (response) {

$('#gameDiv').append(response)

_this.move()

})

this.count += this.img.length

this.go = this.count

this.loadImg()

},

loadImg: function () {

for (var i = 0i <this.img.lengthi++) {

var img = new Image()

var _this = this

img.onload = function () {

_this.move()

}

img.src = 'resource/assets/' + this.img[i]

}

return this

},

move: function () {

--this.go

var press = Math.round((this.count - this.go) / this.count * 100)

console.log('游戏加载进度', press)

// if(press === 100){

// start.init()

// }

}

}

jsload.init()

图片 和txt加载 可以参考下


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11281615.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存