草鞋

Blog grass shoes
posts - 1, comments - 4, trackbacks - 0, articles - 27
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

JS进度条一

Posted on 2007-10-23 09:48 草 鞋 阅读(274) 评论(0)  编辑  收藏 所属分类: JavaScript

<script>
if(window.ActiveXObject){
 document.execCommand("BackgroundImageCache",false,true);
}

function ProcessBar(){
   this.width = 256;
   this.height = 18;
   this.top = 0;
   this.left = 0;
   this.backImg = "process_back.gif";
   this.foreImg = "process.gif";
   this.backDiv = document.createElement("div");
   this.foreDiv = document.createElement("div");
   this.fontDiv = document.createElement("div");

   this.isMoving = false;
   this.nowLength = 0;
   this.moveInterval = 100;
   this.moveRange = 1;
   this.timer;
   this.isHide = true; 

   ProcessBar.nowObj = this;

   this.init = function(){
        this.foreDiv.style.backgroundImage = "url(" + this.foreImg + ")";
  this.foreDiv.style.backgroundRepeat = "no-repeat";
  this.foreDiv.style.position = "absolute";
  this.foreDiv.style.width = this.nowLength;
  this.foreDiv.style.height = this.height;
  this.foreDiv.style.top = 0;
  this.foreDiv.style.left = 0;
  
  this.fontDiv.style.background = "transparent";
  this.fontDiv.style.position = "absolute";
  this.fontDiv.style.width = this.width;
  this.fontDiv.style.height = this.height;
  this.fontDiv.style.top = 2;
  this.fontDiv.style.left = 0;
  this.fontDiv.style.textAlign = "center";
  this.fontDiv.style.fontSize = "13px";
  this.fontDiv.appendChild(document.createTextNode(" "));

  this.backDiv.style.backgroundImage = "url(" + this.backImg + ")";
  this.backDiv.style.backgroundRepeat = "no-repeat";
  this.backDiv.style.position = "absolute";
  this.backDiv.style.width = this.width;
  this.backDiv.style.height = this.height;
  this.backDiv.style.top = this.top;
  this.backDiv.style.left = this.left;

  this.backDiv.appendChild(this.foreDiv);
  this.backDiv.appendChild(this.fontDiv);

  document.body.appendChild(this.backDiv);

  this.backDiv.style.visibility = "";
  this.isHide = false;
   }

   this.changeMode = function(){
  this.isMoving = !this.isMoving;
  
  if(this.isMoving){
   this.timer = window.setInterval("ProcessBar.nowObj.moving()", this.moveInterval);
  }else{
   window.clearInterval(this.timer);
  }
   }

   this.moving = function(range){
  range = range || ProcessBar.nowObj.moveRange;
        ProcessBar.nowObj.nowLength += range;
  ProcessBar.nowObj.foreDiv.style.width = ProcessBar.nowObj.nowLength;

  ProcessBar.nowObj.fontDiv.firstChild.data = Math.ceil((ProcessBar.nowObj.nowLength/ProcessBar.nowObj.width)*100) + "%";

  if(ProcessBar.nowObj.nowLength >= ProcessBar.nowObj.width){
   window.clearInterval(ProcessBar.nowObj.timer);
   ProcessBar.nowObj.fontDiv.firstChild.data = "Complete!";
  }
   }

   this.moveTo = function(pts){
  var isPercent = false;
  var val = pts;
  if(!pts) return;

  if(isNaN(pts)){
   pts = Math.round(parseInt(pts.replace(/\%/,"")) * ProcessBar.nowObj.width/100);
   isPercent = true;
  }
  
  if(pts < 0 || pts > ProcessBar.nowObj.width) return;

  ProcessBar.nowObj.nowLength = pts;
  ProcessBar.nowObj.foreDiv.style.width = ProcessBar.nowObj.nowLength;
  ProcessBar.nowObj.fontDiv.firstChild.data = isPercent?val:Math.ceil((ProcessBar.nowObj.nowLength/ProcessBar.nowObj.width)*100) + "%";
   }

   this.hide = function(){
  this.isHide = true;
  this.backDiv.style.visibility = "hidden";
   }

   this.show = function(){
  this.isHide = false;
  this.backDiv.style.visibility = "";
   }
}
</script>
<body>
<input type="button" value="start" onclick="processBar.changeMode();if(processBar.isMoving){this.value='Stop';}else{this.value='Start';}">
<input type="button"  value="加速" onclick="processBar.moveRange++;">
<input type="button"  value="显示" onclick="if(processBar.isHide) processBar.show();else processBar.hide();">
<input type="button"  value="Go" onclick="processBar.moveTo(sel.value==2?val.value:val.value+'%');"><input id="val" size="2"><select id="sel"><option  value="1">%</option><option value="2">px</option></select>
</body>
<script>
var processBar = new ProcessBar();
processBar.width=327
processBar.backImg = "http://screenprint2007.cpp114.com/UserFiles/20070414161906093.jpg";
processBar.foreImg = "http://screenprint2007.cpp114.com/UserFiles/20070414161919171.jpg";
processBar.top = 100;
processBar.left = 20;
processBar.init();

</script>