Joined February 2012
·

Jeroen Ransijn

Creator at Aan Zee
·
Sassenheim
·
·
·

This uses a callback (http://jsfiddle.net/LAJNz/1/):

$.fn.isOnScreen = function (cbTrue, cbFalse) {

    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();

    var isOnScreen = (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

    return isOnScreen
      ? cbTrue && cbTrue(viewport, bounds)
      : cbFalse && cbFalse(viewport, bounds);
};

SECOND EDIT IS NOT REFLECTED IN JSFIDDLE!

This looks good, can you put this up on gist? I really dislike how code looks on coderwall — the one thing that really bugs me about this service.

Like your gist! Will click through them for sure. Thanks for sharing.

Posted to Backbone usage sample over 1 year ago

If only I had read this before...

Achievements
56 Karma
3,202 Total ProTip Views
Interests & Skills