Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 引导计数器在显示器上启动_Javascript_Jquery_Twitter Bootstrap_Css_Twitter Bootstrap 3 - Fatal编程技术网

Javascript 引导计数器在显示器上启动

Javascript 引导计数器在显示器上启动,javascript,jquery,twitter-bootstrap,css,twitter-bootstrap-3,Javascript,Jquery,Twitter Bootstrap,Css,Twitter Bootstrap 3,所以我找到了这个代码笔教程,它帮助我在我的页面上获取数字计数器。它可以完美地工作,但是计数器在页脚中找到,并且计数器实际上在页面加载时启动。因此,当您到达页面底部时,计数器动画已经发生。我如何修改这个javascript,使计数器在达到上述目标div或counter div之前不会启动。例如,假设我 <div id="services"> //Stuff Here </div> <div class="counter-wrap"> <d

所以我找到了这个代码笔教程,它帮助我在我的页面上获取数字计数器。它可以完美地工作,但是计数器在页脚中找到,并且计数器实际上在页面加载时启动。因此,当您到达页面底部时,计数器动画已经发生。我如何修改这个javascript,使计数器在达到上述目标div或counter div之前不会启动。例如,假设我

<div id="services">
    //Stuff Here
</div>

<div class="counter-wrap">
    <div class="container">
        <div class="row">
            <div class="col-sm-4">
                <h2 class="timer count-title count-number" data-to="300" data-speed="1500"></h2>
                <p class="count-text ">SomeText GoesHere</p>
            </div>
            <div class="col-sm-4">
                <h2 class="timer count-title count-number" data-to="800" data-speed="1500"></h2>
                <p class="count-text ">SomeText GoesHere</p>
            </div>
            <div class="col-sm-4">
                <h2 class="timer count-title count-number" data-to="950" data-speed="1500"></h2>
                <p class="count-text ">SomeText GoesHere</p>
            </div>
        </div>
    </div>
</div>

代码笔示例:

您可以使用以下命令检查元素在视口中是否可见:

$.fn.isOnScreen = function(){
    var win = $(window);
    var viewport = {
        top : win.scrollTop(),
        left : win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();

    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();

    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

};
大概是这样的:

我设置了新的
,并添加了一些css,所以它位于页脚中

更新


只运行一次动画:

您可以使用以下命令检查元素在视口中是否可见:

$.fn.isOnScreen = function(){
    var win = $(window);
    var viewport = {
        top : win.scrollTop(),
        left : win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();

    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();

    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

};
大概是这样的:

我设置了新的
,并添加了一些css,所以它位于页脚中

更新


只运行一次动画:

太棒了!现在,当你向上滚动计数器重置时,动画是否可能只发生一次而不重置?是的,你可以做一些查看计数器,检查更新的回答!现在,当你向上滚动计数器重置时,动画是否可能只发生一次而不重置?是的,你可以做一些查看计数器,检查更新的答案
$(document).on( 'scroll', function(){
  $('.timer').each(count).isOnScreen(); 
});