STYLE SWITCHER

jQuery – test if element is in viewport (visible on screen)

Untitled-1

[js]

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

};

[/js]

[js]
$(‘.box’).click(function(){
alert($(‘.orange’).isOnScreen());
});
[/js]

Recent Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.