jQuery progressbar只在第一次加载

时间:2016-04-12 12:25:57

标签: javascript jquery html

当我运行我的代码时,它会使动画不显示在我的进度条上,并且百分比不断上升并且不会停留在给定值。

我不知道我在编写脚本时是做错了什么,也可能是名字。我尝试将所有进度条加载到给定值,作为一个动画,其下方显示进度条的百分比。

这是一个小提琴

https://jsfiddle.net/2s2bmoj2/

回答我的一些代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Result test</title>

    <!-- Animate CSS -->
    <link href="css/animate.css" rel="stylesheet">

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/style.css" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/2.0.5/waypoints.min.js"></script>
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/modernizr.js" type="text/javascript"></script>
    <script src="js/bars.js" type="text/javascript"></script>

    <script>
    </script>

</head>

<body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Result page test</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#about">About</a></li>
                    <li><a href="#contact">Contact</a></li>
                </ul>
            </div>
            <!--/.nav-collapse -->
        </div>
    </nav>

    <section>
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <h1>Result random test</h1>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 js--wp-2">
                    <h2>Photoshop</h2>
                    <h4>Kanalen</h4>
                    <progress class="progressbar" value="21" max="100">
                    </progress>
                    <span class="progress-value">0%</span>
                    <h4>Kanalen</h4>
                    <progress class="progressbar" value="56" max="100">
                    </progress>
                    <span class="progress-value">0%</span>
                    <h4>Kanalen</h4>
                    <progress class="progressbar" value="83" max="100">
                    </progress>
                    <span class="progress-value">0%</span>
                    <h4>Kanalen</h4>
                    <progress class="progressbar" value="15" max="100">
                    </progress>
                    <span class="progress-value">0%</span>
                </div>
            </div>
            <div class="col-md-12">
                <a href="index.html" class="js--wp-1">View open tests</a>
            </div>
        </div>
    </section>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')
    </script>
    <script src="js/bootstrap.js"></script>
    <script src="js/animation.js"></script>
    <script src="js/progressbar.js"></script>
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/jquery.waypoints.min.js"></script>
</body>

</html>

动画

$(document).ready(function () {
    var progressbar = document.getElementsByClassName('progressbar')
        , target = +progressbar.value
        , time = (300 / target) * 5
        , value = 0;

    var loading = function () {
        value += 1;
        progressbar.value = value;

        $('.progress-value').html(value + '%');

        if (value == target) {
            clearInterval(animate);
        }
    };
    var animate = setInterval(function () {
        loading();
    }, time);
});

2 个答案:

答案 0 :(得分:1)

这个jQuery代码应该正常工作。我在你的codepen中创建了它。

$(function() {  
  $.each($('progress'), function(key, item) {
    var targetVal = item.value;
    var currentVal = 0;
    var time =  (300 / targetVal) * 5;
    var progressText = $(item).next('[class^=progress-value]');
    item.value = 0;

    var loading = setInterval(function() {
      item.value = currentVal++;
      progressText.html(item.value + '%');

      if (targetVal == currentVal) {
        clearInterval(loading);
      }
    }, time);
  });
});

答案 1 :(得分:0)

你可以使用var prbar = getElementsByClassName(&#39; progressbar&#39;);