Home  >  Article  >  php教程  >  Combining html, css and jquery to achieve simple progress bar effect example code

Combining html, css and jquery to achieve simple progress bar effect example code

高洛峰
高洛峰Original
2016-12-09 11:14:331740browse

No more nonsense, let me just post the code for you. The specific code is as follows:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>jquery实现进度条</title>
<style type="text/css">
body{
  padding:30px;
  margin-left:450px;
  margin-top:200px;
  width:350px;
  border: 1px solid #98AFB7;
}
.progressBar{
  width:280px;
  height:20px;
  border: 1px solid #98AFB7;
  border-radius:8px;
  background:#e1dfdf;
}
input{
  margin-bottom:15px;
}
span{
  position:relative;
  top:-20px;
  left:290px;
}
#bar {
  width: 0px;
  height: 20px;
  border-radius: 7px;
  background: #5EC4EA;
}
</style>
//引入Jquery文件
<script src="Jquerys/jquery.js"></script>
<script type="text/javascript">
function progressBar() {
  $("#bar").css("width", "0px");
  var speed =20;//进度条的速度
  bar = setInterval(function () {
  nowWidth = parseInt($("#bar").width());
  if (nowWidth <= 279) {
    var barWidth = (nowWidth + 1);
    $("#bar").css("width", barWidth + "px");
    var totla = parseInt($(".progressBar").width())
    var ss = barWidth / totla * 100;
    $("#span_s").text(ss);
    var index = $("#span_s").text().indexOf(".");
    if (index != -1) {
      var context = $("#span_s").text().substring(0, index);
      $("#span_s").text(context);
    }
    else {
      $("#span_s").text(ss);
      if (parseInt($("#span_s").text()) == 100) {
      alert(&#39;完成&#39;);
      }
    }
  } else {
      clearInterval(bar);
    }
  }, speed);
}
</script>
</head>
<body>
  <input type="button" value="开始" onclick="progressBar()" />
  <div class="progressBar"><div id="bar"></div></div><span id="span_s">0</span><span>%</span>
</body>
</html>

This progress bar is very simple. First of all, in the html, it is a div nested inside a div, and then write what you want Just use the style you want, and the implementation of special effects is also very simple. It is also very simple to write an anonymous function in a timer. Here I execute an anonymous function in 20 milliseconds. The code inside is to add one pixel at a time. Of course, you can also You can use percentages to increase pixels.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn