Created
October 10, 2013 13:15
-
-
Save paldepind/6918117 to your computer and use it in GitHub Desktop.
This file contains 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
angular.module('myApp', []).directive('includeIfVisible', function () { | |
function isElementInViewport(el) { | |
var rect = el.getBoundingClientRect(); | |
return ( | |
rect.top <= (window.innerHeight || document.documentElement.clientHeight) && | |
rect.right >= 0 && | |
rect.bottom >= 0 && | |
rect.left <= (window.innerWidth || document.documentElement.clientWidth) | |
); | |
} | |
return function(scope, element, attr) { | |
element.scroll(function() { | |
var chlds = element.find('[data-ng-include]'); | |
chlds.each(function () { | |
scrolled++; | |
if (isElementInViewport(this)) { | |
// Child is visible | |
} else { | |
// Child is invisible | |
} | |
}); | |
}); | |
}; | |
}); |
This file contains 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
<div id="thumbnailPagesWrapper" data-include-if-visible> | |
<div data-ng-repeat="p in pages" class="pageThumbnailWrapper"> | |
<!-- the template functions figures out what template to use but if the child | |
is not on the screen `template` needs to know in which case it will return | |
and empty string clearing the content --> | |
<div data-ng-include data-src="template(p)" scope="p"></div> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment