javascript - Waypoints 计数器向上计数(然后向下计数)并且当 div 处于即时 View 时不执行脚本

标签 javascript jquery jquery-waypoints waypoint

我有一个数字自动收报机。出于演示目的,这是 how the counter is meant to work .它计数然后停止。

现在,我试图让代码脚本在 #ticker 出现时执行。为此,我使用了 waypoints。但是,我遇到了以下问题:

  1. 一旦 #ticker 出现,脚本就不会立即执行。事实上,我必须滚动过去,然后当我向上滚动时,它才开始计数。
  2. 计数器正在向上计数,达到结束数字然后向下计数?我只是希望它计数并停止在结束数字加上字符串(在本例中为 16,000+)。

为什么会这样?

演示:

$(document).ready(function() {

  $('#ticker').waypoint(function() {
    $('.count').each(function() {
      const initial = $(this).text()
      const format = formatter(initial)
      $(this).prop('Counter', 0).animate({
        Counter: format.value
      }, {
        duration: 3000,
        easing: 'swing',
        step: function(now) {
          $(this).text(format.revert(Math.ceil(now)));
        }
      });
    });
  });

})

function formatter(str) {
  // const delimeter = '-'
  const char = 'x'
  const template = str.replace(/\d/g, char)
  const value = str.replace(/\D/g, '')

  function revert(val) {
    // need better solution
    const valStr = val.toString()
    let result = ''
    let index = 0
    for (let i = 0; i < template.length; i++) {
      const holder = template[i]
      if (holder === char) {
        result += valStr.slice(index, index + 1)
        index++
      } else {
        result += holder
      }
    }
    return result
  }
  return {
    template: template,
    value: value,
    revert: revert
  }
}
.gap{
  background: lightgrey;
  height: 600px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js"></script>

<div class="gap"></div>
<div id="ticker">
  <span class="count counter">16,000+</span>
</div>
<div class="gap"></div>

最佳答案

Waypoint 插件有一个选项offset,它决定了应该在哪个位置触发处理程序,默认情况下是0。这意味着您的处理程序只会在您的元素到达浏览器的顶部边缘时触发,并且每次您的元素到达浏览器的顶部边缘时都会触发。

这就是您的情况,您只需将偏移量传递给 Waypoint,它就会被修复。

$(document).ready(function() {
  $('#ticker').waypoint({
    handler: function() {
      $('.count').each(function() {
        const initial = $(this).text()
        const format = formatter(initial)
        $(this).prop('Counter', 0).animate({
          Counter: format.value
        }, {
          duration: 3000,
          easing: 'swing',
          step: function(now) {
            $(this).text(format.revert(Math.ceil(now)));
          }
        });
      });
    },
    offset: '100%'
  });
})

function formatter(str) {
  // const delimeter = '-'
  const char = 'x'
  const template = str.replace(/\d/g, char)
  const value = str.replace(/\D/g, '')

  function revert(val) {
    // need better solution
    const valStr = val.toString()
    let result = ''
    let index = 0
    for (let i = 0; i < template.length; i++) {
      const holder = template[i]
      if (holder === char) {
        result += valStr.slice(index, index + 1)
        index++
      } else {
        result += holder
      }
    }
    return result
  }
  return {
    template: template,
    value: value,
    revert: revert
  }
}
.gap{
  background: lightgrey;
  height: 600px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js"></script>

<div class="gap"></div>
<div id="ticker">
  <span class="count counter">16,000+</span>
</div>
<div class="gap"></div>

关于javascript - Waypoints 计数器向上计数(然后向下计数)并且当 div 处于即时 View 时不执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58265897/

相关文章:

javascript - 如何与 jQuery 建立长期连接以进行推送通知?

javascript - 根据 "Data attributes"和disabled-attribute禁用一组复选框

javascript - Gulp 构建序列损坏了图像文件

javascript - $(this) 不适用于航路点

javascript - AJAX 内容加载后回调运行两次

javascript - 如何更改网页上 "text-selection"光标的外观或在所有主流浏览器中选择文本时禁用光标更改?

jquery - 如何使用 jquery 获取最后一个通用子节点?

javascript - 找到单词并回显其后的 100 个字符

javascript - jQuery ~ 在跟随之前调整 href

javascript - Jquery $(this) 不在函数内工作