$(window).scroll(function() {
$('#answer').text((visible($('.box'), 25)));
});
// Test whether or not the element is in the viewport
function visible(el, per) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
// amount of px viewport needs to see to fire the event
var elPercentageHeight = rect.height * (per * 0.01);
var elPercentageWidth = rect.width * (per*0.01);
//console.log('elPercentageHeight: ' + elPercentageHeight);
//console.log('elPercentageWidth: ' + elPercentageWidth);
//console.log('viewportHeight: ' + window.innerHeight);
//console.log('viewportWidth: ' + window.innerWidth);
//console.log('rect: ');
//console.log(rect);
var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
var viewportHeight = window.innerHeight || document.documentElement.clientHeight;
//console.log(rect.top >= 0 - elPercentageHeight);
//console.log(rect.left >= 0 + elPercentageWidth);
//console.log(rect.left <= viewportWidth - elPercentageWidth);
//console.log(rect.right <= viewportWidth - elPercentageWidth);
//console.log(rect.right >= 0 + elPercentageWidth);
//console.log(rect.bottom <= viewportHeight + elPercentageHeight);
// if the element is within the viewport
// by definition we say if the top, left, and right are within the viewport or the bottom left and right are in the viewport
return (
(rect.top >= 0 - elPercentageHeight && rect.top <= viewportHeight - elPercentageHeight && rect.left >= 0 + elPercentageWidth && rect.left <= viewportWidth - elPercentageWidth) ||
(rect.top >= 0 - elPercentageHeight && rect.top <= viewportHeight - elPercentageHeight && rect.right <= viewportWidth - elPercentageWidth && rect.right >= 0 + elPercentageWidth) ||
(rect.bottom <= viewportHeight +...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.