javascript - 滚动时执行动画

标签 javascript jquery html css twitter-bootstrap

我有 Bootstrap 的进度条,这是 html:

  $(".progress-bar").each(function () { // .each(function (i, item) work too!
                var progressBar = $(this); // $(this) would work too
                progressBar.animate({
                    width: progressBar.data('width') + '%'
                }, 1500);
            });
<body>
  
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class='jumbotron' style='height:1400px' width='auto'>
<h1>please scroll down</h1>

</div>


    <div class="alert alert-success">
      <div class="skill-name">ON PROGRESS 357/487</div>
      <div class="progress">
        <div class="progress-bar progress-bar-primary" role="progressbar" data-width="70">
        </div>
      </div>
    </div>

<div class="alert alert-danger">
      <div class="skill-name">FAILED 30/487</div>
      <div class="progress">
        <div class="progress-bar progress-bar-danger" role="progressbar" data-width="30">
        </div>
      </div>
    </div>
  
  </body>

当我加载 HTML 时动画正在运行,因此动画进度条不起作用。 当我向下滚动查看进度条时如何设置动画?

最佳答案

这是一些东西:

Bootstrap :http://www.bootply.com/lRLMiaE7Gy

Jquery :

$(function(){
  $(window).scroll(function(){
    $(".progress-bar:not(.animated)").each(function(){
      if ($(this).is(':visible')) {
                var progressBar = $(this); // $(this) would work too
                progressBar.animate({
                    width: progressBar.data('width') + '%'
                }, 1500);
                progressBar.addClass('animated');
      }
    });
  }); 
}); 

片段

$(function(){
  $(window).scroll(function(){
    $(".progress-bar:not(.animated)").each(function(){
      if ($(this).is(':visible')) {
                var progressBar = $(this); // $(this) would work too
                progressBar.animate({
                    width: progressBar.data('width') + '%'
                }, 1500);
                progressBar.addClass('animated');
      }
    });
  }); 
}); 
<body>
  
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class='jumbotron' style='height:1400px' width='auto'>
<h1>please scroll down</h1>

</div>


    <div class="alert alert-success">
      <div class="skill-name">ON PROGRESS 357/487</div>
      <div class="progress">
        <div class="progress-bar progress-bar-primary" role="progressbar" data-width="70">
        </div>
      </div>
    </div>

<div class="alert alert-danger">
      <div class="skill-name">FAILED 30/487</div>
      <div class="progress">
        <div class="progress-bar progress-bar-danger" role="progressbar" data-width="30">
        </div>
      </div>
    </div>
  
  </body>

关于javascript - 滚动时执行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42089114/

相关文章:

javascript - jsPDF autoTable columnWidth 包装切断表格

javascript - JQuery - 如何在桌面和移动菜单之间传递事件类?

javascript - Bootstrap star-rating inside popover CSS 问题

jQuery 如果 img src 等于则为每个 img 应用类名

javascript - 如何使用 html 表单作为输入运行 python 函数?

java - 在 Java 中使用 JSoup 解析 HTML 中的标签数据

javascript - 如何通过jquery通配符搜索数据属性的精确包含值?

javascript - Phonegap 中的 AJAX 成功数据加载 [object Document]

javascript - 我如何在不使用 JavaScript 的情况下使用 JQuery 库来重写它?

html - 带有 CSS 和 html 的两列不起作用