Skip to content

Instantly share code, notes, and snippets.

@ScarletPonytail
Last active June 6, 2018 14:31
Show Gist options
  • Save ScarletPonytail/ceffabe9b7278b56f9136ed259dc36ad to your computer and use it in GitHub Desktop.
Save ScarletPonytail/ceffabe9b7278b56f9136ed259dc36ad to your computer and use it in GitHub Desktop.
jQuery - If .class is visible viewport
jQuery( window ).load( function () {
jQuery.fn.isOnScreen = function() {
var win = jQuery( 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 ) );
};
if ( jQuery( '.sc-play-btn' ).isOnScreen() ) {
jQuery( '.sc-play-btn' ).trigger( 'click' );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment