Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to run the counter when we reach at a specific section in jquery

$(function () {
    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 count($this) {
        var current = parseInt($this.html(), 10);
        if (isScrolledIntoView($this) && !$this.data("isCounting") && current < $this.data('count')) {
            $this.html(++current);
            $this.data("isCounting", true);
            setTimeout(function () {
                $this.data("isCounting", false);
                count($this);
            }, 50);
        }
    }

    $(".c-section4").each(function () {
        $(this).data('count', parseInt($(this).html(), 10));
        $(this).html('0');
        $(this).data("isCounting", false);
    });

    function startCount() {
        $(".c-section4").each(function () {
            count($(this));
        });
    };

    $(window).scroll(function () {
        startCount();
    });

    startCount();
});
.tallDiv 
{
   height: 800px;
   background-color: silver;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

<div class="section4">
  <div class="section4-bg"></div>
  <div class="tallDiv">Scroll down to test</div>
  <div class="counter-section">
    <span class="c-section4">152 </span>
    <h3> کارکنان ما </h3>
  </div>
  <div class="counter-section">
    <span class="c-section4">152 </span>
    <h3> کارکنان ما </h3>
  </div>
  <div class="counter-section">
    <span class="c-section4">152 </span>
    <h3> کارکنان ما </h3>
  </div>
  <div class="tallDiv"></div>
</div>
Comment

PREVIOUS NEXT
Code Example
Javascript :: svelte json 
Javascript :: Remove the warning for setState on unmounted components in React 
Javascript :: como usar for js 
Javascript :: path error 
Javascript :: how to wait for dom in javascript 
Javascript :: gsheet get cell background color 
Javascript :: js how to remove blue while click 
Javascript :: how to add array of object in class in javascript 
Javascript :: remove undefined from object javascript 
Javascript :: node sass version 6.0.0 is incompatible with 4.0.0 
Javascript :: how to make a <li when clicked on a button js 
Javascript :: ubicar escrol en el final jquey 
Javascript :: sending string from jquery ajax to asp.net mvc controller. 
Javascript :: js file not found in laravel on live laravel project 
Javascript :: set @Output through modalref angular 
Javascript :: react hooks port requst 
Javascript :: logo ticker html javascript 
Javascript :: modal in react 
Javascript :: whatsapp images not showing in meta tags 
Javascript :: find component inside tree with enzyme shallow wrapper 
Javascript :: ERROR in ./node_modules/pretty-format/node_modules/ansi-regex/index.js Module build failed: Error: ENOENT: no such file or directory 
Javascript :: radio button not checked when clicked react 
Javascript :: listen to keyboard close event in js 
Javascript :: using for loops to add an event listener 
Javascript :: Could not parse as expression: "1, "desc" DataTable 
Javascript :: reactrouter 
Javascript :: link js filt to html file 
Javascript :: curl --post with api 
Javascript :: es6 syntax 
Javascript :: styling font awesome icons next js 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =