【问题标题】:Make numbers animate when user scrolls and reach a div当用户滚动并到达 div 时使数字动画
【发布时间】:2018-03-13 15:33:07
【问题描述】:

我有一个可以完美运行的简单 jQuery 代码(我想保留)。问题是该动画位于页面下方的部分中,并且在页面加载时开始运行。我需要这个动画(数字从 0 开始并一直运行到我放入 div 的数字)仅在用户滚动并到达该 div 时发生。我在 Google 和 StackOverflow 上进行了搜索,但我发现的解决方案不起作用或需要插件(我不想使用)。

这是我目前拥有的演示代码: https://jsfiddle.net/aj7Lk2bw/2/

jQuery 是:

    $('.value').each(function () {
    $(this).prop('Counter',0).animate({
        Counter: $(this).text()
    }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            $(this).text(Math.ceil(now));
        }
    });
});

【问题讨论】:

标签: javascript jquery html jquery-animate counter


【解决方案1】:

看看这是不是你想要的:https://jsfiddle.net/aj7Lk2bw/19/

$(window).scroll(testScroll);
var viewed = false;

function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

function testScroll() {
  if (isScrolledIntoView($(".numbers")) && !viewed) {
      viewed = true;
      $('.value').each(function () {
      $(this).prop('Counter',0).animate({
          Counter: $(this).text()
      }, {
          duration: 4000,
          easing: 'swing',
          step: function (now) {
              $(this).text(Math.ceil(now));
          }
      });
    });
  }
}

在这里找到了方法:Run script when div is visible in browser window

【讨论】:

  • 嗨!这几乎是完美的。唯一的问题是,数字加起来后,他们开始倒数到1,呵呵。我需要数字从 0 开始计数,直到所需的值并停在这个值。
  • 嗨泽克劳迪奥!您的解决方案完美运行,非常感谢您,很抱歉延迟回复您。
【解决方案2】:

这是一个可能的解决方案:

var section = document.querySelector('.numbers');
var hasEntered = false;

window.addEventListener('scroll', (e) => {
    var shouldAnimate = (window.scrollY + window.innerHeight) >= section.offsetTop;

    if (shouldAnimate && !hasEntered) {
    hasEntered = true;

    $('.value').each(function () {
        $(this).prop('Counter',0).animate({
        Counter: $(this).text()
        }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            $(this).text(Math.ceil(now));
        }
        });
    });

  }
});

变量section 是带有数字的蓝色部分,hasEntered 是不重复动画。

如果窗口滚动高于部分位置,则它会动画!

小提琴:https://jsfiddle.net/aj7Lk2bw/18/

【讨论】:

    【解决方案3】:

    这里我可以分享另一个例子。

    你可以试试那个 HTML:

    $(window).scroll(testScroll);
    var viewed = false;
    
    function isScrolledIntoView(elem) {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();
    
        var elemTop = $(elem).offset().top;
        var elemBottom = elemTop + $(elem).height();
    
        return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    }
    
    function testScroll() {
      if (isScrolledIntoView($(".number")) && !viewed) {
          viewed = true;
          $('.value').each(function () {
          $(this).prop('Counter',0).animate({
              Counter: $(this).text()
          }, {
              duration: 4000,
              easing: 'swing',
              step: function (now) {
                  $(this).text(Math.ceil(now));
              }
          });
        });
      }
    }
    <div class="number">
                        
                                 <div class="value-wrap">
                        <div class="value" akhi="">51</div>
                        <h4>YEARS IN BUSINESS</h4>
                    </div>
                   
                                 <div class="value-wrap">
                        <div class="value" akhi="">29</div>
                        <h4>WORLDWIDE CERTIFICATION</h4>
                    </div>
                   
                                 <div class="value-wrap">
                        <div class="value" akhi="">58</div>
                        <h4>COUNTRIES REPRESENTED</h4>
                    </div>
                   
                                 <div class="value-wrap">
                        <div class="value" akhi="">6</div>
                        <h4>CONTINENT REPRESENTED</h4>
                    </div>
      </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 2012-05-13
      • 2021-08-27
      相关资源
      最近更新 更多