Last active
June 6, 2018 14:31
-
-
Save ScarletPonytail/ceffabe9b7278b56f9136ed259dc36ad to your computer and use it in GitHub Desktop.
jQuery - If .class is visible viewport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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