返回列表 发帖

javascript动态进度条(附实例)

如果你的一段程序需要运行很长时间才能完成,在这段时间内又不想让你的客户等得不耐烦。那么 Progress_bar 可以帮你做到。

Process_bar 利用javascript动态生成一个进度条,并且它会根据你的任务进度来实时改变进度条的进度。
  1. <?php
  2. /**
  3. * @copyright 互联网
  4. * @author Chris Geisler
  5. * @整理 www.phppx.com
  6. * @desc
  7. *    如果你的一段程序需要很长时间才能完成,在这段时间内又不想让你的客户等得不耐烦。那么 Progress_bar 可以帮你做到。
  8. *    Process_bar 利用java script动态生成一个进度条,并且会根据你的任务进度来实时改变进度条的进度。
  9. */
  10. /*
  11. *    Written by Chris Geisler - dean@apartmentlocators.com
  12. *
  13. *    This script is distributed under the GPL License
  14. *
  15. *    This program is distributed in the hope that it will be useful,
  16. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. *    GNU General Public License for more details.
  19. *
  20. *    http://www.gnu.org/licenses/gpl.txt
  21. *
  22. */

  23. class progress_bar
  24. {
  25.         var $name;                // The name of the html element representing the progress bar.
  26.         var $percent;   // The current percent value of the progress bar.
  27.         var $width;     // The maximum width of the progress bar.

  28.         /*Function progress_bar - The progress bar constructor function
  29.         parameters:
  30.         $name the name of the html element representing the progress bar.
  31.         $percent the initial percent value of the progress bar.
  32.         $width the initial width of the progress bar.
  33.         $auto_create if set to TRUE the create() function will be called upon construction of the progress bar
  34.         */
  35.         function progress_bar($name = 'pbar',$percent = 1,$width = 100,$auto_create = TRUE)
  36.         {
  37.                 $this->name = $name;

  38.                 $this->percent = $percent;

  39.                 $this->width = $width;
  40.                 if($auto_create)
  41.                 {
  42.                         $this->create();
  43.                 }
  44.           }

  45.         /*Function create() - Dispalys the progress bar as an html
  46.         element. (Warning: do not call this function twice or if $auto_create is set to TRUE)

  47.         parameters:
  48.         $name sets the name of the html element
  49.         (This function becomes usless after the create(); function is called.)*/
  50.         function create()
  51.         {
  52.                 ?>
  53.                 <div align="center">
  54.                   <center>
  55.                   <table height="20" name="<? echo('table_' . $this->name);?>" border="0" cellpadding="0" cellspacing="0" width="<? echo($this->width + 60);?>">
  56.                         <tr>
  57.               <td width="52" height="20"><p>Progress: </p></td>
  58.                       <td width="4" height="20" valign="top" align="left"><img border="0" src="http://www.realestate-in-fortworth.com/admin/images/begin-filled.gif" width="4" height="20"></td>
  59.                       <td name="<? echo('cell_' . $this->name);?>" align="left" valign="middle" width="<? echo($this->width);?>" height="20" style="background-repeat: repeat-x" background="http://www.realestate-in-fortworth.com/admin/images/fill_bg.gif"><img name="<? echo($this->name)?>" border="0" src="http://www.realestate-in-fortworth.com/admin/images/fill.gif" width="<? echo(($percent * .01) * $width);?>" height="11"></td>
  60.                       <td width="4"  height="20" valign="top" align="left"><img border="0" src="http://www.realestate-in-fortworth.com/admin/images/end-filled.gif" width="4" height="20"></td>
  61.                     </tr>
  62.                   </table>
  63.                   </center>
  64.                 </div>
  65.                 <?
  66.         }

  67.         /*Function set_name() - Sets the $percent of the object based
  68.         on current tasks done and the total tasks to finish.

  69.         parameters:
  70.         $name sets the name of the html element
  71.         (This function becomes usless after the create(); function is called.)*/
  72.         function set_name($name)
  73.         {
  74.                 $this->name = $name;
  75.         }

  76.         /*Function set_percent() - Sets the $percent of the progress bar
  77.         using a pre-calculated percent.

  78.         parameters:
  79.         $percent the pre-calculated percent*/
  80.         function set_percent($percent)
  81.         {
  82.                 $this->percent = $percent;

  83.                 echo('<script>document.images.' . $this->name . '.width = ' . ($this->percent / 100) * $this->width . '</script>');
  84.         }

  85.         /*Function set_percent_adv() - Sets the $percent of the object based
  86.         on current tasks done and the total tasks to finish.

  87.         parameters:
  88.         $cur_amount the curent number of tasks completed in a script
  89.         $max_amount the number of tasks to complete in a script*/
  90.         function set_percent_adv($cur_amount,$max_amount)
  91.         {
  92.                 $this->percent = ($cur_amount / $max_amount) * 100;
  93.                 echo('<script>document.images.' . $this->name . '.width = ' . ($this->percent / 100) * $this->width . '</script>');
  94.         }

  95.         /*Function set_width() - Sets the maximum $width of the progress bar.

  96.         parameters:
  97.         $cur_amount the curent number of tasks completed in a script
  98.         $max_amount the number of tasks to complete in a script*/
  99.         function set_width($width)
  100.         {
  101.                 $this->width = $width;
  102.         }
  103. }

  104. ?>
复制代码
例子:
  1. <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
  2. <html>
  3. <head>
  4. <title>Progress Bar Example</title>
  5. </head>
  6. <body>
  7. <?php
  8. set_time_limit(0);
  9. include("class.progress_bar.php");

  10. $pbar = new progress_bar('pbar',1,200,FALSE); //Creates a 200 pixle width progress bar starting at 1 percent with the name pbar auto create = false

  11. $num_tasks = 20000; // the number of tasks to be completed.
  12. $pbar->create();
  13. for($cur_task = 0; $cur_task <= $num_tasks; $cur_task++)
  14. {
  15.     echo("<p>Task $cur_task complete.</p>"); // Execute the current task.
  16.         $pbar->set_percent_adv($cur_task,$num_tasks); // tells the progress bar that $cur_task of the 500 tasks is completed.
  17. }
  18. ?>
  19. </body>
  20. </html>
复制代码

TOP

返回列表