Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

events - JQuery: fire action when element is in view

In the footer of my site I'm using counUp.js (Link: http://inorganik.github.io/countUp.js/ ) to count up three numbers. I added this code at the bottom of the site:

   <script type="text/javascript">
          var c1 = new countUp("upnum1", 1, 27, 0, 4);
          var c2 = new countUp("upnum2", 1, 10, 0, 4);
          var c3 = new countUp("upnum3", 1, 27, 0, 4);
          var c4 = new countUp("upnum4", 1, 1000, 0, 4);
          window.onload = function() {
            c1.start();
            c2.start();
            c3.start();
            c4.start();
          }
    </script>

This works well, but starts counting once the page is loaded of course. How do I manage to fire this effect when the containing div of the numbers is 'in view' and not when the page is loaded? Tried several jQuery things but can't find a working solution... Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a simple check to see whether or not an element is in the viewport.

You can choose either to use a plugin or pure JQuery.

Relevant HTML :

<div id="inViewport">Is this in the viewport?</div>

Pure JQuery :

Here is the fiddle for this : http://jsfiddle.net/zWtkc/1/

$.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));

};

$(document).ready(function(){
    $(window).scroll(function(){
        if ($('#inViewport').isOnScreen()) {
            // The element is visible, do something
            alert("in viewport!");
        } else {
            // The element is NOT visible, do something else
        }
    });
});

Plugins :

You can use this plugin : https://github.com/teamdf/jquery-visible/

$(document).ready(function(){
    if ($('#inViewport').visible(true)) {
        // The element is visible, do something
    } else {
        // The element is NOT visible, do something else
    }
});

Or you can use this plugin : http://www.appelsiini.net/projects/viewport which allows for viewport selectors and your code will look like this :

$('#inViewport:in-viewport').doSomething();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...